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-upload #98

Closed
wants to merge 7 commits into from
8 changes: 8 additions & 0 deletions Bynder/Sample/ApiSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ 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...");
Expand Down
4 changes: 4 additions & 0 deletions Bynder/Sample/MediaSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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 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);
}
}

}
}
74 changes: 72 additions & 2 deletions Bynder/Sample/UploadSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading.Tasks;
using System.Linq;
using Bynder.Sdk.Query.Upload;
using System.Collections.Generic;
using System.IO;
namespace Bynder.Sample
{
public class UploadSample
Expand Down Expand Up @@ -41,9 +43,77 @@ private async Task RunUploadSampleAsync()
return;
}

await assetService.UploadFileAsync(new UploadQuery { Filepath = uploadPath, BrandId = brands.First().Id });
Console.WriteLine("Name of the media item after upload: ");
var name = Console.ReadLine();
if (string.IsNullOrEmpty(name))
{
name = null;
}

Console.WriteLine("Override original filename (leave empty to use the actual filename): ");
var filename = Console.ReadLine();

Console.WriteLine("Description (leave empty to use default): ");
var description = Console.ReadLine();

var customParameters = GetCustomParameters();

Console.WriteLine("Do you want to pass a file stream to the SDK?: (y/n)");
var passAsStream = Console.ReadLine().ToLower().StartsWith("y");


var query = new UploadQuery
{
Filepath = uploadPath,
BrandId = brands.First().Id,
Name = name,
CustomParameters = customParameters
};
if (!string.IsNullOrEmpty(filename))
{
query.OriginalFileName = filename;
}
if (!string.IsNullOrEmpty(description))
{
query.Description = description;
}

FileStream fileStream = null;
if (passAsStream)
{
fileStream = File.Open(query.Filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
}
var before = DateTime.Now;
var response = passAsStream ? await assetService.UploadFileAsync(fileStream, query) : await assetService.UploadFileAsync(query);
var ms = Math.Round((DateTime.Now - before).TotalMilliseconds);
Console.WriteLine($"Uploaded file as media with id {response.MediaId} (time elapsed: {ms})");

}


private Dictionary<string,string> GetCustomParameters()
{
Console.WriteLine("Do you want to add custom parameters during the upload? (y/n)");
var input = Console.ReadLine();

if (!input.ToString().ToLower().StartsWith("y"))
{
return null;
}

Dictionary<string,string> parameters = new Dictionary<string,string>();
while (input.ToString().ToLower().StartsWith("y"))
{
Console.WriteLine("Parameter name: ");
var paramName = Console.ReadLine();
Console.WriteLine("Parameter value: ");
var paramValue = Console.ReadLine();
parameters.Add(paramName, paramValue);
Console.WriteLine("Do you want to add another custom parameter? (y/n)");
input = Console.ReadLine();
}
return parameters;
}

private async Task AuthenticateWithOAuth2Async(bool useClientCredentials)
{
if (useClientCredentials)
Expand Down
22 changes: 22 additions & 0 deletions Bynder/Sdk/Model/MediaFullResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bynder.Sdk.Model
{
public class MediaFullResult
{
[JsonProperty("total")]
public Total Total { get; set; }

[JsonProperty("media")]
public IList<Media> Media { get; set; }
}
public class Total
{
public long Count { get; set; }
}
}
15 changes: 15 additions & 0 deletions Bynder/Sdk/Query/Asset/MediaQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,20 @@ public class MediaQuery
/// </summary>
[ApiField("propertyOptionId", Converter = typeof(ListConverter))]
public IList<string> PropertyOptionId { get; set; } = new List<string>();

/// <summary>
/// Metaproperties
/// Look for assets by specifying meta properties and values by name
/// e.g. City: Amsterdam
/// </summary>
/// <remarks>
/// - Use the database names of the metaproperties, not the IDs
/// - If the metaproperty is of type single/multiple select, the values should be the database names of the option(s)
/// - If the metaproperty is of type text, the values refer to the text itself
/// </remarks>
///
[ApiField("property_", Converter = typeof(MetapropertyOptionsConverter))]
public IDictionary<string, IList<string>> MetaProperties { get; set; }

}
}
19 changes: 19 additions & 0 deletions Bynder/Sdk/Query/Asset/MediaQueryFull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Bynder. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using Bynder.Sdk.Model;
using Bynder.Sdk.Api.Converters;
using Bynder.Sdk.Query.Decoder;

namespace Bynder.Sdk.Query.Asset
{
/// <summary>
/// Query to filter media results, including the option to see the total number of results
/// </summary>
public class MediaQueryFull : MediaQuery
{
[ApiField("total")]
public bool Total { get; set; }
}
}
5 changes: 3 additions & 2 deletions Bynder/Sdk/Query/Decoder/QueryDecoder.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;
Expand Down Expand Up @@ -67,7 +67,7 @@ private void ConvertProperty(PropertyInfo propertyInfo, object query, IDictionar
{
foreach (var item in dictConverter.Convert(value))
{
AddParam(parameters, $"{apiField.ApiName}.{item.Key}", item.Value);
AddParam(parameters, $"{apiField.ApiName}{item.Key}", item.Value);
}
}

Expand All @@ -77,6 +77,7 @@ private void ConvertProperty(PropertyInfo propertyInfo, object query, IDictionar
}
}


private void AddParam(IDictionary<string, string> parameters, string key, string value)
{
if (!string.IsNullOrEmpty(value))
Expand Down
27 changes: 26 additions & 1 deletion Bynder/Sdk/Query/Upload/SaveMediaQuery.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.Collections.Generic;
Expand Down Expand Up @@ -43,6 +43,31 @@ internal class SaveMediaQuery
[ApiField("tags", Converter = typeof(ListConverter))]
public IList<string> Tags { get; set; }


/// <summary>
/// Description of the media
/// </summary>
[ApiField("description")]
public string Description { get; set; }

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

/// <summary>
/// Copyright information for the media
/// </summary>
[ApiField("copyright")]
public string Copyright { get; set; }

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

/// <summary>
/// Metaproperty options to set on the asset.
/// </summary>
Expand Down
Loading