Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding assignLabelToScreenshots & unassignLabelToScreenshots apis #163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/com/crowdin/client/labels/LabelsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
import com.crowdin.client.labels.model.Label;
import com.crowdin.client.labels.model.LabelResponseList;
import com.crowdin.client.labels.model.LabelResponseObject;
import com.crowdin.client.labels.model.LabelToScreenshotsRequest;
import com.crowdin.client.labels.model.LabelToStringsRequest;
import com.crowdin.client.screenshots.model.Screenshot;
import com.crowdin.client.screenshots.model.ScreenshotResponseList;
import com.crowdin.client.sourcestrings.model.SourceString;
import com.crowdin.client.sourcestrings.model.SourceStringResponseList;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class LabelsApi extends CrowdinApi {

Expand Down Expand Up @@ -141,5 +145,35 @@ public ResponseList<SourceString> unassignLabelFromStrings(Long projectId, Long
return SourceStringResponseList.to(response);
}

/**
* @param projectId Project Identifier
* @param labelId Label Identifier
* @param request Request Object
* @return Screenshots
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.labels.screenshots.post" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.labels.screenshots.post" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<Screenshot> assignLabelToScreenshots(Long projectId, Long labelId, LabelToScreenshotsRequest request) throws HttpException, HttpBadRequestException{
String builtUrl = String.format("%s/projects/%d/labels/%d/screenshots", this.url, projectId, labelId);
ScreenshotResponseList response = this.httpClient.post(builtUrl, request, new HttpRequestConfig(), ScreenshotResponseList.class);
return ScreenshotResponseList.to(response);
}

/**
* @param projectId Project Identifier
* @param labelId Label Identifier
* @return Screenshots
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.labels.screenshots.deleteMany" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.labels.screenshots.deleteMany" target="_blank"><b> Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<Screenshot> unassignLabelFromScreenshots(Long projectId, Long labelId, List<Long> screenshotIds) throws HttpException, HttpBadRequestException {
String builtUrl = String.format("%s/projects/%d/labels/%d/screenshots", this.url, projectId, labelId);
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams("screenshotIds", Optional.ofNullable(screenshotIds == null ? null : screenshotIds.stream().map(String::valueOf).collect(Collectors.joining(","))));
ScreenshotResponseList response = this.httpClient.delete(builtUrl, new HttpRequestConfig(queryParams), ScreenshotResponseList.class);
return ScreenshotResponseList.to(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.crowdin.client.labels.model;

import lombok.Data;

import java.util.List;

@Data
public class LabelToScreenshotsRequest {

private List<Long> screenshotIds;
}
52 changes: 39 additions & 13 deletions src/test/java/com/crowdin/client/labels/LabelsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.crowdin.client.framework.TestClient;
import com.crowdin.client.labels.model.AddLabelRequest;
import com.crowdin.client.labels.model.Label;
import com.crowdin.client.labels.model.LabelToScreenshotsRequest;
import com.crowdin.client.labels.model.LabelToStringsRequest;
import com.crowdin.client.screenshots.model.Screenshot;
import com.crowdin.client.sourcestrings.model.SourceString;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -17,7 +19,9 @@
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -33,23 +37,30 @@ private String file(String fileName) {
private final Long projectId = 8L;
private final String labelTitle = "main";
private final Long labelId = 34L;
private final Map<String, Object> urlParams = new HashMap<String, Object>() {{
put("screenshotIds", "1");
}};

@Override
public List<RequestMock> getMocks() {
return Arrays.asList(
RequestMock.build(String.format("%s/projects/%d/labels", this.url, projectId), HttpGet.METHOD_NAME,
"api/labels/listLabels.json"),
RequestMock.build(String.format("%s/projects/%d/labels", this.url, projectId), HttpPost.METHOD_NAME,
"api/labels/addLabelRequest.json", "api/labels/label.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d", this.url, projectId, labelId), HttpGet.METHOD_NAME,
"api/labels/label.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d", this.url, projectId, labelId), HttpDelete.METHOD_NAME),
RequestMock.build(String.format("%s/projects/%d/labels/%d", this.url, projectId, labelId), HttpPatch.METHOD_NAME,
"api/labels/editLabelRequest.json", "api/labels/label.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d/strings", this.url, projectId, labelId), HttpPost.METHOD_NAME,
"api/labels/labelToStringsRequest.json", "api/labels/listStrings.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d/strings", this.url, projectId, labelId), HttpDelete.METHOD_NAME,
"api/labels/listStrings.json")
RequestMock.build(String.format("%s/projects/%d/labels", this.url, projectId), HttpGet.METHOD_NAME,
"api/labels/listLabels.json"),
RequestMock.build(String.format("%s/projects/%d/labels", this.url, projectId), HttpPost.METHOD_NAME,
"api/labels/addLabelRequest.json", "api/labels/label.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d", this.url, projectId, labelId), HttpGet.METHOD_NAME,
"api/labels/label.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d", this.url, projectId, labelId), HttpDelete.METHOD_NAME),
RequestMock.build(String.format("%s/projects/%d/labels/%d", this.url, projectId, labelId), HttpPatch.METHOD_NAME,
"api/labels/editLabelRequest.json", "api/labels/label.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d/strings", this.url, projectId, labelId), HttpPost.METHOD_NAME,
"api/labels/labelToStringsRequest.json", "api/labels/listStrings.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d/strings", this.url, projectId, labelId), HttpDelete.METHOD_NAME,
"api/labels/listStrings.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d/screenshots", this.url, projectId, labelId), HttpPost.METHOD_NAME,
"api/labels/labelToScreenshotsRequest.json", "api/labels/listScreenshots.json"),
RequestMock.build(String.format("%s/projects/%d/labels/%d/screenshots", this.url, projectId, labelId), HttpDelete.METHOD_NAME,
"api/labels/listScreenshots.json", urlParams)
);
}

Expand Down Expand Up @@ -111,5 +122,20 @@ public void unassignLabelToStringTest() {
assertEquals(1, response.getData().size());
}

@Test
public void assignLabelToScreenshot() {
LabelToScreenshotsRequest request = new LabelToScreenshotsRequest() {{
setScreenshotIds(Arrays.asList(1L));
}};
ResponseList<Screenshot> response = this.getLabelsApi().assignLabelToScreenshots(projectId, labelId, request);
assertEquals(1, response.getData().size());
}

@Test
public void unassignLabelToScreenshot() {
List<Long> screenshotIds= Arrays.asList(1L);
ResponseList<Screenshot> response = this.getLabelsApi().unassignLabelFromScreenshots(projectId, labelId, screenshotIds);
assertEquals(1, response.getData().size());

}
}
5 changes: 5 additions & 0 deletions src/test/resources/api/labels/labelToScreenshotsRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"screenshotIds": [
1
]
}
37 changes: 37 additions & 0 deletions src/test/resources/api/labels/listScreenshots.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"data": [
{
"data": {
"id": 1,
"userId": 6,
"url": "https://production-enterprise-screenshots.downloads.crowdin.com/992000002/6/2/middle.jpg?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIGJKLQV66ZXPMMEA%2F20190923%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190923T093016Z&X-Amz-SignedHeaders=host&X-Amz-Expires=120&X-Amz-Signature=8df06f57594f7d1804b7c037629f6916224415e9b935c4f6619fbe002fb25e73",
"name": "translate_with_siri.jpg",
"size": {
"width": 267,
"height": 176
},
"tagsCount": 0,
"tags": [
{
"id": 98,
"screenshotId": 2,
"stringId": 2822,
"position": {
"x": 474,
"y": 147,
"width": 490,
"height": 99
},
"createdAt": "2019-09-23T09:35:31+00:00"
}
],
"createdAt": "2019-09-23T09:29:19+00:00",
"updatedAt": "2019-09-23T09:29:19+00:00"
}
}
],
"pagination": {
"offset": 0,
"limit": 25
}
}