Skip to content

Commit

Permalink
feat(screenshots): add the stringIds parameter support (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilKot authored Oct 16, 2024
1 parent a52b611 commit 98f92b8
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 44 deletions.
55 changes: 46 additions & 9 deletions src/Crowdin.Api/Core/InternalExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -26,7 +26,7 @@ internal static string ToDescriptionString(this Enum? path)
.GetType()
.GetField(path.ToString())
.GetCustomAttribute<DescriptionAttribute>(false);

return attribute != null ? attribute.Description : string.Empty;
}

Expand All @@ -42,15 +42,15 @@ internal static void AddParamIfPresent(this IDictionary<string, string> queryPar
queryParams.AddParamIfPresent(key, value.ToString());
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, long? value)
{
if (value.HasValue)
{
queryParams.AddParamIfPresent(key, value.ToString());
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, bool? value)
{
if (value.HasValue)
Expand All @@ -75,6 +75,46 @@ internal static void AddParamIfPresent(this IDictionary<string, string> queryPar
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, IEnumerable<int>? values)
{
if (values != null && values.Any())
{
queryParams.Add(key, string.Join(",", values.Select(value => value.ToString())));
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, IEnumerable<long>? values)
{
if (values != null && values.Any())
{
queryParams.Add(key, string.Join(",", values.Select(value => value.ToString())));
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, IEnumerable<bool>? values)
{
if (values != null && values.Any())
{
queryParams.Add(key, string.Join(",", values.Select(value => value.ToString().ToLower())));
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, IEnumerable<object>? values)
{
if (values != null && values.Any())
{
queryParams.Add(key, string.Join(",", values.Select(value => value.ToString())));
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, IEnumerable<string>? values)
{
if (values != null && values.Any())
{
queryParams.Add(key, string.Join(",", values));
}
}

internal static void AddDescriptionEnumValueIfPresent<TEnum>(
this IDictionary<string, string> queryParams, string key, TEnum? enumMember)
where TEnum : struct, Enum
Expand All @@ -84,15 +124,12 @@ internal static void AddDescriptionEnumValueIfPresent<TEnum>(
queryParams.Add(key, enumMember.Value.ToDescriptionString());
}
}

internal static void AddSortingRulesIfPresent(
this IDictionary<string, string> queryParams,
IEnumerable<SortingRule>? sortingRules)
{
if (sortingRules != null && sortingRules.Any())
{
queryParams.Add("orderBy", string.Join(",", sortingRules.Select(rule => rule.ToString())));
}
AddParamIfPresent(queryParams, "orderBy", sortingRules);
}
}
}
18 changes: 10 additions & 8 deletions src/Crowdin.Api/Screenshots/ScreenshotsApiExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
Expand Down Expand Up @@ -28,7 +28,7 @@ public ScreenshotsApiExecutor(ICrowdinApiClient apiClient, IJsonParser jsonParse
}

#region Screenshots

/// <summary>
/// List screenshots. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.getMany">Crowdin API</a>
Expand All @@ -39,12 +39,14 @@ public async Task<ResponseList<Screenshot>> ListScreenshots(
int projectId,
int limit = 25,
int offset = 0,
IEnumerable<SortingRule>? orderBy = null)
IEnumerable<SortingRule>? orderBy = null,
IEnumerable<int>? stringIds = null)
{
string url = FormUrl_Screenshots(projectId);
IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);
queryParams.AddSortingRulesIfPresent(orderBy);

queryParams.AddParamIfPresent("stringIds", stringIds);

CrowdinApiResult result = await _apiClient.SendGetRequest(url, queryParams);
return _jsonParser.ParseResponseList<Screenshot>(result.JsonObject);
}
Expand Down Expand Up @@ -113,7 +115,7 @@ public async Task<Screenshot> EditScreenshot(int projectId, int screenshotId, IE
CrowdinApiResult result = await _apiClient.SendPatchRequest(url, patches);
return _jsonParser.ParseResponseObject<Screenshot>(result.JsonObject);
}

#region Helper methods

private static string FormUrl_Screenshots(int projectId)
Expand Down Expand Up @@ -142,11 +144,11 @@ public async Task<ResponseList<Tag>> ListTags(int projectId, int screenshotId, i
{
string url = FormUrl_ScreenshotId(projectId, screenshotId);
IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);

CrowdinApiResult result = await _apiClient.SendGetRequest(url, queryParams);
return _jsonParser.ParseResponseList<Tag>(result.JsonObject);
}

/// <summary>
/// Replace tags. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.tags.putMany">Crowdin API</a>
Expand All @@ -163,7 +165,7 @@ public async Task ReplaceTags(int projectId, int screenshotId, IEnumerable<AddTa
throw new CrowdinApiException($"Failed to replace tags of screenshot {screenshotId}");
}
}

/// <summary>
/// Replace tags. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.tags.putMany">Crowdin API</a>
Expand Down
202 changes: 202 additions & 0 deletions tests/Crowdin.Api.Tests/Core/InternalExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@

using System.Collections.Generic;

using Crowdin.Api.Core;

using Moq;
using Xunit;

namespace Crowdin.Api.Tests.Core
{
public class InternalExtensionsTests
{
[Fact]
public void ToQueryString()
{
var queryParams = new Dictionary<string, string>
{
{ "limit", "25" },
{ "offset", "0" }
};

var queryString = queryParams.ToQueryString();

Assert.Equal("limit=25&offset=0", queryString);
}

[Fact]
public void AddParamIfPresentInt()
{
var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", 1);
queryParams.AddParamIfPresent("other", (int?) null);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("1", value);
}

[Fact]
public void AddParamIfPresentLong()
{
var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", 1L);
queryParams.AddParamIfPresent("other", (long?) null);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("1", value);
}

[Fact]
public void AddParamIfPresentBool()
{
var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", true);
queryParams.AddParamIfPresent("other", (bool?) null);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("true", value);
}

[Fact]
public void AddParamIfPresentObject()
{
Mock<object> mockObject = new Mock<object>();
mockObject.Setup(o => o.ToString()).Returns("test");

var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", mockObject.Object);
queryParams.AddParamIfPresent("other", null as object);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("test", value);
}

[Fact]
public void AddParamIfPresentString()
{
var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", "test");
queryParams.AddParamIfPresent("other1", null as string);
queryParams.AddParamIfPresent("other2", "");
queryParams.AddParamIfPresent("other3", " ");

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("test", value);
}

[Fact]
public void AddParamListIfPresentInt()
{
var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", new int[] { 1, 2, 3 });
queryParams.AddParamIfPresent("other1", new int[] { });
queryParams.AddParamIfPresent("other2", null as int[]);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("1,2,3", value);
}

[Fact]
public void AddParamListIfPresentLong()
{
var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", new long[] { 1, 2, 3 });
queryParams.AddParamIfPresent("other1", new long[] { });
queryParams.AddParamIfPresent("other2", null as long[]);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("1,2,3", value);
}

[Fact]
public void AddParamListIfPresentBool()
{
var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", new bool[] { true, false, true });
queryParams.AddParamIfPresent("other1", new bool[] { });
queryParams.AddParamIfPresent("other2", null as bool[]);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("true,false,true", value);
}

[Fact]
public void AddParamListIfPresentObject()
{
Mock<object> mockObject1 = new Mock<object>();
Mock<object> mockObject2 = new Mock<object>();
Mock<object> mockObject3 = new Mock<object>();

mockObject1.Setup(o => o.ToString()).Returns("test1");
mockObject2.Setup(o => o.ToString()).Returns("test2");
mockObject3.Setup(o => o.ToString()).Returns("test3");

var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", new object[] {
mockObject1.Object,
mockObject2.Object,
mockObject3.Object
});
queryParams.AddParamIfPresent("other1", new object[] {});
queryParams.AddParamIfPresent("other2", null as object[]);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("test1,test2,test3", value);
}

[Fact]
public void AddParamListIfPresentString()
{
var queryParams = new Dictionary<string, string>();

queryParams.AddParamIfPresent("param", new string[] {"test1", "test2", "test3"});
queryParams.AddParamIfPresent("other1", new string[] {});
queryParams.AddParamIfPresent("other2", null as string[]);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("param", out string? value));
Assert.Equal("test1,test2,test3", value);
}

[Fact]
public void AddSortingRulesIfPresent()
{
var sortingRules1 = new SortingRule[]
{
new SortingRule() { Field = "createdAt", Order = SortingOrder.Descending },
new SortingRule() { Field = "name", Order = SortingOrder.Ascending },
};

var sortingRules2 = new SortingRule[] { };
var sortingRules3 = null as SortingRule[];

var queryParams = new Dictionary<string, string>();

queryParams.AddSortingRulesIfPresent(sortingRules1);
queryParams.AddSortingRulesIfPresent(sortingRules2);
queryParams.AddSortingRulesIfPresent(sortingRules3);

Assert.Single(queryParams.Keys);
Assert.True(queryParams.TryGetValue("orderBy", out string? value));
Assert.Equal("createdAt desc,name asc", value);
}
}
}
18 changes: 17 additions & 1 deletion tests/Crowdin.Api.Tests/Core/Resources/Screenshots.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 98f92b8

Please sign in to comment.