Skip to content

Commit

Permalink
fixed merge conflicts with master
Browse files Browse the repository at this point in the history
  • Loading branch information
quirijnslings committed Sep 19, 2024
2 parents 0289a16 + e472748 commit 5fe4be5
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,4 @@ FodyWeavers.xsd
### VisualStudio Patch ###
# Additional files built by Visual Studio

# End of https://www.toptal.com/developers/gitignore/api/visualstudio
# End of https://www.toptal.com/developers/gitignore/api/visualstudio
9 changes: 9 additions & 0 deletions Bynder/Sample/ApiSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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"
}
}
}
74 changes: 66 additions & 8 deletions Bynder/Sample/TagsSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using Bynder.Sdk.Query.Asset;
using Bynder.Sdk.Model;
using Bynder.Sdk.Service.Asset;

namespace Bynder.Sample
{
Expand All @@ -33,25 +34,82 @@ private TagsSample(Configuration configuration) {

private async Task RunTagsSampleAsync()
{
var assetService = _bynderClient.GetAssetService();
Console.WriteLine("Getting tags with a limit of 10: ");
var tags = await _bynderClient.GetAssetService().GetTagsAsync(new GetTagsQuery{Limit = 10});
var tags = await assetService.GetTagsAsync(new GetTagsQuery{Limit = 10});
foreach(Tag tag in tags){
Console.WriteLine($"Tag Id: {tag.ID}");
Console.WriteLine($"Tag Name: {tag.TagName}");
Console.WriteLine($"Tag MediaCount: {tag.MediaCount}");
}

Console.WriteLine("Enter the media ID to add a tag to: ");
var mediaIdAddTag = Console.ReadLine();
var mediaId = Console.ReadLine();
Console.WriteLine("Enter the tag ID to add to the media: ");
var tagIdAddToMedia = Console.ReadLine();
List<string> mediasAddTag = new List<string>
var tagId = Console.ReadLine();
await assetService.AddTagToMediaAsync(new AddTagToMediaQuery(tagId, [ mediaId ]));

Console.WriteLine("Hit enter to view the asset (it may take a few seconds before the tag is registered)");
Console.ReadKey();
var asset = await assetService.GetMediaInfoAsync(new MediaInformationQuery() { MediaId = mediaId });
ShowTags(asset);

Console.WriteLine("Enter a new tag to add to the same media: ");
var anotherTag = Console.ReadLine();
if (asset.Tags == null)
{
asset.Tags = [anotherTag];
}
else
{
asset.Tags.Add(anotherTag);
}

await assetService.ModifyMediaAsync(new ModifyMediaQuery(mediaId) { Tags = asset.Tags } );

Console.WriteLine("Hit enter to view the asset (it may take a few seconds before the tag is registered)");
Console.ReadKey();
asset = await assetService.GetMediaInfoAsync(new MediaInformationQuery() { MediaId = mediaId });
ShowTags(asset);

Console.WriteLine("Hit enter to remove the tags again");
Console.ReadKey();

foreach (var tag in asset.Tags)
{
var matchingTags = await assetService.GetTagsAsync(new GetTagsQuery() { Keyword = tag });
if (matchingTags.Any())
{
var tagToRemove = matchingTags.FirstOrDefault(t => t.TagName.Equals(tag, StringComparison.InvariantCultureIgnoreCase));
Console.WriteLine($"Removing tag {tagToRemove.TagName} with id {tagToRemove.ID}");
await assetService.RemoveTagFromMediaAsync(tagToRemove.ID, [mediaId]);
}
else
{
Console.WriteLine($"Error: after adding tag with name '{tag}' to asset {mediaId}, tag cannot be found in Bynder");
}
}

Console.WriteLine("Hit enter to view the asset (it may take a few seconds before the tags have been removed)");
Console.ReadKey();

asset = await assetService.GetMediaInfoAsync(new MediaInformationQuery() { MediaId = mediaId });
ShowTags(asset);

}

private async void ShowTags(Media asset)
{
if (asset.Tags?.Any() ?? false)
{
Console.WriteLine($"Media with name {asset.Name} now has the following tags: {string.Join(',', asset.Tags)}");
}
else
{
mediaIdAddTag
};
await _bynderClient.GetAssetService().AddTagToMediaAsync(new AddTagToMediaQuery(tagIdAddToMedia, mediasAddTag));
Console.WriteLine($"Media with name {asset.Name} has no tags");
}
}

private async Task AuthenticateWithOAuth2Async(bool useClientCredentials)
{
if (useClientCredentials)
Expand Down
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
Loading

0 comments on commit 5fe4be5

Please sign in to comment.