Skip to content

Commit 5f4a4d1

Browse files
authored
Merge pull request #329 from crowdin/string-corrections-api
feat: string corrections api
2 parents 9ac47c0 + 0899fb1 commit 5f4a4d1

File tree

12 files changed

+344
-0
lines changed

12 files changed

+344
-0
lines changed

src/main/java/com/crowdin/client/Client.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.crowdin.client.sourcestrings.SourceStringsApi;
2626
import com.crowdin.client.storage.StorageApi;
2727
import com.crowdin.client.stringcomments.StringCommentsApi;
28+
import com.crowdin.client.stringcorrections.StringCorrectionsApi;
2829
import com.crowdin.client.stringtranslations.StringTranslationsApi;
2930
import com.crowdin.client.tasks.TasksApi;
3031
import com.crowdin.client.teams.TeamsApi;
@@ -74,6 +75,7 @@ public class Client extends CrowdinApi {
7475
private final ClientsApi clientsApi;
7576
private final BranchesApi branchesApi;
7677
private final AIApi aiApi;
78+
private final StringCorrectionsApi stringCorrectionsApi;
7779

7880
public Client(Credentials credentials) {
7981
super(credentials);
@@ -110,6 +112,7 @@ public Client(Credentials credentials) {
110112
this.clientsApi = new ClientsApi(credentials);
111113
this.branchesApi = new BranchesApi(credentials);
112114
this.aiApi = new AIApi(credentials);
115+
this.stringCorrectionsApi = new StringCorrectionsApi(credentials);
113116
}
114117

115118
public Client(Credentials credentials, ClientConfig clientConfig) {
@@ -147,6 +150,7 @@ public Client(Credentials credentials, ClientConfig clientConfig) {
147150
this.clientsApi = new ClientsApi(credentials, clientConfig);
148151
this.branchesApi = new BranchesApi(credentials, clientConfig);
149152
this.aiApi = new AIApi(credentials, clientConfig);
153+
this.stringCorrectionsApi = new StringCorrectionsApi(credentials, clientConfig);
150154
}
151155

152156
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.crowdin.client.stringcorrections;
2+
3+
import com.crowdin.client.core.CrowdinApi;
4+
import com.crowdin.client.core.http.HttpRequestConfig;
5+
import com.crowdin.client.core.http.exceptions.HttpBadRequestException;
6+
import com.crowdin.client.core.http.exceptions.HttpException;
7+
import com.crowdin.client.core.model.ClientConfig;
8+
import com.crowdin.client.core.model.Credentials;
9+
import com.crowdin.client.core.model.ResponseList;
10+
import com.crowdin.client.core.model.ResponseObject;
11+
import com.crowdin.client.stringcorrections.model.*;
12+
13+
import java.util.Map;
14+
import java.util.Optional;
15+
16+
public class StringCorrectionsApi extends CrowdinApi {
17+
18+
19+
public StringCorrectionsApi(Credentials credentials) {
20+
super(credentials);
21+
}
22+
23+
public StringCorrectionsApi(Credentials credentials, ClientConfig clientConfig) {
24+
super(credentials, clientConfig);
25+
}
26+
27+
/**
28+
* @param projectId project identifier
29+
* @param params query params
30+
* @return list of corrections
31+
* @see <ul>
32+
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/String-Corrections/operation/api.projects.corrections.getMany" target="_blank"><b>Enterprise API Documentation</b></a></li>
33+
* </ul>
34+
*/
35+
public ResponseList<Correction> listCorrections(Long projectId, ListCorrectionsQueryParams params) throws HttpException, HttpBadRequestException {
36+
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
37+
"stringId", Optional.of(params.getStringId()),
38+
"orderBy", Optional.ofNullable(params.getOrderBy()),
39+
"denormalizePlaceholders", Optional.ofNullable(params.getDenormalizePlaceholders()),
40+
"limit", Optional.ofNullable(params.getLimit()),
41+
"offset", Optional.ofNullable(params.getOffset())
42+
);
43+
CorrectionResponseList response = this.httpClient.get(this.url + "/projects/" + projectId + "/corrections", new HttpRequestConfig(queryParams), CorrectionResponseList.class);
44+
return CorrectionResponseList.to(response);
45+
}
46+
47+
/**
48+
* @param projectId project identifier
49+
* @param request request object
50+
* @return newly created correction
51+
* @see <ul>
52+
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/String-Corrections/operation/api.projects.corrections.post" target="_blank"><b>Enterprise API Documentation</b></a></li>
53+
* </ul>
54+
*/
55+
public ResponseObject<Correction> addCorrection(Long projectId, AddCorrectionRequest request) throws HttpException, HttpBadRequestException {
56+
CorrectionResponseObject response = this.httpClient.post(this.url + "/projects/" + projectId + "/corrections", request, new HttpRequestConfig(), CorrectionResponseObject.class);
57+
return ResponseObject.of(response.getData());
58+
}
59+
60+
/**
61+
* @param projectId project identifier
62+
* @param correctionId correction identifier
63+
* @return correction object
64+
* @see <ul>
65+
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/String-Corrections/operation/api.projects.corrections.get" target="_blank"><b>Enterprise API Documentation</b></a></li>
66+
* </ul>
67+
*/
68+
public ResponseObject<Correction> getCorrection(Long projectId, Long correctionId) throws HttpException, HttpBadRequestException {
69+
CorrectionResponseObject response = this.httpClient.get(this.url + "/projects/" + projectId + "/corrections/" + correctionId, new HttpRequestConfig(), CorrectionResponseObject.class);
70+
return ResponseObject.of(response.getData());
71+
}
72+
73+
/**
74+
* @param projectId project identifier
75+
* @param stringId string identifier
76+
* @see <ul>
77+
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/String-Corrections/operation/api.projects.corrections.deleteMany" target="_blank"><b>Enterprise API Documentation</b></a></li>
78+
* </ul>
79+
*/
80+
public void deleteCorrections(Long projectId, Long stringId) throws HttpException, HttpBadRequestException {
81+
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
82+
"stringId", Optional.of(stringId)
83+
);
84+
this.httpClient.delete(this.url + "/projects/" + projectId + "/corrections", new HttpRequestConfig(queryParams), Void.class);
85+
}
86+
87+
/**
88+
* @param projectId project identifier
89+
* @param correctionId correction identifier
90+
* @return newly created correction
91+
* @see <ul>
92+
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/String-Corrections/operation/api.projects.corrections.put" target="_blank"><b>Enterprise API Documentation</b></a></li>
93+
* </ul>
94+
*/
95+
public ResponseObject<Correction> restoreCorrection(Long projectId, Long correctionId) throws HttpException, HttpBadRequestException {
96+
CorrectionResponseObject response = this.httpClient.put(this.url + "/projects/" + projectId + "/corrections/" + correctionId, null, new HttpRequestConfig(), CorrectionResponseObject.class);
97+
return ResponseObject.of(response.getData());
98+
}
99+
100+
/**
101+
* @param projectId project identifier
102+
* @param correctionId correction identifier
103+
* @see <ul>
104+
* <li><a href="https://support.crowdin.com/developer/enterprise/api/v2/#tag/String-Corrections/operation/api.projects.corrections.delete" target="_blank"><b>Enterprise API Documentation</b></a></li>
105+
* </ul>
106+
*/
107+
public void deleteCorrection(Long projectId, Long correctionId) throws HttpException, HttpBadRequestException {
108+
this.httpClient.delete(this.url + "/projects/" + projectId + "/corrections/" + correctionId, new HttpRequestConfig(), Void.class);
109+
}
110+
111+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.crowdin.client.stringcorrections.model;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class AddCorrectionRequest {
7+
private Long stringId;
8+
private String text;
9+
private String pluralCategoryName;
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.crowdin.client.stringcorrections.model;
2+
3+
import lombok.Data;
4+
5+
import java.util.Date;
6+
7+
@Data
8+
public class Correction {
9+
10+
private Long id;
11+
private String text;
12+
private String pluralCategoryName;
13+
private User user;
14+
private Date createdAt;
15+
16+
@Data
17+
public static class User {
18+
private Long id;
19+
private String username;
20+
private String fullName;
21+
private String avatarUrl;
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.crowdin.client.stringcorrections.model;
2+
3+
import com.crowdin.client.core.model.Pagination;
4+
import com.crowdin.client.core.model.ResponseList;
5+
import com.crowdin.client.core.model.ResponseObject;
6+
import lombok.Data;
7+
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
@Data
12+
public class CorrectionResponseList {
13+
14+
private List<CorrectionResponseObject> data;
15+
private Pagination pagination;
16+
17+
public static ResponseList<Correction> to(CorrectionResponseList correctionResponseList) {
18+
return ResponseList.of(
19+
correctionResponseList.getData().stream()
20+
.map(CorrectionResponseObject::getData)
21+
.map(ResponseObject::of)
22+
.collect(Collectors.toList()),
23+
correctionResponseList.getPagination()
24+
);
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.crowdin.client.stringcorrections.model;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class CorrectionResponseObject {
7+
8+
private Correction data;
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.crowdin.client.stringcorrections.model;
2+
3+
import com.crowdin.client.core.model.BooleanInt;
4+
import com.crowdin.client.core.model.Pagination;
5+
import lombok.Data;
6+
import lombok.EqualsAndHashCode;
7+
8+
@EqualsAndHashCode(callSuper = true)
9+
@Data
10+
public class ListCorrectionsQueryParams extends Pagination {
11+
private Long stringId;
12+
private String orderBy;
13+
private BooleanInt denormalizePlaceholders;
14+
}

src/test/java/com/crowdin/client/framework/RequestMock.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ public static RequestMock build(String url, String httpMethod, String responseFi
5252
);
5353
}
5454

55+
public static RequestMock build(String url, String httpMethod, Map<String, ?> urlParams) {
56+
return new RequestMock(
57+
url,
58+
null,
59+
null,
60+
httpMethod,
61+
urlParams,
62+
Collections.emptyMap()
63+
);
64+
}
65+
5566
public static RequestMock build(String url, String httpMethod) {
5667
return new RequestMock(
5768
url,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.crowdin.client.stringcorrections.bundles;
2+
3+
import com.crowdin.client.core.model.ResponseList;
4+
import com.crowdin.client.core.model.ResponseObject;
5+
import com.crowdin.client.framework.RequestMock;
6+
import com.crowdin.client.framework.TestClient;
7+
import com.crowdin.client.stringcorrections.model.AddCorrectionRequest;
8+
import com.crowdin.client.stringcorrections.model.Correction;
9+
import com.crowdin.client.stringcorrections.model.ListCorrectionsQueryParams;
10+
import org.apache.http.client.methods.HttpDelete;
11+
import org.apache.http.client.methods.HttpGet;
12+
import org.apache.http.client.methods.HttpPost;
13+
import org.apache.http.client.methods.HttpPut;
14+
import org.junit.jupiter.api.Test;
15+
16+
import java.util.Arrays;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
23+
public class StringCorrectionsApiTest extends TestClient {
24+
25+
private final Long projectId = 1L;
26+
private final Long stringId = 35434L;
27+
private final Long correctionId = 190695L;
28+
private final String text = "This string has been corrected";
29+
private final String pluralCategoryName = "few";
30+
31+
@Override
32+
public List<RequestMock> getMocks() {
33+
return Arrays.asList(
34+
RequestMock.build(this.url + "/projects/" + projectId + "/corrections", HttpGet.METHOD_NAME, "api/stringcorrections/listCorrectionsResponse.json", new HashMap<String, Long>() {{
35+
put("stringId", stringId);
36+
}}),
37+
RequestMock.build(this.url + "/projects/" + projectId + "/corrections", HttpPost.METHOD_NAME, "api/stringcorrections/addCorrectionRequest.json", "api/stringcorrections/correction.json"),
38+
RequestMock.build(this.url + "/projects/" + projectId + "/corrections/" + correctionId, HttpGet.METHOD_NAME, "api/stringcorrections/correction.json"),
39+
RequestMock.build(this.url + "/projects/" + projectId + "/corrections/" + correctionId, HttpDelete.METHOD_NAME),
40+
RequestMock.build(this.url + "/projects/" + projectId + "/corrections", HttpDelete.METHOD_NAME, new HashMap<String, Long>() {{
41+
put("stringId", stringId);
42+
}}),
43+
RequestMock.build(this.url + "/projects/" + projectId + "/corrections/" + correctionId, HttpPut.METHOD_NAME, "api/stringcorrections/correction.json")
44+
);
45+
}
46+
47+
@Test
48+
public void listCorrectionsTest() {
49+
ListCorrectionsQueryParams listCorrectionsQueryParams = new ListCorrectionsQueryParams();
50+
listCorrectionsQueryParams.setStringId(stringId);
51+
ResponseList<Correction> response = this.getStringCorrectionsApi().listCorrections(projectId, listCorrectionsQueryParams);
52+
assertNotNull(response);
53+
assertEquals(1, response.getData().size());
54+
assertEquals(response.getData().get(0).getData().getId(), correctionId);
55+
assertEquals(response.getData().get(0).getData().getText(), text);
56+
assertEquals(response.getData().get(0).getData().getPluralCategoryName(), pluralCategoryName);
57+
}
58+
59+
@Test
60+
public void addCorrectionTest() {
61+
AddCorrectionRequest request = new AddCorrectionRequest();
62+
request.setStringId(stringId);
63+
request.setPluralCategoryName(pluralCategoryName);
64+
request.setText(text);
65+
66+
ResponseObject<Correction> response = this.getStringCorrectionsApi().addCorrection(projectId, request);
67+
assertEquals(response.getData().getText(), text);
68+
assertEquals(response.getData().getId(), correctionId);
69+
assertEquals(response.getData().getPluralCategoryName(), pluralCategoryName);
70+
}
71+
72+
@Test
73+
public void getCorrectionTest() {
74+
ResponseObject<Correction> response = this.getStringCorrectionsApi().getCorrection(projectId, correctionId);
75+
assertEquals(response.getData().getText(), text);
76+
assertEquals(response.getData().getId(), correctionId);
77+
assertEquals(response.getData().getPluralCategoryName(), pluralCategoryName);
78+
}
79+
80+
@Test
81+
public void deleteCorrectionTest() {
82+
this.getStringCorrectionsApi().deleteCorrection(projectId, correctionId);
83+
}
84+
85+
@Test
86+
public void deleteCorrectionsTest() {
87+
this.getStringCorrectionsApi().deleteCorrections(projectId, stringId);
88+
}
89+
90+
@Test
91+
public void restoreCorrectionsTest() {
92+
ResponseObject<Correction> response = this.getStringCorrectionsApi().restoreCorrection(projectId, correctionId);
93+
assertEquals(response.getData().getText(), text);
94+
assertEquals(response.getData().getId(), correctionId);
95+
assertEquals(response.getData().getPluralCategoryName(), pluralCategoryName);
96+
}
97+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"stringId": 35434,
3+
"text": "This string has been corrected",
4+
"pluralCategoryName": "few"
5+
}

0 commit comments

Comments
 (0)