Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a.mikava committed Oct 15, 2024
1 parent 67238b3 commit bfd36ec
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 51 deletions.
31 changes: 16 additions & 15 deletions src/Crowdin.Api/Core/InternalExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -18,102 +19,102 @@ internal static async Task<JObject> ParseJsonBodyAsync(this HttpContent content)
string responseJson = await content.ReadAsStringAsync();
return JObject.Parse(responseJson);
}

internal static string ToDescriptionString(this Enum? path)
{
var attribute = path?
.GetType()
.GetField(path.ToString())
.GetCustomAttribute<DescriptionAttribute>(false);

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

internal static string ToQueryString(this IDictionary<string, string> queryParams)
{
return string.Join("&", queryParams.Select(kvPair => $"{kvPair.Key}={kvPair.Value}"));
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, int? value)
{
if (value.HasValue)
{
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)
{
queryParams.AddParamIfPresent(key, value.ToString().ToLower());
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, object? value)
{
if (value != null)
{
queryParams.AddParamIfPresent(key, value.ToString());
}
}

internal static void AddParamIfPresent(this IDictionary<string, string> queryParams, string key, string? value)
{
if (!string.IsNullOrWhiteSpace(value))
{
queryParams.Add(key, value!);
}
}

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 @@ -123,7 +124,7 @@ internal static void AddDescriptionEnumValueIfPresent<TEnum>(
queryParams.Add(key, enumMember.Value.ToDescriptionString());
}
}

internal static void AddSortingRulesIfPresent(
this IDictionary<string, string> queryParams,
IEnumerable<SortingRule>? sortingRules)
Expand Down
65 changes: 33 additions & 32 deletions src/Crowdin.Api/Screenshots/ScreenshotsApiExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
Expand All @@ -13,21 +14,21 @@ public class ScreenshotsApiExecutor
{
private readonly ICrowdinApiClient _apiClient;
private readonly IJsonParser _jsonParser;

public ScreenshotsApiExecutor(ICrowdinApiClient apiClient)
{
_apiClient = apiClient;
_jsonParser = apiClient.DefaultJsonParser;
}

public ScreenshotsApiExecutor(ICrowdinApiClient apiClient, IJsonParser jsonParser)
{
_apiClient = apiClient;
_jsonParser = jsonParser;
}

#region Screenshots

/// <summary>
/// List screenshots. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.getMany">Crowdin API</a>
Expand All @@ -45,11 +46,11 @@ public async Task<ResponseList<Screenshot>> ListScreenshots(
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);
}

/// <summary>
/// Add screenshot. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.post">Crowdin API</a>
Expand All @@ -62,7 +63,7 @@ public async Task<Screenshot> AddScreenshot(int projectId, AddScreenshotRequest
CrowdinApiResult result = await _apiClient.SendPostRequest(url, request);
return _jsonParser.ParseResponseObject<Screenshot>(result.JsonObject);
}

/// <summary>
/// Get screenshot. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.get">Crowdin API</a>
Expand All @@ -75,7 +76,7 @@ public async Task<Screenshot> GetScreenshot(int projectId, int screenshotId)
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<Screenshot>(result.JsonObject);
}

/// <summary>
/// Update screenshot. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.put">Crowdin API</a>
Expand All @@ -88,7 +89,7 @@ public async Task<Screenshot> UpdateScreenshot(int projectId, int screenshotId,
CrowdinApiResult result = await _apiClient.SendPutRequest(url, request);
return _jsonParser.ParseResponseObject<Screenshot>(result.JsonObject);
}

/// <summary>
/// Delete screenshot. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.delete">Crowdin API</a>
Expand All @@ -101,7 +102,7 @@ public async Task DeleteScreenshot(int projectId, int screenshotId)
HttpStatusCode statusCode = await _apiClient.SendDeleteRequest(url);
Utils.ThrowIfStatusNot204(statusCode, $"Screenshot {screenshotId} removal failed");
}

/// <summary>
/// Edit screenshot. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.patch">Crowdin API</a>
Expand All @@ -114,25 +115,25 @@ 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)
{
return $"/projects/{projectId}/screenshots";
}

private static string FormUrl_ScreenshotId(int projectId, int screenshotId)
{
return $"/projects/{projectId}/screenshots/{screenshotId}";
}

#endregion

#endregion

#region Tags

/// <summary>
/// List tags. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.tags.getMany">Crowdin API</a>
Expand All @@ -143,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 @@ -158,13 +159,13 @@ public async Task ReplaceTags(int projectId, int screenshotId, IEnumerable<AddTa
{
string url = FormUrl_ScreenshotTags(projectId, screenshotId);
CrowdinApiResult result = await _apiClient.SendPutRequest(url, request);

if (result.StatusCode != HttpStatusCode.OK)
{
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 All @@ -175,13 +176,13 @@ public async Task ReplaceTags(int projectId, int screenshotId, AutoTagReplaceTag
{
string url = FormUrl_ScreenshotTags(projectId, screenshotId);
CrowdinApiResult result = await _apiClient.SendPutRequest(url, request);

if (result.StatusCode != HttpStatusCode.OK)
{
throw new CrowdinApiException($"Failed to set AutoTag flag for screenshot {screenshotId}");
}
}

/// <summary>
/// Add tag. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.tags.post">Crowdin API</a>
Expand All @@ -194,7 +195,7 @@ public async Task<ResponseList<Tag>> AddTag(int projectId, int screenshotId, IEn
CrowdinApiResult result = await _apiClient.SendPostRequest(url, request);
return _jsonParser.ParseResponseList<Tag>(result.JsonObject);
}

/// <summary>
/// Clear tags. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.tags.deleteMany">Crowdin API</a>
Expand All @@ -207,7 +208,7 @@ public async Task ClearTags(int projectId, int screenshotId)
HttpStatusCode statusCode = await _apiClient.SendDeleteRequest(url);
Utils.ThrowIfStatusNot204(statusCode, $"Tags cleanup failed");
}

/// <summary>
/// Get tag. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.tags.get">Crowdin API</a>
Expand All @@ -220,7 +221,7 @@ public async Task<Tag> GetTag(int projectId, int screenshotId, int tagId)
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<Tag>(result.JsonObject);
}

/// <summary>
/// Delete tag. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.tags.delete">Crowdin API</a>
Expand All @@ -233,7 +234,7 @@ public async Task DeleteTag(int projectId, int screenshotId, int tagId)
HttpStatusCode statusCode = await _apiClient.SendDeleteRequest(url);
Utils.ThrowIfStatusNot204(statusCode, $"Tag {tagId} removal failed");
}

/// <summary>
/// Edit tag. Documentation:
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.screenshots.tags.patch">Crowdin API</a>
Expand All @@ -246,21 +247,21 @@ public async Task<Screenshot> EditTag(int projectId, int screenshotId, int tagId
CrowdinApiResult result = await _apiClient.SendPatchRequest(url, patches);
return _jsonParser.ParseResponseObject<Screenshot>(result.JsonObject);
}

#region Helper methods

private static string FormUrl_ScreenshotTags(int projectId, int screenshotId)
{
return FormUrl_ScreenshotId(projectId, screenshotId) + "/tags";
}

private static string FormUrl_ScreenshotTagId(int projectId, int screenshotId, int tagId)
{
return FormUrl_ScreenshotId(projectId, screenshotId) + $"/tags/{tagId}";
}

#endregion

#endregion
}
}
Loading

0 comments on commit bfd36ec

Please sign in to comment.