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-modify-asset #93

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions Bynder/Sample/ApiSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public static async Task Main(string[] args)
await MediaSample.MediaSampleAsync();
return;
}

// Run samples related to modifying media
if (args[0].Equals("ModifyMediaSample"))
{
Console.WriteLine("Running samples for the modification of media...");
await ModifyMediaSample.ModifyMediaSampleAsync();
return;
}

// Run samples related to collections
if (args[0].Equals("CollectionsSample")) {
Console.WriteLine("Running samples for collections...");
Expand Down
179 changes: 179 additions & 0 deletions Bynder/Sample/ModifyMediaSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// 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;

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

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

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

private async Task RunModifyMediaSampleAsync()
{
var assetService = _bynderClient.GetAssetService();
// Get a list of media with limit 10
Console.WriteLine("Listing media with limit of 10: ");
var mediaList = await assetService.GetMediaListAsync(new MediaQuery { Limit = 10 });
foreach (Media media in mediaList)
{
Console.WriteLine($"Media ID: {media.Id}");
Console.WriteLine($"Media Name: {media.Name}");
}

// Get the media info
Console.WriteLine("Enter the media ID to modify: ");
var mediaId = Console.ReadLine();
var mediaInformationQuery = new MediaInformationQuery
{
MediaId = mediaId.Trim()
};
var mediaInfo = await assetService.GetMediaInfoAsync(mediaInformationQuery);
Console.WriteLine($"ID: {mediaInfo.Id}");
Console.WriteLine($"Name: {mediaInfo.Name}");


// datePublished
Console.WriteLine($"---\r\nTest with datePublished");
Console.WriteLine($"datePublished is currently set to: {mediaInfo.DatePublished}");
Console.WriteLine("New value (use ISO8601 format: yyyy-mm-ddThh:mm:ssZ, or n = now, or leave empty to erase): ");
var cmd = Console.ReadLine();
var query = new ModifyMediaQuery(mediaId);
if (cmd.ToLower().StartsWith("n"))
{
query.PublishedDate = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");
}
else if (string.IsNullOrEmpty(cmd))
{
query.PublishedDate = "";
}
else
{
query.PublishedDate = cmd;
}
await assetService.ModifyMediaAsync(query);
Console.WriteLine("The asset has been modified. It takes a few seconds for changes come through. Hit enter when you want to retrieve the asset again.");
Console.ReadLine();
mediaInfo = await assetService.GetMediaInfoAsync(mediaInformationQuery);
Console.WriteLine($"datePublished is now set to: {mediaInfo.DatePublished}");

// isArchived (boolean)
Console.WriteLine($"---\r\nTest with boolean value (isArchived)");
Console.WriteLine($"isArchived is currently set to: {mediaInfo.IsArchived}");
Console.WriteLine("New value (t=true, f=false, n=not set): ");
cmd = Console.ReadLine();
query = new ModifyMediaQuery(mediaId);
if (cmd.ToLower().StartsWith("t"))
{
query.Archive = true;
}
else if (cmd.ToLower().StartsWith("f"))
{
query.Archive = false;
}
await assetService.ModifyMediaAsync(query);
Console.WriteLine("The asset has been modified. It takes a few seconds for changes come through. Hit enter when you want to retrieve the asset again.");
Console.ReadLine();
mediaInfo = await assetService.GetMediaInfoAsync(mediaInformationQuery);
Console.WriteLine($"isArchived is now set to: {mediaInfo.IsArchived}");

// Copyright message
Console.WriteLine($"---\r\nTest with string value (copyright)");
Console.WriteLine($"Copyright message is currently set to {mediaInfo.Copyright}");
Console.WriteLine($"Please supply a new value for the copyright message, or hit enter to erase it");

cmd = Console.ReadLine();
query = new ModifyMediaQuery(mediaId);
query.Copyright = string.IsNullOrEmpty(cmd) ? "" : cmd;
await assetService.ModifyMediaAsync(query);
Console.WriteLine("The asset has been modified. It takes a few seconds for changes come through. Hit enter when you want to retrieve the asset again.");
Console.ReadLine();
mediaInfo = await assetService.GetMediaInfoAsync(mediaInformationQuery);
Console.WriteLine($"Copyright message is now set to {mediaInfo.Copyright}");

// Metaproperties
if (!(mediaInfo.PropertyOptionsDictionary?.Keys?.Any() ?? false))
{
Console.WriteLine("This media item has no meta properties, please choose a different one the next time! Bye for now.");
return;
}
var metaprop = mediaInfo.PropertyOptionsDictionary.FirstOrDefault();
var metaPropertyName = metaprop.Key.Replace("property_", "");

Console.WriteLine($"---\r\nTest with metaproperties");
Console.WriteLine($"Meta property {metaprop.Key} is currently set to {metaprop.Value.ToString()}");
Console.WriteLine($"Please supply a new value for the meta property, or hit enter to erase it");

cmd = Console.ReadLine();

// get ID of the meta property
var metaProperties = await assetService.GetMetapropertiesAsync();
var metaProperty = metaProperties.Values.FirstOrDefault(mp => mp.Name == metaPropertyName);
if (metaProperty == null)
{
throw new Exception("Unable to find property with name " + metaprop.Key.Replace("property_", ""));
}

query = new ModifyMediaQuery(mediaId);
query.MetapropertyOptions = new Dictionary<string,IList<string>>()
{
{
metaProperty.Id,
string.IsNullOrEmpty(cmd) ? [] : [cmd]
}
};
await assetService.ModifyMediaAsync(query);
Console.WriteLine("The asset has been modified. It takes a few seconds for changes come through. Hit enter when you want to retrieve the asset again.");
Console.ReadLine();
mediaInfo = await assetService.GetMediaInfoAsync(mediaInformationQuery);
if (mediaInfo.PropertyOptionsDictionary?.TryGetValue("property_" + metaPropertyName, out JToken value) ?? false)
{
Console.WriteLine($"Meta property {metaPropertyName} is now set to {value.ToString()}");
}
else
{
Console.WriteLine($"Asset has no value for metaproperty {metaPropertyName}");
}
}

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 @@ -36,5 +36,9 @@
"commandName": "Project",
"commandLineArgs": "AssetUsageSample"
},
"Run ModifyMediaSample": {
"commandName": "Project",
"commandLineArgs": "ModifyMediaSample"
}
}
}
8 changes: 6 additions & 2 deletions Bynder/Sdk/Api/RequestSender/HttpRequestSender.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Bynder. All rights reserved.
// Copyright (c) Bynder. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.

using System.Net.Http;
Expand Down Expand Up @@ -32,7 +32,11 @@ public string UserAgent
public async Task<HttpResponseMessage> SendHttpRequest(HttpRequestMessage httpRequest)
{
httpRequest.Headers.Add("User-Agent", UserAgent);
var response = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
var response = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().Result;
}
response.EnsureSuccessStatusCode();
return response;
}
Expand Down
12 changes: 9 additions & 3 deletions Bynder/Sdk/Query/Asset/ModifyMediaQuery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Bynder.Sdk.Api.Converters;
using Bynder.Sdk.Query.Decoder;

Expand Down Expand Up @@ -41,17 +41,23 @@ public ModifyMediaQuery(string mediaId)
[ApiField("copyright")]
public string Copyright { get; set; }

/// <summary>
/// Published date for the media
/// </summary>
[ApiField("datePublished")]
public string PublishedDate { get; set; }

/// <summary>
/// Indicates if the media is archived
/// </summary>
[ApiField("archive")]
public bool Archive { get; set; }
public bool? Archive { get; set; }

/// <summary>
/// Indicates if the media is public
/// </summary>
[ApiField("isPublic")]
public bool IsPublic { get; set; }
public bool? IsPublic { get; set; }

/// <summary>
/// Metaproperty options to set on the asset.
Expand Down
60 changes: 28 additions & 32 deletions Bynder/Sdk/Query/Decoder/QueryDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Bynder. All rights reserved.
// 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 System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Bynder.Sdk.Api.Converters;

Expand Down Expand Up @@ -43,46 +44,41 @@ public IDictionary<string, string> GetParameters(object query)
/// <param name="collection">collection to add the converted values</param>
private void ConvertProperty(PropertyInfo propertyInfo, object query, IDictionary<string, string> parameters)
{
foreach (var attribute in propertyInfo.GetCustomAttributes(true))
var apiField = propertyInfo.GetCustomAttributes(true).FirstOrDefault(a => a is ApiField) as ApiField;
if (apiField == null)
{
if (attribute is ApiField apiField)
{
object value = propertyInfo.GetValue(query);
if (value == null)
{
return;
}
return;
}

if (apiField.Converter == null)
{
AddParam(parameters, apiField.ApiName, value.ToString());
}
else if (Activator.CreateInstance(apiField.Converter) is ITypeToStringConverter stringConverter
&& stringConverter.CanConvert(propertyInfo.PropertyType))
{
AddParam(parameters, apiField.ApiName, stringConverter.Convert(value));
}
else if (Activator.CreateInstance(apiField.Converter) is ITypeToDictionaryConverter dictConverter
&& dictConverter.CanConvert(propertyInfo.PropertyType))
{
foreach (var item in dictConverter.Convert(value))
{
AddParam(parameters, $"{apiField.ApiName}.{item.Key}", item.Value);
}
}
object value = propertyInfo.GetValue(query);
if (value == null)
{
return;
}

// No need to continue. Only one ApiField attribute per property
return;
if (apiField.Converter == null)
{
AddParam(parameters, apiField.ApiName, value.ToString());
}
else if (Activator.CreateInstance(apiField.Converter) is ITypeToStringConverter stringConverter
&& stringConverter.CanConvert(propertyInfo.PropertyType))
{
AddParam(parameters, apiField.ApiName, stringConverter.Convert(value));
}
else if (Activator.CreateInstance(apiField.Converter) is ITypeToDictionaryConverter dictConverter
&& dictConverter.CanConvert(propertyInfo.PropertyType))
{
foreach (var item in dictConverter.Convert(value))
{
AddParam(parameters, $"{apiField.ApiName}.{item.Key}", item.Value);
}
}

}

private void AddParam(IDictionary<string, string> parameters, string key, string value)
{
if (!string.IsNullOrEmpty(value))
{
parameters.Add(key, value);
}
parameters.Add(key, value);
}

}
Expand Down
Loading