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: add support for the list and edit pre-translation API endpoints #268

Merged
merged 5 commits into from
Oct 8, 2024
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
45 changes: 45 additions & 0 deletions src/main/java/com/crowdin/client/translations/TranslationsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.core.model.DownloadLink;
import com.crowdin.client.core.model.DownloadLinkResponseObject;
import com.crowdin.client.core.model.PatchRequest;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.translations.model.ApplyPreTranslationRequest;
Expand All @@ -16,6 +17,9 @@
import com.crowdin.client.translations.model.BuildProjectFileTranslationRequest;
import com.crowdin.client.translations.model.BuildProjectTranslationRequest;
import com.crowdin.client.translations.model.ExportProjectTranslationRequest;
import com.crowdin.client.translations.model.PreTranslation;
import com.crowdin.client.translations.model.PreTranslationResponseList;
import com.crowdin.client.translations.model.PreTranslationResponseObject;
import com.crowdin.client.translations.model.PreTranslationStatus;
import com.crowdin.client.translations.model.PreTranslationStatusResponseObject;
import com.crowdin.client.translations.model.ProjectBuild;
Expand All @@ -30,6 +34,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -242,4 +247,44 @@ public ResponseObject<DownloadLink> exportProjectTranslation(Long projectId, Exp
DownloadLinkResponseObject response = this.httpClient.post(builtUrl, request, new HttpRequestConfig(), DownloadLinkResponseObject.class);
return ResponseObject.of(response.getData());
}

/**
* List Pre-Translations
*
* @param projectId project identifier
* @param limit maximum number of items to retrieve (default 25)
* @param offset starting offset in the collection (default 0)
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.pre-translations.getMany" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.pre-translations.getMany" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<PreTranslation> listPreTranslations(Long projectId, Integer limit, Integer offset) throws HttpException, HttpBadRequestException {
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
"limit", Optional.ofNullable(limit),
"offset", Optional.ofNullable(offset)
);
PreTranslationResponseList preTranslationResponseList = this.httpClient.get(this.url + "/projects/" + projectId + "/pre-translations", new HttpRequestConfig(queryParams), PreTranslationResponseList.class);
return PreTranslationResponseList.to(preTranslationResponseList);
}

/**
* Edit Pre-Translation
*
* @param projectId project identifier
* @param preTranslationId pre-translation identifier
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.pre-translations.patch" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.pre-translations.patch" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<PreTranslation> editPreTranslation(Long projectId, String preTranslationId, List<PatchRequest> request) throws HttpException, HttpBadRequestException {
PreTranslationResponseObject preTranslationResponseObject = this.httpClient.patch(
this.url + "/projects/" + projectId + "/pre-translations/" + preTranslationId,
request,
new HttpRequestConfig(),
PreTranslationResponseObject.class
);
return ResponseObject.of(preTranslationResponseObject.getData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.crowdin.client.translations.model;

import com.crowdin.client.core.model.EnumConverter;
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
public class PreTranslation {
private String identifier;
private Status status;
private Integer progress;
private Attributes attributes;
private Date createdAt;
private Date updatedAt;
private String startedAt;
private String finishedAt;

@Data
public static class Attributes {
private List<String> languageIds;
private List<Long> fileIds;
private String method;
private String autoApproveOption;
private Boolean duplicateTranslations;
private Boolean skipApprovedTranslations;
private Boolean translateUntranslatedOnly;
private Boolean translateWithPerfectMatchOnly;
}

public enum Status implements EnumConverter<Status> {
CREATED("created"),
IN_PROGRESS("in_progress"),
CANCELED("canceled"),
FAILED("failed"),
FINISHED("finished");

private String value;

Status(String value) {
this.value = value;
}

public static Status from(String value) {
for (Status s : Status.values()) {
if (s.value.equals(value)) {
return s;
}
}
return null;
}

@Override
public Object to(Status v) {
return this.value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.crowdin.client.translations.model;

import com.crowdin.client.core.model.Pagination;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import lombok.Data;

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

@Data
public class PreTranslationResponseList {
private List<PreTranslationResponseObject> data;
private Pagination pagination;

public static ResponseList<PreTranslation> to(PreTranslationResponseList preTranslationResponseList) {
return ResponseList.of(
preTranslationResponseList.getData().stream()
.map(PreTranslationResponseObject::getData)
.map(ResponseObject::of)
.collect(Collectors.toList()),
preTranslationResponseList.getPagination()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.crowdin.client.translations.model;

import lombok.Data;

@Data
public class PreTranslationResponseObject {
private PreTranslation data;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.crowdin.client.translations;

import com.crowdin.client.core.model.DownloadLink;
import com.crowdin.client.core.model.PatchOperation;
import com.crowdin.client.core.model.PatchRequest;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.framework.RequestMock;
Expand All @@ -16,6 +18,7 @@
import com.crowdin.client.translations.model.CrowdinTranslationCreateProjectPseudoBuildForm;
import com.crowdin.client.translations.model.ExportProjectTranslationRequest;
import com.crowdin.client.translations.model.Method;
import com.crowdin.client.translations.model.PreTranslation;
import com.crowdin.client.translations.model.PreTranslationStatus;
import com.crowdin.client.translations.model.ProjectBuild;
import com.crowdin.client.translations.model.UploadTranslationsRequest;
Expand All @@ -24,6 +27,7 @@
import com.crowdin.client.translations.model.UploadTranslationsStringsResponse;
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;

Expand Down Expand Up @@ -63,8 +67,10 @@ public List<RequestMock> getMocks() {
RequestMock.build(this.url + "/projects/" + projectId + "/translations/builds/" + buildId + "/download", HttpGet.METHOD_NAME, "api/translations/downloadLink.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/translations/builds/" + buildId, HttpGet.METHOD_NAME, "api/translations/projectBuildStatus.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/translations/builds/" + buildId, HttpDelete.METHOD_NAME),
RequestMock.build(String.format("%s/projects/%d/translations/exports", this.url, projectId), HttpPost.METHOD_NAME, "api/translations/exportProjectTranslationRequest.json", "api/translations/exportProjectTranslationResponse.json")
);
RequestMock.build(String.format("%s/projects/%d/translations/exports", this.url, projectId), HttpPost.METHOD_NAME, "api/translations/exportProjectTranslationRequest.json", "api/translations/exportProjectTranslationResponse.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/pre-translations", HttpGet.METHOD_NAME, "api/translations/listPreTranslations.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/pre-translations/" + preTranslationId, HttpPatch.METHOD_NAME, "api/translations/editPreTranslationRequest.json", "api/translations/editPreTranslationResponse.json")
);
}

@Test
Expand Down Expand Up @@ -235,4 +241,29 @@ public void exportProjectTranslationTest() {
ResponseObject<DownloadLink> response = this.getTranslationsApi().exportProjectTranslation(projectId, request);
assertEquals(link, response.getData().getUrl());
}

@Test
public void listPreTranslationsTest() {
ResponseList<PreTranslation> preTranslationsResponseList = this.getTranslationsApi().listPreTranslations(projectId, null, null);
assertEquals(preTranslationsResponseList.getData().size(), 1);
PreTranslation preTranslation = preTranslationsResponseList.getData().get(0).getData();
assertEquals(preTranslation.getIdentifier(), preTranslationId);

assertEquals(language, preTranslation.getAttributes().getLanguageIds().get(0));
assertEquals(fileId, preTranslation.getAttributes().getFileIds().get(0));
}

@Test
public void editPreTranslationTest() {
PatchRequest request = new PatchRequest();
request.setOp(PatchOperation.REPLACE);
request.setPath("/status");
request.setValue("cancelled");

ResponseObject<PreTranslation> preTranslationResponseObject = this.getTranslationsApi().editPreTranslation(projectId, preTranslationId, Arrays.asList(request));
assertEquals(preTranslationResponseObject.getData().getIdentifier(), preTranslationId);

assertEquals(language, preTranslationResponseObject.getData().getAttributes().getLanguageIds().get(0));
assertEquals(fileId, preTranslationResponseObject.getData().getAttributes().getFileIds().get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"op": "replace",
"path": "/status",
"value": "cancelled"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"data": {
"identifier": "9e7de270-4f83-41cb-b606-2f90631f26e2",
"status": "cancelled",
"progress": 50,
"attributes": {
"method": "tm",
"fileIds": [
2
],
"languageIds": [
"uk"
],
"autoApproveOption": "none",
"fallbackLanguages": null,
"duplicateTranslations": false,
"skipApprovedTranslations": false,
"translateUntranslatedOnly": true,
"translateWithPerfectMatchOnly": false
},
"createdAt": "2024-01-01T01:00:00+00:00",
"updatedAt": "2024-01-01T01:01:00+00:00",
"startedAt": "2024-01-01T01:00:00+00:00",
"finishedAt": "2024-01-01T01:01:00+00:00"
}
}
34 changes: 34 additions & 0 deletions src/test/resources/api/translations/listPreTranslations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"data": [
{
"data": {
"identifier": "9e7de270-4f83-41cb-b606-2f90631f26e2",
"status": "finished",
"progress": 100,
"attributes": {
"method": "tm",
"fileIds": [
2
],
"languageIds": [
"uk"
],
"autoApproveOption": "none",
"fallbackLanguages": null,
"duplicateTranslations": false,
"skipApprovedTranslations": false,
"translateUntranslatedOnly": true,
"translateWithPerfectMatchOnly": false
},
"createdAt": "2024-01-01T01:00:00+00:00",
"updatedAt": "2024-01-01T01:01:00+00:00",
"startedAt": "2024-01-01T01:00:00+00:00",
"finishedAt": "2024-01-01T01:01:00+00:00"
}
}
],
"pagination": {
"offset": 0,
"limit": 100
}
}