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
63 changes: 63 additions & 0 deletions src/main/java/com/crowdin/client/sourcefiles/SourceFilesApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,67 @@ public ResponseObject<DownloadLink> downloadFilePreview(Long projectId, Long fil
DownloadLinkResponseObject downloadLinkResponseObject = this.httpClient.get(this.url + "/projects/" + projectId + "/files/" + fileId + "/preview", new HttpRequestConfig(), DownloadLinkResponseObject.class);
return ResponseObject.of(downloadLinkResponseObject.getData());
}

/**
* @param projectId project identifier
* @param fileId file identifier
* @param limit maximum number of items to retrieve (default 25)
* @param offset starting offset in the collection (default 0)
* @return list of asset references
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.files.references.getMany" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.files.references.getMany" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<AssetReference> listAssetReferences(Long projectId, Long fileId, Integer limit, Integer offset) throws HttpException, HttpBadRequestException {
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
"limit", Optional.ofNullable(limit),
"offset", Optional.ofNullable(offset)
);
AssetReferenceResponseList assetReferenceResponseList = this.httpClient.get(this.url + "/projects/" + projectId + "/files/" + fileId + "/references", new HttpRequestConfig(queryParams), AssetReferenceResponseList.class);
return AssetReferenceResponseList.to(assetReferenceResponseList);
}

/**
* @param projectId project identifier
* @param fileId file identifier
* @param referenceId reference identifier
* @return asset reference
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.files.references.get" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.files.references.get" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<AssetReference> getAssetReference(Long projectId, Long fileId, Long referenceId) throws HttpException, HttpBadRequestException {
AssetReferenceResponseObject assetResponseObject = this.httpClient.get(this.url + "/projects/" + projectId + "/files/" + fileId + "/references/" + referenceId, new HttpRequestConfig(), AssetReferenceResponseObject.class);
return ResponseObject.of(assetResponseObject.getData());
}

/**
* @param projectId project identifier
* @param fileId file identifier
* @param request request object
* @return newly created asset reference
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.files.references.post" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.files.references.post" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<AssetReference> addAssetReference(Long projectId, Long fileId, AddAssetReferenceRequest request) throws HttpException, HttpBadRequestException {
AssetReferenceResponseObject post = this.httpClient.post(this.url + "/projects/" + projectId + "/files/" + fileId + "/references", request, new HttpRequestConfig(), AssetReferenceResponseObject.class);
return ResponseObject.of(post.getData());
}

/**
* @param projectId project identifier
* @param fileId file identifier
* @param referenceId reference identifier
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.files.references.delete" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.files.references.delete" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public void deleteAssetReference(Long projectId, Long fileId, Long referenceId) throws HttpException, HttpBadRequestException {
this.httpClient.delete(this.url + "/projects/" + projectId + "/files/" + fileId + "/references/" + referenceId, new HttpRequestConfig(), Void.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.crowdin.client.sourcefiles.model;

import lombok.Data;

@Data
public class AddAssetReferenceRequest {
private Long storageId;
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.crowdin.client.sourcefiles.model;

import lombok.Data;

import java.util.Date;

@Data
public class AssetReference {
private Long id;
private String name;
private String url;
private User user;
private Date createdAt;
private String mimeType;

@Data
public static class User {
private Long id;
private String username;
private String fullName;
private String avatarUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.crowdin.client.sourcefiles.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 AssetReferenceResponseList {
private List<AssetReferenceResponseObject> data;
private Pagination pagination;

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

import lombok.Data;

@Data
public class AssetReferenceResponseObject {
private AssetReference data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SourceFilesApiTest extends TestClient {
private final Long project4Id = 6L;
private final Long fileId = 44L;
private final Long storageId = 61L;
private final Long referenceId = 123L;
private final Long fileRevisionId = 2L;
private final Long buildId = 42L;
private final String branchName = "develop-master";
Expand All @@ -43,6 +44,7 @@ public class SourceFilesApiTest extends TestClient {
private final String directoryName = "main";
private final String directory2Name = "main-#2";
private final String fileName = "umbrella_app.xliff";
private final String referenceName = "design_reference.png";
private final String context = "Context for translators";
private final String downloadLink = "test.com";
private final String status = "finished";
Expand Down Expand Up @@ -95,7 +97,11 @@ public List<RequestMock> getMocks() {
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds", this.url, projectId), HttpGet.METHOD_NAME, "api/sourcefiles/listReviewedSourceFileBuilds.json"),
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds", url, projectId), HttpPost.METHOD_NAME, "api/sourcefiles/buildReviewedSourceFilesRequest.json", "api/sourcefiles/buildReviewedSourceFiles.json"),
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds/%d", url, projectId, buildId), HttpGet.METHOD_NAME, "api/sourcefiles/checkReviewedSourceFilesBuildStatus.json"),
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds/%d/download", url, projectId, buildId), HttpGet.METHOD_NAME, "api/sourcefiles/downloadReviewedSourceFiles.json")
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds/%d/download", url, projectId, buildId), HttpGet.METHOD_NAME, "api/sourcefiles/downloadReviewedSourceFiles.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/files/" + fileId + "/references", HttpGet.METHOD_NAME, "api/sourcefiles/listAssetReferences.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/files/" + fileId + "/references", HttpPost.METHOD_NAME, "api/sourcefiles/addAssetReferenceRequest.json","api/sourcefiles/assetReference.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/files/" + fileId + "/references/" + referenceId, HttpGet.METHOD_NAME, "api/sourcefiles/assetReference.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/files/" + fileId + "/references/" + referenceId, HttpDelete.METHOD_NAME)
);
}

Expand Down Expand Up @@ -640,4 +646,36 @@ private void assertListFilesOrderByIdDesc(ResponseList<File> fileResponseList) {
assertNull(((DocxFileImportOptions) importOptions).getSrxStorageId());
}
//</editor-fold>

@Test
public void listAssetReferencesTest() {
ResponseList<AssetReference> referenceResponseList = this.getSourceFilesApi().listAssetReferences(projectId, fileId, null, null);
assertEquals(1, referenceResponseList.getData().size());
assertEquals(referenceId, referenceResponseList.getData().get(0).getData().getId());
assertEquals(referenceName, referenceResponseList.getData().get(0).getData().getName());
}

@Test
public void addAssetReferenceTest() {
AddAssetReferenceRequest request = new AddAssetReferenceRequest();
request.setName(referenceName);
request.setStorageId(67890L);
ResponseObject<AssetReference> assetReferenceObject = this.getSourceFilesApi().addAssetReference(projectId, fileId, request);
assertEquals(referenceId, assetReferenceObject.getData().getId());
assertEquals(referenceName, assetReferenceObject.getData().getName());
}

@Test
public void getAssetReferenceTest() {
TimeZone.setDefault(tz);
ResponseObject<AssetReference> assetReferenceObject = this.getSourceFilesApi().getAssetReference(projectId, fileId, referenceId);
assertEquals(referenceId, assetReferenceObject.getData().getId());
assertEquals(referenceName, assetReferenceObject.getData().getName());
assertEquals(new Date(119,Calendar.SEPTEMBER, 20, 11, 5, 24), assetReferenceObject.getData().getCreatedAt());
}

@Test
public void deleteAssetReferenceTest() {
this.getSourceFilesApi().deleteAssetReference(projectId, fileId, referenceId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"storageId": 67890,
"name": "design_reference.png"
}
15 changes: 15 additions & 0 deletions src/test/resources/api/sourcefiles/assetReference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": {
"id": 123,
"name": "design_reference.png",
"url": "https://example.com/reference/design_reference.png",
"user": {
"id": 1,
"username": "john_doe",
"fullName": "John Smith",
"avatarUrl": ""
},
"createdAt": "2019-09-20T11:05:24+00:00",
"mimeType": "image/png"
}
}
23 changes: 23 additions & 0 deletions src/test/resources/api/sourcefiles/listAssetReferences.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"data": [
{
"data": {
"id": 123,
"name": "design_reference.png",
"url": "https://example.com/reference/design_reference.png",
"user": {
"id": 1,
"username": "john_doe",
"fullName": "John Smith",
"avatarUrl": ""
},
"createdAt": "2019-09-20T11:05:24+00:00",
"mimeType": "image/png"
}
}
],
"pagination": {
"offset": 0,
"limit": 25
}
}
Loading