Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3-find-assets #96

Merged
merged 15 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Bynder/Sample/ApiSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,28 @@ public static async Task Main(string[] args)
await MetapropertiesSample.MetapropertiesSampleAsync();
return;
}

// Run samples related to the relation between metaproperties and media items
if (args[0].Equals("MetapropertyToMediaSample")) {
Console.WriteLine("Running samples for metaproperties and related media...");
await MetaPropertyToMediaSample.MetaPropertyToMediaSampleAsync();
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 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"))
{
Expand Down
191 changes: 191 additions & 0 deletions Bynder/Sample/FindMediaSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// 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<string, Metaproperty> metaProperties)
{
Console.WriteLine("You have the following meta properties in your Bynder environment: ");
var mpKeys = metaProperties.Keys.OrderBy(k => k);
var counter = 1;
foreach (var metaProperty in metaProperties.OrderBy(mp => mp.Key))
{
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 selectedMetaPropertyKey = mpKeys.Skip(mpNr - 1).FirstOrDefault();
var selectedMetaProperty = metaProperties[selectedMetaPropertyKey];
if (selectedMetaProperty == null)
{
Console.WriteLine("No meta property found, stopping execution");

return ;
}

string searchString = null;
if (selectedMetaProperty.Options?.Any() ?? false)
{
counter = 1;
var sortedOptions = selectedMetaProperty.Options.OrderBy(o => o.Label);
foreach (var option in sortedOptions)
{
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 = sortedOptions.Skip(mpNr - 1).FirstOrDefault();
searchString = selectedOption.Name;

Console.WriteLine($"Searching via the meta property named {selectedMetaProperty.Name} and option named {searchString}");
var assets = await _bynderClient.GetAssetService().GetMediaListAsync(new MediaQuery()
{
MetaProperties = new Dictionary<string, IList<string>>
{
{
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 name / option name");
}

Console.WriteLine($"Searching via the meta property option ID {selectedOption.Id}");
assets = await _bynderClient.GetAssetService().GetMediaListAsync(new MediaQuery()
{
PropertyOptionId = [ selectedOption.Id ]
});

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 option id");
}

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

}
}
14 changes: 13 additions & 1 deletion Bynder/Sample/MediaSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand All @@ -40,6 +41,10 @@ private async Task RunMediaSampleAsync()
Console.WriteLine($"Media Name: {media.Name}");
}

// Get ths same list as a full result
var mediaFullResult = await _bynderClient.GetAssetService().GetMediaFullResultAsync(new MediaQuery { Limit = 10 });
Console.WriteLine($"Retrieving full result based on same query, total number of matching assets is {mediaFullResult.Total.Count}");

// Get the media info
Console.WriteLine("Enter the media ID to get the media info for: ");
var mediaIdForInfo = Console.ReadLine();
Expand All @@ -50,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)
Expand Down
118 changes: 118 additions & 0 deletions Bynder/Sample/MetaPropertyToMediaSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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;
using Newtonsoft.Json.Linq;
using System.Text;

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

public static async Task MetaPropertyToMediaSampleAsync()
{
var configuration = Configuration.FromJson("Config.json");
Console.WriteLine($"BaseUrl: {configuration.BaseUrl}");
var apiSample = new MetaPropertyToMediaSample(configuration);
await apiSample.AuthenticateWithOAuth2Async(
useClientCredentials: configuration.RedirectUri == null
);
await apiSample.RunMetaPropertyToMediaSampleAsync();
}

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

private async Task RunMetaPropertyToMediaSampleAsync()
{
var assetService = _bynderClient.GetAssetService();
// Get a list of media with limit 10
Console.WriteLine("Available metaproperties: ");
var metaProperties = await assetService.GetMetapropertiesAsync();
int i = 0;
foreach(var metaProperty in metaProperties.Values) {
Console.WriteLine($"({++i}) MetaProperty {metaProperty.Name} ({metaProperty.Id})");
}
Console.WriteLine("Enter number of the metaProperty to search by");
var metaPropertyNr = Convert.ToInt32(Console.ReadLine());
var metaPropertySelected = metaProperties.Skip(metaPropertyNr - 1).FirstOrDefault().Value;
i = 0;
foreach (var option in metaPropertySelected.Options)
{
Console.WriteLine($"({++i}) Option {option.Name} ({option.Id})");
}
Console.WriteLine("Enter number of the option to search by, or a text value");
var optionSearchText = Console.ReadLine();
if (Int32.TryParse(optionSearchText, out int optionNr))
{
var optionSelected = metaPropertySelected.Options.Skip(optionNr - 1).FirstOrDefault();
optionSearchText = optionSelected.Name;
}

// Get matching media (assets)
var mediaQuery = new MediaQuery()
{
MetaProperties = new Dictionary<string, IList<string>> { { metaPropertySelected.Name, [optionSearchText] } },
Limit = 10
};
var mediaList = await _bynderClient.GetAssetService().GetMediaListAsync(mediaQuery);
foreach (var media in mediaList)
{
Console.WriteLine($"ID: {media.Id}");
Console.WriteLine($"Name: {media.Name}");
Console.WriteLine($"Meta properties: {ShowMetaProperties(media.PropertyOptionsDictionary)}");
Console.WriteLine("-----------------");
}
}

private string ShowMetaProperties(Dictionary<string, JToken> propertyOptionsDictionary)
{
if (propertyOptionsDictionary == null)
{
return "";
}
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (var key in propertyOptionsDictionary.Keys)
{
if (first)
{
first = false;
}
else
{
sb.Append("|");
}
sb.Append($"{key.Replace("property_","")}:{string.Join(',', propertyOptionsDictionary[key].Select(a => a.ToString()))}");
}
return sb.ToString();
}

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

}
}
4 changes: 4 additions & 0 deletions Bynder/Sample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
"Run ModifyMediaSample": {
"commandName": "Project",
"commandLineArgs": "ModifyMediaSample"
},
"Run FindMediaSample": {
"commandName": "Project",
"commandLineArgs": "FindMediaSample"
}
}
}
Loading