-
Notifications
You must be signed in to change notification settings - Fork 24
/
BundlesApiExecutor.cs
198 lines (171 loc) · 8.97 KB
/
BundlesApiExecutor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using Crowdin.Api.Core;
using Crowdin.Api.SourceFiles;
using JetBrains.Annotations;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace Crowdin.Api.Bundles
{
public class BundlesApiExecutor
{
private readonly ICrowdinApiClient _apiClient;
private readonly IJsonParser _jsonParser;
public BundlesApiExecutor(ICrowdinApiClient apiClient)
{
_apiClient = apiClient;
_jsonParser = apiClient.DefaultJsonParser;
}
public BundlesApiExecutor(ICrowdinApiClient apiClient, IJsonParser jsonParser)
{
_apiClient = apiClient;
_jsonParser = jsonParser;
}
/// <summary>
/// List bundles. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.getMany">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.bundles.getMany">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.getMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<ResponseList<Bundle>> ListBundles(int projectId, int limit = 25, int offset = 0)
{
string url = FormUrl_Bundles(projectId);
IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);
CrowdinApiResult result = await _apiClient.SendGetRequest(url, queryParams);
return _jsonParser.ParseResponseList<Bundle>(result.JsonObject);
}
/// <summary>
/// List bundle branches. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.bundles.branches.getMany">Crowdin String Based API</a>
/// </summary>
[PublicAPI]
public async Task<ResponseList<Branch>> ListBundleBranches(int projectId, int bundleId, int limit = 25, int offset = 0)
{
string url = FormUrl_BundleId(projectId, bundleId) + "/branches";
IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);
CrowdinApiResult result = await _apiClient.SendGetRequest(url, queryParams);
return _jsonParser.ParseResponseList<Branch>(result.JsonObject);
}
/// <summary>
/// Add bundle. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.post">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.post">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Bundle> AddBundle(int projectId, AddBundleRequest request)
{
string url = FormUrl_Bundles(projectId);
CrowdinApiResult result = await _apiClient.SendPostRequest(url, request);
return _jsonParser.ParseResponseObject<Bundle>(result.JsonObject);
}
/// <summary>
/// Get bundle. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.get">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Bundle> GetBundle(int projectId, int bundleId)
{
string url = FormUrl_BundleId(projectId, bundleId);
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<Bundle>(result.JsonObject);
}
/// <summary>
/// Delete bundle. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.delete">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.delete">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task DeleteBundle(int projectId, int bundleId)
{
string url = FormUrl_BundleId(projectId, bundleId);
HttpStatusCode statusCode = await _apiClient.SendDeleteRequest(url);
Utils.ThrowIfStatusNot204(statusCode, $"Bundle {bundleId} removal failed");
}
/// <summary>
/// Edit bundle. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.patch">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.patch">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Bundle> EditBundle(int projectId, int bundleId, IEnumerable<BundlePatch> patches)
{
string url = FormUrl_BundleId(projectId, bundleId);
CrowdinApiResult result = await _apiClient.SendPatchRequest(url, patches);
return _jsonParser.ParseResponseObject<Bundle>(result.JsonObject);
}
/// <summary>
/// Download Bundle. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.exports.download.get">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.exports.download.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<DownloadLink> DownloadBundle(int projectId, int bundleId, string exportId)
{
string url = FormUrl_BundleExportId(projectId, bundleId, exportId) + "/download";
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<DownloadLink>(result.JsonObject);
}
/// <summary>
/// Export Bundle. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.exports.post">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.exports.post">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<BundleExport> ExportBundle(int projectId, int bundleId)
{
string url = FormUrl_BundleExports(projectId, bundleId);
CrowdinApiResult result = await _apiClient.SendPostRequest(url);
return _jsonParser.ParseResponseObject<BundleExport>(result.JsonObject);
}
/// <summary>
/// Check Bundle Export Status. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.exports.get">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.exports.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<BundleExport> CheckBundleExportStatus(int projectId, int bundleId, string exportId)
{
string url = FormUrl_BundleExportId(projectId, bundleId, exportId);
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<BundleExport>(result.JsonObject);
}
/// <summary>
/// Bundle list files. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#operation/api.projects.bundles.files.getMany">Crowdin API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.bundles.files.getMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<ResponseList<T>> BundleListFiles<T>(
int projectId, int bundleId, int limit = 25, int offset = 0)
where T : FileResourceBase
{
string url = FormUrl_BundleFiles(projectId, bundleId);
IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);
CrowdinApiResult result = await _apiClient.SendGetRequest(url, queryParams);
return _jsonParser.ParseResponseList<T>(result.JsonObject);
}
#region Helper methods
private static string FormUrl_Bundles(int projectId)
{
return $"/projects/{projectId}/bundles";
}
private static string FormUrl_BundleId(int projectId, int bundleId)
{
return $"/projects/{projectId}/bundles/{bundleId}";
}
private static string FormUrl_BundleFiles(int projectId, int bundleId)
{
return $"/projects/{projectId}/bundles/{bundleId}/files";
}
private static string FormUrl_BundleExports(int projectId, int bundleId)
{
return $"/projects/{projectId}/bundles/{bundleId}/exports";
}
private static string FormUrl_BundleExportId(int projectId, int bundleId, string exportId)
{
return $"/projects/{projectId}/bundles/{bundleId}/exports/{exportId}";
}
#endregion
}
}