Skip to content

Commit db34625

Browse files
feat(sourceFiles): add support for new File References API (#350)
1 parent 2bd23cf commit db34625

File tree

7 files changed

+655
-38
lines changed

7 files changed

+655
-38
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using JetBrains.Annotations;
2+
using Newtonsoft.Json;
3+
4+
namespace Crowdin.Api.SourceFiles
5+
{
6+
[PublicAPI]
7+
public class AddAssetReferenceRequest
8+
{
9+
[JsonProperty("storageId")]
10+
public long StorageId { get; set; }
11+
12+
[JsonProperty("name")]
13+
public string Name { get; set; }
14+
}
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Crowdin.Api.Users;
2+
using JetBrains.Annotations;
3+
using Newtonsoft.Json;
4+
using System;
5+
6+
namespace Crowdin.Api.SourceFiles
7+
{
8+
[PublicAPI]
9+
public class AssetReference
10+
{
11+
[JsonProperty("id")]
12+
public int Id { get; set; }
13+
14+
[JsonProperty("name")]
15+
public string Name { get; set; }
16+
17+
[JsonProperty("url")]
18+
public string Url { get; set; }
19+
20+
[JsonProperty("user")]
21+
public User User { get; set; }
22+
23+
[JsonProperty("createdAt")]
24+
public DateTimeOffset CreatedAt { get; set; }
25+
26+
[JsonProperty("mimeType")]
27+
public string MimeType { get; set; }
28+
}
29+
}

src/Crowdin.Api/SourceFiles/ISourceFilesApiExecutor.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,21 @@ Task<ResponseList<ReviewedStringBuild>> ListReviewedSourceFilesBuilds(
110110
Task<DownloadLink> DownloadReviewedSourceFiles(long projectId, long buildId);
111111

112112
#endregion
113+
114+
#region Asset References
115+
116+
Task<ResponseList<AssetReference>> ListAssetReferences(
117+
long projectId,
118+
long fileId,
119+
int limit = 25,
120+
int offset = 0);
121+
122+
Task<AssetReference> AddAssetReference(long projectId, long fileId, AddAssetReferenceRequest request);
123+
124+
Task<AssetReference> GetAssetReference(long projectId, long fileId, long referenceId);
125+
126+
Task DeleteAssetReference(long projectId, long fileId, long referenceId);
127+
128+
#endregion
113129
}
114130
}

src/Crowdin.Api/SourceFiles/SourceFilesApiExecutor.cs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -467,5 +467,76 @@ public async Task<DownloadLink> DownloadReviewedSourceFiles(long projectId, long
467467
}
468468

469469
#endregion
470+
471+
#region File References
472+
473+
/// <summary>
474+
/// List asset references. Documentation:
475+
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.files.references.getMany">Crowdin API</a>
476+
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.files.references.getMany">Crowdin Enterprise API</a>
477+
/// </summary>
478+
[PublicAPI]
479+
public async Task<ResponseList<AssetReference>> ListAssetReferences(long projectId, long fileId, int limit = 25, int offset = 0)
480+
{
481+
string url = FormUrl_FileReferences(projectId, fileId);
482+
IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);
483+
CrowdinApiResult result = await _apiClient.SendGetRequest(url, queryParams);
484+
return _jsonParser.ParseResponseList<AssetReference>(result.JsonObject);
485+
}
486+
487+
/// <summary>
488+
/// Add asset reference. Documentation:
489+
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.files.references.post">Crowdin API</a>
490+
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.files.references.post">Crowdin Enterprise API</a>
491+
/// </summary>
492+
[PublicAPI]
493+
public async Task<AssetReference> AddAssetReference(long projectId, long fileId, AddAssetReferenceRequest request)
494+
{
495+
string url = FormUrl_FileReferences(projectId, fileId);
496+
CrowdinApiResult result = await _apiClient.SendPostRequest(url, request);
497+
return _jsonParser.ParseResponseObject<AssetReference>(result.JsonObject);
498+
}
499+
500+
/// <summary>
501+
/// Get asset reference. Documentation:
502+
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.files.references.get">Crowdin API</a>
503+
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.files.references.get">Crowdin Enterprise API</a>
504+
/// </summary>
505+
[PublicAPI]
506+
public async Task<AssetReference> GetAssetReference(long projectId, long fileId, long referenceId)
507+
{
508+
string url = FormUrl_FileReferences_ReferenceId(projectId, fileId, referenceId);
509+
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
510+
return _jsonParser.ParseResponseObject<AssetReference>(result.JsonObject);
511+
}
512+
513+
/// <summary>
514+
/// Delete asset reference. Documentation:
515+
/// <a href="https://support.crowdin.com/api/v2/#operation/api.projects.files.references.delete">Crowdin API</a>
516+
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.files.references.delete">Crowdin Enterprise API</a>
517+
/// </summary>
518+
[PublicAPI]
519+
public async Task DeleteAssetReference(long projectId, long fileId, long referenceId)
520+
{
521+
string url = FormUrl_FileReferences_ReferenceId(projectId, fileId, referenceId);
522+
HttpStatusCode statusCode = await _apiClient.SendDeleteRequest(url);
523+
Utils.ThrowIfStatusNot204(statusCode, $"File reference {referenceId} removal failed");
524+
}
525+
526+
#region Helper methods
527+
528+
private static string FormUrl_FileReferences(long projectId, long fileId)
529+
{
530+
return $"/projects/{projectId}/files/{fileId}/references";
531+
}
532+
533+
private static string FormUrl_FileReferences_ReferenceId(long projectId, long fileId, long referenceId)
534+
{
535+
return $"/projects/{projectId}/files/{fileId}/references/{referenceId}";
536+
}
537+
538+
#endregion
539+
540+
#endregion
470541
}
471542
}

0 commit comments

Comments
 (0)