From 193cf87424e75931c88f10d7293c42a62dc108ab Mon Sep 17 00:00:00 2001 From: Quirijn Slings Date: Thu, 19 Sep 2024 09:46:15 +0200 Subject: [PATCH] added separate sample class to test the new ways of finding assets --- Bynder/Sample/ApiSample.cs | 7 + Bynder/Sample/FindMediaSample.cs | 163 +++++++++++++++++++ Bynder/Sample/MediaSample.cs | 10 +- Bynder/Sample/Properties/launchSettings.json | 4 + 4 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 Bynder/Sample/FindMediaSample.cs diff --git a/Bynder/Sample/ApiSample.cs b/Bynder/Sample/ApiSample.cs index 84546d9..837f4b8 100644 --- a/Bynder/Sample/ApiSample.cs +++ b/Bynder/Sample/ApiSample.cs @@ -63,6 +63,13 @@ public static async Task Main(string[] args) return; } + // Run samples related to finding media + if (args[0].Equals("FindMediaSample")) + { + Console.WriteLine("Running samples for find media..."); + await FindMediaSample.MediaSampleAsync(); + return; + } // Run samples related to modifying media if (args[0].Equals("ModifyMediaSample")) { diff --git a/Bynder/Sample/FindMediaSample.cs b/Bynder/Sample/FindMediaSample.cs new file mode 100644 index 0000000..7cd3da0 --- /dev/null +++ b/Bynder/Sample/FindMediaSample.cs @@ -0,0 +1,163 @@ +// 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; +using Bynder.Sdk.Model; +using System.Collections.Generic; + +namespace Bynder.Sample +{ + public class FindMediaSample + { + private IBynderClient _bynderClient; + + public static async Task MediaSampleAsync() + { + var configuration = Configuration.FromJson("Config.json"); + var apiSample = new FindMediaSample(configuration); + await apiSample.AuthenticateWithOAuth2Async( + useClientCredentials: configuration.RedirectUri == null + ); + await apiSample.RunFindMediaSampleAsync(); + } + + private FindMediaSample(Configuration configuration) + { + _bynderClient = ClientFactory.Create(configuration); + } + + private async Task RunFindMediaSampleAsync() + { + var metaProperties = await _bynderClient.GetAssetService().GetMetapropertiesAsync(); + bool c = true; + while (c) + { + await PerformSearch(metaProperties); + Console.WriteLine("Do you want to perform another search? (y/N)"); + var inp = Console.ReadLine(); + c = inp.ToLower().StartsWith("y"); + } + } + + private async Task PerformSearch(IDictionary metaProperties) + { + Console.WriteLine("You have the following meta properties in your Bynder environment: "); + var counter = 1; + foreach (var metaProperty in metaProperties) + { + var extraInfo = metaProperty.Value.Options?.Any() ?? false ? $"[with {metaProperty.Value.Options.Count()} options]" : "[without options]"; + Console.WriteLine($"{counter++}) {metaProperty.Key} {extraInfo}"); + } + Console.WriteLine("Type the number of the meta property to perform a search with: "); + var mpNrInput = Console.ReadLine(); + if (!int.TryParse(mpNrInput, out int mpNr)) + { + mpNr = 1; + } + var selectedMetaProperty = metaProperties.Skip(mpNr - 1).FirstOrDefault().Value; + if (selectedMetaProperty == null) + { + Console.WriteLine("No meta property found, stopping execution"); + + return ; + } + + string searchString = null; + if (selectedMetaProperty.Options?.Any() ?? false) + { + counter = 1; + foreach (var option in selectedMetaProperty.Options) + { + Console.WriteLine($"{counter++}) {option.Label}"); + } + Console.WriteLine("Type the number of the option to search for: "); + mpNrInput = Console.ReadLine(); + if (!int.TryParse(mpNrInput, out mpNr)) + { + mpNr = 1; + } + var selectedOption = selectedMetaProperty.Options.Skip(mpNr - 1).FirstOrDefault(); + searchString = selectedOption.Name; + + Console.WriteLine("Searching via the meta property"); + var assets = await _bynderClient.GetAssetService().GetMediaListAsync(new MediaQuery() + { + MetaProperties = new Dictionary> + { + { + selectedMetaProperty.Name, [ searchString ] + } + } + }); + + if (assets?.Any() ?? false) + { + Console.WriteLine($"Found {assets.Count} assets, showing first 5"); + counter = 1; + foreach (var asset in assets) + { + Console.WriteLine($"{counter++}) {asset.Name}"); + if (counter == 6) + { + break; + } + } + } + else + { + Console.WriteLine("No assets found by metaproperty"); + } + } + else + { + Console.WriteLine("String to search for: "); + searchString = Console.ReadLine(); + } + Console.WriteLine("Searching by keyword"); + var assetsByKeyword = await _bynderClient.GetAssetService().GetMediaListAsync(new MediaQuery() + { + Keyword = searchString + } + ); + if (assetsByKeyword?.Any() ?? false) + { + Console.WriteLine($"Found {assetsByKeyword.Count} assets, showing first 5"); + counter = 1; + foreach (var asset in assetsByKeyword) + { + Console.WriteLine($"{counter++}) {asset.Name}"); + if (counter == 6) + { + break; + } + } + } + else + { + Console.WriteLine("No assets found by keyword"); + } + } + + 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); + } + } + + } +} diff --git a/Bynder/Sample/MediaSample.cs b/Bynder/Sample/MediaSample.cs index ed92cad..146e463 100644 --- a/Bynder/Sample/MediaSample.cs +++ b/Bynder/Sample/MediaSample.cs @@ -32,6 +32,7 @@ private MediaSample(Configuration configuration) { private async Task RunMediaSampleAsync() { + // Get a list of media with limit 10 Console.WriteLine("Listing media with limit of 10: "); var mediaList = await _bynderClient.GetAssetService().GetMediaListAsync(new MediaQuery{Limit=10}); @@ -54,7 +55,14 @@ private async Task RunMediaSampleAsync() Console.WriteLine($"ID: {mediaInfo.Id}"); Console.WriteLine($"Name: {mediaInfo.Name}"); Console.WriteLine($"Brand Id: {mediaInfo.BrandId}"); - Console.WriteLine($"Asset type: {string.Join(',', mediaInfo.PropertyAssetType)}"); + if (mediaInfo.PropertyAssetType == null) + { + Console.WriteLine($"No asset type"); + } + else + { + Console.WriteLine($"Asset type: {string.Join(',', mediaInfo.PropertyAssetType)}"); + } if (mediaInfo.PropertyOptionsDictionary != null) { foreach (var propertyKey in mediaInfo.PropertyOptionsDictionary.Keys) diff --git a/Bynder/Sample/Properties/launchSettings.json b/Bynder/Sample/Properties/launchSettings.json index 6c8a765..3434b7c 100644 --- a/Bynder/Sample/Properties/launchSettings.json +++ b/Bynder/Sample/Properties/launchSettings.json @@ -39,6 +39,10 @@ "Run ModifyMediaSample": { "commandName": "Project", "commandLineArgs": "ModifyMediaSample" + }, + "Run FindMediaSample": { + "commandName": "Project", + "commandLineArgs": "FindMediaSample" } } } \ No newline at end of file