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(api): add String Batch Operations API support #166

Merged
merged 1 commit into from
Jul 26, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,19 @@ public ResponseObject<SourceString> editSourceString(Long projectId, Long string
SourceStringResponseObject sourceStringResponseObject = this.httpClient.patch(this.url + "/projects/" + projectId + "/strings/" + stringId, request, new HttpRequestConfig(), SourceStringResponseObject.class);
return ResponseObject.of(sourceStringResponseObject.getData());
}

/**
* @param projectId project identifier
* @param request request object
* @return updated source string
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.strings.batchPatch" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.strings.batchPatch" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<SourceString> stringBatchOperations(Long projectId, List<PatchRequest> request) throws HttpException, HttpBadRequestException {
String url = this.url + "/projects/" + projectId + "/strings";
SourceStringResponseList sourceStringResponseList = this.httpClient.patch(url, request, new HttpRequestConfig(), SourceStringResponseList.class);
return SourceStringResponseList.to(sourceStringResponseList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.crowdin.client.sourcestrings.model;

import lombok.Data;

import java.util.List;

@Data
public class SourceStringForm {
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
private Long fileId;
private String identifier;
private Object text;
private String context;
private Integer maxLength;
private Boolean isHidden;
private List<Long> labelIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
import com.crowdin.client.framework.TestClient;
import com.crowdin.client.sourcestrings.model.AddSourceStringRequest;
import com.crowdin.client.sourcestrings.model.SourceString;
import com.crowdin.client.sourcestrings.model.SourceStringForm;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class SourceStringsApiTest extends TestClient {
Expand All @@ -36,7 +39,8 @@ public List<RequestMock> getMocks() {
RequestMock.build(this.url + "/projects/" + projectId + "/strings", HttpPost.METHOD_NAME, "api/strings/addStringRequest.json", "api/strings/string.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/strings/" + id, HttpGet.METHOD_NAME, "api/strings/string.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/strings/" + id, HttpDelete.METHOD_NAME),
RequestMock.build(this.url + "/projects/" + projectId + "/strings/" + id, HttpPatch.METHOD_NAME, "api/strings/editString.json", "api/strings/string.json")
RequestMock.build(this.url + "/projects/" + projectId + "/strings/" + id, HttpPatch.METHOD_NAME, "api/strings/editString.json", "api/strings/string.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/strings", HttpPatch.METHOD_NAME, "api/strings/stringBatchOperationsRequest.json", "api/strings/listStrings.json")
);
}

Expand Down Expand Up @@ -89,4 +93,45 @@ public void editStringTest() {
assertEquals(sourceStringResponseObject.getData().getId(), id);
assertEquals(sourceStringResponseObject.getData().getText(), text);
}

@Test
public void stringBatchOperationsTest() {
List<PatchRequest> request = new ArrayList<PatchRequest>() {{
add(new PatchRequest() {{
setOp(PatchOperation.REPLACE);
setPath("/2814/isHidden");
setValue(true);
}});
add(new PatchRequest() {{
setOp(PatchOperation.REPLACE);
setPath("/2814/context");
setValue("some context");
}});
add(new PatchRequest() {{
setOp(PatchOperation.ADD);
setPath("/-");
setValue(new SourceStringForm() {{
setText("new added string");
setIdentifier("a.b.c");
setContext("context for new string");
setFileId(5L);
setIsHidden(false);
}});
}});
add(new PatchRequest() {{
setOp(PatchOperation.REMOVE);
setPath("/2815");
}});
}};

ResponseList<SourceString> response = this.getSourceStringsApi().stringBatchOperations(projectId, request);

SourceString item = response.getData().get(0).getData();
assertNotNull(item);

assertEquals(2814, item.getId());
assertEquals(2, item.getProjectId());
assertEquals(48, item.getFileId());
assertEquals(667, item.getBranchId());
}
}
27 changes: 27 additions & 0 deletions src/test/resources/api/strings/stringBatchOperationsRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"op": "replace",
"path": "/2814/isHidden",
"value": true
},
{
"op": "replace",
"path": "/2814/context",
"value": "some context"
},
{
"op": "add",
"path": "/-",
"value": {
"text": "new added string",
"identifier": "a.b.c",
"context": "context for new string",
"fileId": 5,
"isHidden": false
}
},
{
"op": "remove",
"path": "/2815"
}
]