Skip to content

Commit

Permalink
API-1823 Add sample files for existing methods in the SDK and updatin…
Browse files Browse the repository at this point in the history
…g README
  • Loading branch information
thegreatdeku committed Dec 12, 2023
1 parent bf704f9 commit 4c52984
Show file tree
Hide file tree
Showing 9 changed files with 736 additions and 93 deletions.
135 changes: 44 additions & 91 deletions Bynder/Sample/ApiSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,103 +28,56 @@ public class ApiSample
/// <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())
if (args.Length == 0)
{
Console.Error.WriteLine("No brands found in this account.");
Console.WriteLine("Please enter the sample Class to run.");
Console.WriteLine("Usage: dotnet run -- BrandsSample");
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();
Console.WriteLine(args[0]);
// Run samples related to brands
if (args[0].Equals("BrandsSample")) {
Console.WriteLine("Running samples for brands...");
await BrandsSample.BrandsSampleAsync();
return;
}
else
{
Browser.Launch(_bynderClient.GetOAuthService().GetAuthorisationUrl("state example"));
Console.WriteLine("Insert the code: ");
var code = Console.ReadLine();
await _bynderClient.GetOAuthService().GetAccessTokenAsync(code);
// Run samples related to metaproperties
if (args[0].Equals("MetapropertiesSample")) {
Console.WriteLine("Running samples for metaproperties...");
await MetapropertiesSample.MetapropertiesSampleAsync();
return;
}
// Run samples related to media
if (args[0].Equals("MediaSample")) {
Console.WriteLine("Running samples for media...");
await MediaSample.MediaSampleAsync();
return;
}
// Run samples related to collections
if (args[0].Equals("CollectionsSample")) {
Console.WriteLine("Running samples for collections...");
await CollectionsSample.CollectionsSampleAsync();
return;
}
// Run samples related to tags
if (args[0].Equals("TagsSample")) {
Console.WriteLine("Running samples for tags...");
await TagsSample.TagsSampleAsync();
return;
}
// Run samples related to upload
if (args[0].Equals("UploadSample")) {
Console.WriteLine("Running samples for upload...");
await UploadSample.UploadSampleAsync();
return;
}
// Run samples related to asset usage
if (args[0].Equals("AssetUsageSample")) {
Console.WriteLine("Running samples for asset usage...");
await AssetUsageSample.AssetUsageSampleAsync();
return;
}
}

}
}
64 changes: 64 additions & 0 deletions Bynder/Sample/AssetUsageSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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.Settings;
using System.Threading.Tasks;
using System.Linq;
using Bynder.Sdk.Query.Asset;

namespace Bynder.Sample
{
public class AssetUsageSample
{
private IBynderClient _bynderClient;

public static async Task AssetUsageSampleAsync()
{
var configuration = Configuration.FromJson("Config.json");
var apiSample = new AssetUsageSample(configuration);
await apiSample.AuthenticateWithOAuth2Async(
useClientCredentials: configuration.RedirectUri == null
);
await apiSample.RunAssetUsageSampleAsync();
}

private AssetUsageSample(Configuration configuration) {
_bynderClient = ClientFactory.Create(configuration);
}

private async Task RunAssetUsageSampleAsync()
{
Console.WriteLine("Enter the media ID to create asset usage for: ");
var createAssetUsageMediaId = Console.ReadLine();
Console.WriteLine("Enter the integration ID to create the asset usage for: ");
var createAssetUsageIntegrationId = Console.ReadLine();
await _bynderClient.GetAssetService().CreateAssetUsage(new AssetUsageQuery(createAssetUsageIntegrationId, createAssetUsageMediaId));


Console.WriteLine("Enter the media ID to create delete usage from: ");
var deleteAssetUsageMediaId = Console.ReadLine();
Console.WriteLine("Enter the integration ID to delete the asset usage from: ");
var deleteAssetUsageIntegrationId = Console.ReadLine();
await _bynderClient.GetAssetService().DeleteAssetUsage(new AssetUsageQuery(deleteAssetUsageIntegrationId, deleteAssetUsageMediaId));
}

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);
}
}

}
}
53 changes: 53 additions & 0 deletions Bynder/Sample/BrandsSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.Settings;
using System.Threading.Tasks;
using System.Linq;

namespace Bynder.Sample
{
public class BrandsSample
{
private IBynderClient _bynderClient;

public static async Task BrandsSampleAsync()
{
var configuration = Configuration.FromJson("Config.json");
var apiSample = new BrandsSample(configuration);
await apiSample.AuthenticateWithOAuth2Async(
useClientCredentials: configuration.RedirectUri == null
);
await apiSample.RunBrandsSampleAsync();
}

private BrandsSample(Configuration configuration) {
_bynderClient = ClientFactory.Create(configuration);
}

private async Task RunBrandsSampleAsync()
{
var brands = await _bynderClient.GetAssetService().GetBrandsAsync();
Console.WriteLine($"Brands: [{string.Join(", ", brands.Select(m => m.Name))}]");
}

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);
}
}

}
}
Loading

0 comments on commit 4c52984

Please sign in to comment.