Skip to content

Commit

Permalink
API-748 - Add logic to be able to pass metaproperty options during sa…
Browse files Browse the repository at this point in the history
…ve & modify media (#70)

* Added ITypeToDictionaryConverter

* Added converter for metaproperty options

* Added "metaproperty" param to queries

* Version update to 2.2.10
  • Loading branch information
Huib Piguillet authored Dec 3, 2021
1 parent b772d7d commit d1480c6
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 47 deletions.
29 changes: 29 additions & 0 deletions Bynder/Sdk/Api/Converters/ITypeToDictionaryConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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;

namespace Bynder.Sdk.Api.Converters
{
/// <summary>
/// Interface for type converters used to decode specific
/// parameters to strings
/// </summary>
public interface ITypeToDictionaryConverter
{
/// <summary>
/// Checks if the converter can convert a specific type
/// </summary>
/// <param name="typeToConvert">Type to convert from</param>
/// <returns>true if it can convert the type</returns>
bool CanConvert(Type typeToConvert);

/// <summary>
/// Converts the value to string
/// </summary>
/// <param name="value">value to be converted</param>
/// <returns>converted string value</returns>
IDictionary<string, string> Convert(object value);
}
}
24 changes: 24 additions & 0 deletions Bynder/Sdk/Api/Converters/MetapropertyOptionsConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Bynder.Sdk.Api.Converters
{
public class MetapropertyOptionsConverter : ITypeToDictionaryConverter
{

public bool CanConvert(Type typeToConvert)
{
return typeof(IDictionary<string, IList<string>>).IsAssignableFrom(typeToConvert);
}

public IDictionary<string, string> Convert(object value)
{
return ((IDictionary<string, IList<string>>)value).ToDictionary(
item => item.Key,
item => string.Join(",", item.Value)
);
}

}
}
12 changes: 6 additions & 6 deletions Bynder/Sdk/Bynder.Sdk.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;net48</TargetFrameworks>
<AssemblyVersion>2.2.9.0</AssemblyVersion>
<FileVersion>2.2.9.0</FileVersion>
<AssemblyVersion>2.2.10.0</AssemblyVersion>
<FileVersion>2.2.10.0</FileVersion>
<Company>Bynder</Company>
<Product>Bynder.Sdk</Product>
<Copyright>Copyright © Bynder</Copyright>
<PackOnBuild>true</PackOnBuild>
<PackageVersion>2.2.9</PackageVersion>
<PackageVersion>2.2.10</PackageVersion>
<Authors>BynderDevops</Authors>
<Description>The main goal of this SDK is to speed up the integration of Bynder customers who use C# making it easier to connect to the Bynder API (http://docs.bynder.apiary.io/) and executing requests on it.</Description>
<PackageIcon>BynderLogo.png</PackageIcon>
Expand All @@ -17,7 +17,7 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Owners>BynderDevops</Owners>
<PackageProjectUrl>https://github.com/Bynder/bynder-c-sharp-sdk</PackageProjectUrl>
<PackageReleaseNotes>The UploadFile call now returns data about the created asset.</PackageReleaseNotes>
<PackageReleaseNotes>Added "transformBaseUrl" field to Media model. Added functionality to create and delete asset usage records. Added functionality to pass metaproperty options during save &amp; modify media.</PackageReleaseNotes>
<Summary>The main goal of this SDK is to speed up the integration of Bynder customers who use C# making it easier to connect to the Bynder API (http://docs.bynder.apiary.io/) and executing requests on it.</Summary>
<PackageTags>Bynder API C# SDK</PackageTags>
<Title>Bynder.Sdk</Title>
Expand All @@ -37,7 +37,7 @@
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<None Include="PackageResources\LICENSE" Pack="true" PackagePath=""/>
<None Include="PackageResources\BynderLogo.png" Pack="true" PackagePath=""/>
<None Include="PackageResources\LICENSE" Pack="true" PackagePath="" />
<None Include="PackageResources\BynderLogo.png" Pack="true" PackagePath="" />
</ItemGroup>
</Project>
20 changes: 18 additions & 2 deletions Bynder/Sdk/Query/Asset/ModifyMediaQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Bynder.Sdk.Api.Converters;
using Bynder.Sdk.Query.Decoder;

namespace Bynder.Sdk.Query.Asset
Expand Down Expand Up @@ -52,6 +52,22 @@ public ModifyMediaQuery(string mediaId)
/// </summary>
[ApiField("isPublic")]
public bool IsPublic { get; set; }

/// <summary>
/// Metaproperty options to set on the asset.
/// </summary>
[ApiField("metaproperty", Converter = typeof(MetapropertyOptionsConverter))]
public IDictionary<string, IList<string>> MetapropertyOptions { get; set; }

/// <summary>
/// Add a set of options to a metaproperty
/// </summary>
/// <param name="metapropertyId">metaproperty ID</param>
/// <param name="optionIds">set of options</param>
public void AddMetapropertyOptions(string metapropertyId, IList<string> optionIds)
{
MetapropertyOptions.Add(metapropertyId, optionIds);
}
}
}

49 changes: 27 additions & 22 deletions Bynder/Sdk/Query/Decoder/QueryDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,33 @@ public IDictionary<string, string> GetParameters(object query)
/// <param name="propertyInfo">property type information</param>
/// <param name="query">query object</param>
/// <param name="collection">collection to add the converted values</param>
private void ConvertProperty(PropertyInfo propertyInfo, object query, IDictionary<string, string> collection)
private void ConvertProperty(PropertyInfo propertyInfo, object query, IDictionary<string, string> parameters)
{
var attributes = propertyInfo.GetCustomAttributes(true);
foreach (var attribute in attributes)
foreach (var attribute in propertyInfo.GetCustomAttributes(true))
{
if (attribute is ApiField nameAttr)
if (attribute is ApiField apiField)
{
object value = propertyInfo.GetValue(query);
if (value != null)
if (value == null)
{
var convertedValue = ConvertPropertyValue(nameAttr, propertyInfo.PropertyType, value);
if (!string.IsNullOrEmpty(convertedValue))
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))
{
collection.Add(nameAttr.ApiName, convertedValue);
AddParam(parameters, $"{apiField.ApiName}.{item.Key}", item.Value);
}
}

Expand All @@ -64,21 +77,13 @@ private void ConvertProperty(PropertyInfo propertyInfo, object query, IDictionar
}
}

/// <summary>
/// Function called to convert property values to string. If no converter is
/// specified, then .ToString is called.
/// </summary>
/// <param name="apiField">API field attribute</param>
/// <param name="propertyType">property type information</param>
/// <param name="value">current value</param>
/// <returns>converted value</returns>
private string ConvertPropertyValue(ApiField apiField, Type propertyType, object value)
private void AddParam(IDictionary<string, string> parameters, string key, string value)
{
return apiField.Converter != null
&& Activator.CreateInstance(apiField.Converter) is ITypeToStringConverter converter
&& converter.CanConvert(propertyType)
? converter.Convert(value)
: value.ToString();
if (!string.IsNullOrEmpty(value))
{
parameters.Add(key, value);
}
}

}
}
17 changes: 17 additions & 0 deletions Bynder/Sdk/Query/Upload/SaveMediaQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,22 @@ internal class SaveMediaQuery
/// </summary>
[ApiField("tags", Converter = typeof(ListConverter))]
public IList<string> Tags { get; set; }

/// <summary>
/// Metaproperty options to set on the asset.
/// </summary>
[ApiField("metaproperty", Converter = typeof(MetapropertyOptionsConverter))]
public IDictionary<string, IList<string>> MetapropertyOptions { get; set; } = new Dictionary<string, IList<string>>();

/// <summary>
/// Add a set of options to a metaproperty
/// </summary>
/// <param name="metapropertyId">metaproperty ID</param>
/// <param name="optionIds">set of options</param>
public void AddMetapropertyOptions(string metapropertyId, IList<string> optionIds)
{
MetapropertyOptions.Add(metapropertyId, optionIds);
}

}
}
53 changes: 53 additions & 0 deletions Bynder/Test/Api/Converters/MetapropertyOptionsConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using Bynder.Sdk.Api.Converters;
using Xunit;

namespace Bynder.Test.Api.Converters
{
public class MetapropertyOptionsConverterTest
{
[Fact]
public void CanConvertOnlyWhenTypeIsDateTimeOffset()
{
var converter = new MetapropertyOptionsConverter();
Assert.False(converter.CanConvert(typeof(int)));
Assert.False(converter.CanConvert(typeof(string)));
Assert.False(converter.CanConvert(typeof(bool)));
Assert.True(converter.CanConvert(typeof(IDictionary<string, IList<string>>)));
}

[Fact]
public void ConvertReturnsStringWithDate()
{
const string metaprop1 = "metaprop1";
const string metaprop1option1 = "metaprop1option1";
const string metaprop1option2 = "metaprop1option2";
const string metaprop1option3 = "metaprop1option3";

const string metaprop2 = "metaprop2";
const string metaprop2option1 = "metaprop2option1";
const string metaprop2option2 = "metaprop2option2";
const string metaprop2option3 = "metaprop2option3";

const string metaprop3 = "metaprop3";
const string metaprop3option1 = "metaprop3option1";
const string metaprop3option2 = "metaprop3option2";
const string metaprop3option3 = "metaprop3option3";

var converter = new MetapropertyOptionsConverter();
var converted = converter.Convert(new Dictionary<string, IList<string>>
{
{ metaprop1, new List<string> { metaprop1option1, metaprop1option2, metaprop1option3 } },
{ metaprop2, new List<string> { metaprop2option1, metaprop2option2, metaprop2option3 } },
{ metaprop3, new List<string> { metaprop3option1, metaprop3option2, metaprop3option3 } }
});
var expected = new Dictionary<string, string>
{
{ metaprop1, $"{metaprop1option1},{metaprop1option2},{metaprop1option3}" },
{ metaprop2, $"{metaprop2option1},{metaprop2option2},{metaprop2option3}" },
{ metaprop3, $"{metaprop3option1},{metaprop3option2},{metaprop3option3}" }
};
Assert.Equal(expected, converted);
}
}
}
Loading

0 comments on commit d1480c6

Please sign in to comment.