-
Notifications
You must be signed in to change notification settings - Fork 27
/
ScreenshotsApiExecutor.cs
258 lines (224 loc) · 11.9 KB
/
ScreenshotsApiExecutor.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Crowdin.Api.Core;
using JetBrains.Annotations;
namespace Crowdin.Api.Screenshots
{
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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.getMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<ResponseList<Screenshot>> ListScreenshots(int projectId, int limit = 25, int offset = 0)
{
string url = FormUrl_Screenshots(projectId);
IDictionary<string, string> queryParams = Utils.CreateQueryParamsFromPaging(limit, offset);
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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.post">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Screenshot> AddScreenshot(int projectId, AddScreenshotRequest request)
{
string url = FormUrl_Screenshots(projectId);
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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Screenshot> GetScreenshot(int projectId, int screenshotId)
{
string url = FormUrl_ScreenshotId(projectId, 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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.put">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Screenshot> UpdateScreenshot(int projectId, int screenshotId, UpdateScreenshotRequest request)
{
string url = FormUrl_ScreenshotId(projectId, 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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.delete">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task DeleteScreenshot(int projectId, int screenshotId)
{
string url = FormUrl_ScreenshotId(projectId, 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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.patch">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Screenshot> EditScreenshot(int projectId, int screenshotId, IEnumerable<ScreenshotPatch> patches)
{
string url = FormUrl_ScreenshotId(projectId, screenshotId);
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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.tags.getMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<ResponseList<Tag>> ListTags(int projectId, int screenshotId, int limit = 25, int offset = 0)
{
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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.tags.putMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task ReplaceTags(int projectId, int screenshotId, IEnumerable<AddTagRequest> request)
{
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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.tags.putMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task ReplaceTags(int projectId, int screenshotId, AutoTagReplaceTagsRequest request)
{
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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.tags.post">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<ResponseList<Tag>> AddTag(int projectId, int screenshotId, IEnumerable<AddTagRequest> request)
{
string url = FormUrl_ScreenshotTags(projectId, screenshotId);
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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.tags.deleteMany">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task ClearTags(int projectId, int screenshotId)
{
string url = FormUrl_ScreenshotTags(projectId, 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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.tags.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Tag> GetTag(int projectId, int screenshotId, int tagId)
{
string url = FormUrl_ScreenshotTagId(projectId, screenshotId, 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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.tags.delete">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task DeleteTag(int projectId, int screenshotId, int tagId)
{
string url = FormUrl_ScreenshotTagId(projectId, screenshotId, 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>
/// <a href="https://support.crowdin.com/enterprise/api/#operation/api.projects.screenshots.tags.patch">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<Screenshot> EditTag(int projectId, int screenshotId, int tagId, IEnumerable<TagPatch> patches)
{
string url = FormUrl_ScreenshotTagId(projectId, screenshotId, 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
}
}