forked from Bynder/bynder-c-sharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiSample.cs
130 lines (115 loc) · 4.72 KB
/
ApiSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) Bynder. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
using System;
using Bynder.Sdk.Service;
using Bynder.Sample.Utils;
using Bynder.Sdk.Query.Asset;
using Bynder.Sdk.Query.Collection;
using Bynder.Sdk.Settings;
using System.Threading.Tasks;
using System.Linq;
using Bynder.Sdk.Model;
using Bynder.Sdk.Query.Upload;
namespace Bynder.Sample
{
/// <summary>
/// Class to show a little example on how to use the Bynder C# SDK. Before running this example is important that you fill
/// API_BASE_URL, CONSUMER_KEY and CONSUMER_SECRET in App.config
/// </summary>
public class ApiSample
{
private IBynderClient _bynderClient;
/// <summary>
/// Main function
/// </summary>
/// <param name="args">arguments to main</param>
public static async Task Main(string[] args)
{
var configuration = Configuration.FromJson("Config.json");
var apiSample = new ApiSample(configuration);
await apiSample.AuthenticateWithOAuth2Async(
useClientCredentials: configuration.RedirectUri == null
);
await apiSample.ListItemsAsync();
await apiSample.UploadFileAsync("/path/to/file.ext");
}
private ApiSample(Configuration configuration) {
_bynderClient = ClientFactory.Create(configuration);
}
private async Task ListItemsAsync()
{
var brands = await _bynderClient.GetAssetService().GetBrandsAsync();
Console.WriteLine($"Brands: [{string.Join(", ", brands.Select(m => m.Name))}]");
var mediaList = await _bynderClient.GetAssetService().GetMediaListAsync(
new MediaQuery
{
Type = AssetType.Image,
Limit = 10,
Page = 1,
}
);
Console.WriteLine($"Assets: [{string.Join(", ", mediaList.Select(m => m.Name))}]");
var collectionList = await _bynderClient.GetCollectionService().GetCollectionsAsync(
new GetCollectionsQuery
{
Limit = 10,
Page = 1,
}
);
Console.WriteLine($"Collections: [{string.Join(", ", mediaList.Select(m => m.Name))}]");
}
private async Task UploadFileAsync(string uploadPath)
{
var assetService = _bynderClient.GetAssetService();
var brands = await assetService.GetBrandsAsync();
if (!brands.Any())
{
Console.Error.WriteLine("No brands found in this account.");
return;
}
await assetService.UploadFileAsync(new UploadQuery { Filepath = uploadPath, BrandId = brands.First().Id });
//TODO: This can be done instead when UploadFileToNewAssetAsync gets the correct response type
//var saveMediaResponse = await assetService.UploadFileToNewAssetAsync(uploadPath, brands.First().Id);
//Console.WriteLine($"Asset uploaded: {saveMediaResponse.MediaId}");
//
//Media media = null;
//for (int iterations = 10; iterations > 0; --iterations)
//{
// try
// {
// media = await assetService.GetMediaInfoAsync(
// new MediaInformationQuery
// {
// MediaId = saveMediaResponse.MediaId,
// }
// );
// }
// catch (HttpRequestException)
// {
// await Task.Delay(1000).ConfigureAwait(false);
// }
//}
//if (media == null)
//{
// Console.Error.WriteLine("The asset could not be retrieved");
// return;
//}
//var saveMediaVersionResponse = await assetService.UploadFileToExistingAssetAsync(uploadPath, media.Id);
//Console.WriteLine($"New asset version uploaded: {saveMediaVersionResponse.MediaId}");
}
private async Task AuthenticateWithOAuth2Async(bool useClientCredentials)
{
if (useClientCredentials)
{
await _bynderClient.GetOAuthService().GetAccessTokenAsync();
}
else
{
Browser.Launch(_bynderClient.GetOAuthService().GetAuthorisationUrl("state example"));
Console.WriteLine("Insert the code: ");
var code = Console.ReadLine();
await _bynderClient.GetOAuthService().GetAccessTokenAsync(code);
}
}
}
}