Skip to content
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 @@ -5,6 +5,7 @@
import com.crowdin.client.core.http.exceptions.HttpBadRequestException;
import com.crowdin.client.core.http.exceptions.HttpException;
import com.crowdin.client.core.model.*;
import com.crowdin.client.sourcefiles.model.UpdateOption;
import com.crowdin.client.sourcestrings.model.*;

import java.util.List;
Expand Down Expand Up @@ -165,22 +166,57 @@ public void deleteSourceString(Long projectId, Long stringId) throws HttpExcepti
* </ul>
*/
public ResponseObject<SourceString> editSourceString(Long projectId, Long stringId, List<PatchRequest> request) throws HttpException, HttpBadRequestException {
SourceStringResponseObject sourceStringResponseObject = this.httpClient.patch(this.url + "/projects/" + projectId + "/strings/" + stringId, request, new HttpRequestConfig(), SourceStringResponseObject.class);
return ResponseObject.of(sourceStringResponseObject.getData());
return editSourceString(projectId, stringId, request, null);
}

/**
* @param projectId project identifier
* @param stringId string identifier
* @param request request object
* @param updateOption defines whether existing translations and approvals are kept when the string is updated (applied only when {@code text} or {@code identifier} is changed)
* @return updated source string
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.strings.patch" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.strings.patch" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<SourceString> editSourceString(Long projectId, Long stringId, List<PatchRequest> request, UpdateOption updateOption) throws HttpException, HttpBadRequestException {
HttpRequestConfig config = new HttpRequestConfig(HttpRequestConfig.buildUrlParams(
"updateOption", Optional.ofNullable(updateOption)
));
SourceStringResponseObject sourceStringResponseObject = this.httpClient.patch(this.url + "/projects/" + projectId + "/strings/" + stringId, request, config, SourceStringResponseObject.class);
return ResponseObject.of(sourceStringResponseObject.getData());
}

/**
* @param projectId project identifier
* @param request request object
* @return list of updated source strings
* @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 {
return stringBatchOperations(projectId, request, null);
}

/**
* @param projectId project identifier
* @param request request object
* @param updateOption defines whether existing translations and approvals are kept when a string is updated (applied only when {@code text} or {@code identifier} is changed)
* @return list of updated source strings
* @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, UpdateOption updateOption) throws HttpException, HttpBadRequestException {
HttpRequestConfig config = new HttpRequestConfig(HttpRequestConfig.buildUrlParams(
"updateOption", Optional.ofNullable(updateOption)
));
String url = this.url + "/projects/" + projectId + "/strings";
SourceStringResponseList sourceStringResponseList = this.httpClient.patch(url, request, new HttpRequestConfig(), SourceStringResponseList.class);
SourceStringResponseList sourceStringResponseList = this.httpClient.patch(url, request, config, SourceStringResponseList.class);
return SourceStringResponseList.to(sourceStringResponseList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import java.util.*;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -53,7 +55,9 @@ public List<RequestMock> getMocks() {
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", HttpPatch.METHOD_NAME, "api/strings/stringBatchOperationsRequest.json", "api/strings/listStrings.json")
RequestMock.build(this.url + "/projects/" + projectId + "/strings", HttpPatch.METHOD_NAME, "api/strings/stringBatchOperationsRequest.json", "api/strings/listStrings.json"),
new RequestMock(this.url + "/projects/" + project2Id + "/strings/" + id, "api/strings/editString.json", "api/strings/string.json", HttpPatch.METHOD_NAME, singletonMap("updateOption", UpdateOption.KEEP_TRANSLATIONS_AND_APPROVALS), emptyMap()),
new RequestMock(this.url + "/projects/" + project2Id + "/strings", "api/strings/stringBatchOperationsRequest.json", "api/strings/listStrings.json", HttpPatch.METHOD_NAME, singletonMap("updateOption", UpdateOption.CLEAR_TRANSLATIONS_AND_APPROVALS), emptyMap())
);
}

Expand Down Expand Up @@ -325,4 +329,54 @@ public void stringBatchOperationsTest() {
assertEquals(48, item.getFileId());
assertEquals(667, item.getBranchId());
}

@Test
public void editStringWithUpdateOptionTest() {
PatchRequest request = new PatchRequest();
request.setOp(PatchOperation.REPLACE);
request.setValue(text);
request.setPath("/text");
ResponseObject<SourceString> sourceStringResponseObject = this.getSourceStringsApi()
.editSourceString(project2Id, id, singletonList(request), UpdateOption.KEEP_TRANSLATIONS_AND_APPROVALS);
assertEquals(sourceStringResponseObject.getData().getId(), id);
assertEquals(sourceStringResponseObject.getData().getText(), text);
Comment on lines +339 to +342
}

@Test
public void stringBatchOperationsWithUpdateOptionTest() {
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(project2Id, request, UpdateOption.CLEAR_TRANSLATIONS_AND_APPROVALS);

SourceString item = response.getData().get(0).getData();
assertNotNull(item);
assertEquals(2814, item.getId());
}
}
Loading