-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
various fixes and enhancements regarding the modification of assets
- Loading branch information
1 parent
19fe12b
commit 99140e2
Showing
7 changed files
with
278 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// 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}"); | ||
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): "); | ||
var cmd = Console.ReadLine(); | ||
var 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); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Bynder.Sdk.Api.Requests; | ||
using Bynder.Sdk.Query.Asset; | ||
using Bynder.Sdk.Query.Decoder; | ||
using Bynder.Sdk.Utils; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Bynder.Test.Utils | ||
{ | ||
public class QueryParameterTest | ||
{ | ||
[Fact] | ||
public void BooleanPropertiesSetWhenModified() | ||
{ | ||
ModifyMediaQuery query = new ModifyMediaQuery("") | ||
{ | ||
Archive = false, | ||
}; | ||
|
||
var parametersAsString = GetQueryParameters(query); | ||
Assert.DoesNotContain("isPublic=", parametersAsString); | ||
Assert.Contains("archive=False", parametersAsString); | ||
} | ||
|
||
[Fact] | ||
public void MetaPropertyCanBeDeleted() | ||
{ | ||
ModifyMediaQuery query = new ModifyMediaQuery("") | ||
{ | ||
MetapropertyOptions = new Dictionary<string, IList<string>>() | ||
{ | ||
{ | ||
"TestProp", | ||
[] | ||
} | ||
} | ||
}; | ||
var parametersAsString = GetQueryParameters(query); | ||
Assert.Contains("metaproperty.TestProp=", parametersAsString); | ||
} | ||
|
||
[Fact] | ||
public void StringPropertyCanBeErased() | ||
{ | ||
ModifyMediaQuery query = new ModifyMediaQuery("") | ||
{ | ||
Name = "", | ||
Copyright = null, | ||
}; | ||
var parametersAsString = GetQueryParameters(query); | ||
|
||
Assert.Contains("name=", parametersAsString); | ||
Assert.DoesNotContain("copyright=", parametersAsString); | ||
} | ||
|
||
private string GetQueryParameters(ModifyMediaQuery query) | ||
{ | ||
QueryDecoder queryDecoder = new QueryDecoder(); | ||
var parameters = queryDecoder.GetParameters(query); | ||
return Url.ConvertToQuery(parameters); | ||
} | ||
|
||
} | ||
} |