Skip to content

Commit 4b25348

Browse files
feat: add support for file references api (#351)
1 parent e9a2e31 commit 4b25348

9 files changed

Lines changed: 208 additions & 1 deletion

File tree

src/main/java/com/crowdin/client/sourcefiles/SourceFilesApi.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,67 @@ public ResponseObject<DownloadLink> downloadFilePreview(Long projectId, Long fil
481481
DownloadLinkResponseObject downloadLinkResponseObject = this.httpClient.get(this.url + "/projects/" + projectId + "/files/" + fileId + "/preview", new HttpRequestConfig(), DownloadLinkResponseObject.class);
482482
return ResponseObject.of(downloadLinkResponseObject.getData());
483483
}
484+
485+
/**
486+
* @param projectId project identifier
487+
* @param fileId file identifier
488+
* @param limit maximum number of items to retrieve (default 25)
489+
* @param offset starting offset in the collection (default 0)
490+
* @return list of asset references
491+
* @see <ul>
492+
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.files.references.getMany" target="_blank"><b>API Documentation</b></a></li>
493+
* <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>
494+
* </ul>
495+
*/
496+
public ResponseList<AssetReference> listAssetReferences(Long projectId, Long fileId, Integer limit, Integer offset) throws HttpException, HttpBadRequestException {
497+
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
498+
"limit", Optional.ofNullable(limit),
499+
"offset", Optional.ofNullable(offset)
500+
);
501+
AssetReferenceResponseList assetReferenceResponseList = this.httpClient.get(this.url + "/projects/" + projectId + "/files/" + fileId + "/references", new HttpRequestConfig(queryParams), AssetReferenceResponseList.class);
502+
return AssetReferenceResponseList.to(assetReferenceResponseList);
503+
}
504+
505+
/**
506+
* @param projectId project identifier
507+
* @param fileId file identifier
508+
* @param referenceId reference identifier
509+
* @return asset reference
510+
* @see <ul>
511+
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.files.references.get" target="_blank"><b>API Documentation</b></a></li>
512+
* <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>
513+
* </ul>
514+
*/
515+
public ResponseObject<AssetReference> getAssetReference(Long projectId, Long fileId, Long referenceId) throws HttpException, HttpBadRequestException {
516+
AssetReferenceResponseObject assetResponseObject = this.httpClient.get(this.url + "/projects/" + projectId + "/files/" + fileId + "/references/" + referenceId, new HttpRequestConfig(), AssetReferenceResponseObject.class);
517+
return ResponseObject.of(assetResponseObject.getData());
518+
}
519+
520+
/**
521+
* @param projectId project identifier
522+
* @param fileId file identifier
523+
* @param request request object
524+
* @return newly created asset reference
525+
* @see <ul>
526+
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.files.references.post" target="_blank"><b>API Documentation</b></a></li>
527+
* <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>
528+
* </ul>
529+
*/
530+
public ResponseObject<AssetReference> addAssetReference(Long projectId, Long fileId, AddAssetReferenceRequest request) throws HttpException, HttpBadRequestException {
531+
AssetReferenceResponseObject post = this.httpClient.post(this.url + "/projects/" + projectId + "/files/" + fileId + "/references", request, new HttpRequestConfig(), AssetReferenceResponseObject.class);
532+
return ResponseObject.of(post.getData());
533+
}
534+
535+
/**
536+
* @param projectId project identifier
537+
* @param fileId file identifier
538+
* @param referenceId reference identifier
539+
* @see <ul>
540+
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.projects.files.references.delete" target="_blank"><b>API Documentation</b></a></li>
541+
* <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>
542+
* </ul>
543+
*/
544+
public void deleteAssetReference(Long projectId, Long fileId, Long referenceId) throws HttpException, HttpBadRequestException {
545+
this.httpClient.delete(this.url + "/projects/" + projectId + "/files/" + fileId + "/references/" + referenceId, new HttpRequestConfig(), Void.class);
546+
}
484547
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.crowdin.client.sourcefiles.model;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class AddAssetReferenceRequest {
7+
private Long storageId;
8+
private String name;
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.crowdin.client.sourcefiles.model;
2+
3+
import lombok.Data;
4+
5+
import java.util.Date;
6+
7+
@Data
8+
public class AssetReference {
9+
private Long id;
10+
private String name;
11+
private String url;
12+
private User user;
13+
private Date createdAt;
14+
private String mimeType;
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.crowdin.client.sourcefiles.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 AssetReferenceResponseList {
13+
private List<AssetReferenceResponseObject> data;
14+
private Pagination pagination;
15+
16+
public static ResponseList<AssetReference> to(AssetReferenceResponseList assetReferenceList) {
17+
return ResponseList.of(assetReferenceList.getData().stream()
18+
.map(AssetReferenceResponseObject::getData)
19+
.map(ResponseObject::of)
20+
.collect(Collectors.toList()),
21+
assetReferenceList.getPagination()
22+
);
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.crowdin.client.sourcefiles.model;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class AssetReferenceResponseObject {
7+
private AssetReference data;
8+
}

src/test/java/com/crowdin/client/sourcefiles/SourceFilesApiTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class SourceFilesApiTest extends TestClient {
3535
private final Long project4Id = 6L;
3636
private final Long fileId = 44L;
3737
private final Long storageId = 61L;
38+
private final Long referenceId = 123L;
3839
private final Long fileRevisionId = 2L;
3940
private final Long buildId = 42L;
4041
private final String branchName = "develop-master";
@@ -43,6 +44,7 @@ public class SourceFilesApiTest extends TestClient {
4344
private final String directoryName = "main";
4445
private final String directory2Name = "main-#2";
4546
private final String fileName = "umbrella_app.xliff";
47+
private final String referenceName = "design_reference.png";
4648
private final String context = "Context for translators";
4749
private final String downloadLink = "test.com";
4850
private final String status = "finished";
@@ -95,7 +97,11 @@ public List<RequestMock> getMocks() {
9597
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds", this.url, projectId), HttpGet.METHOD_NAME, "api/sourcefiles/listReviewedSourceFileBuilds.json"),
9698
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds", url, projectId), HttpPost.METHOD_NAME, "api/sourcefiles/buildReviewedSourceFilesRequest.json", "api/sourcefiles/buildReviewedSourceFiles.json"),
9799
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds/%d", url, projectId, buildId), HttpGet.METHOD_NAME, "api/sourcefiles/checkReviewedSourceFilesBuildStatus.json"),
98-
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds/%d/download", url, projectId, buildId), HttpGet.METHOD_NAME, "api/sourcefiles/downloadReviewedSourceFiles.json")
100+
RequestMock.build(String.format("%s/projects/%d/strings/reviewed-builds/%d/download", url, projectId, buildId), HttpGet.METHOD_NAME, "api/sourcefiles/downloadReviewedSourceFiles.json"),
101+
RequestMock.build(this.url + "/projects/" + projectId + "/files/" + fileId + "/references", HttpGet.METHOD_NAME, "api/sourcefiles/listAssetReferences.json"),
102+
RequestMock.build(this.url + "/projects/" + projectId + "/files/" + fileId + "/references", HttpPost.METHOD_NAME, "api/sourcefiles/addAssetReferenceRequest.json","api/sourcefiles/assetReference.json"),
103+
RequestMock.build(this.url + "/projects/" + projectId + "/files/" + fileId + "/references/" + referenceId, HttpGet.METHOD_NAME, "api/sourcefiles/assetReference.json"),
104+
RequestMock.build(this.url + "/projects/" + projectId + "/files/" + fileId + "/references/" + referenceId, HttpDelete.METHOD_NAME)
99105
);
100106
}
101107

@@ -640,4 +646,36 @@ private void assertListFilesOrderByIdDesc(ResponseList<File> fileResponseList) {
640646
assertNull(((DocxFileImportOptions) importOptions).getSrxStorageId());
641647
}
642648
//</editor-fold>
649+
650+
@Test
651+
public void listAssetReferencesTest() {
652+
ResponseList<AssetReference> referenceResponseList = this.getSourceFilesApi().listAssetReferences(projectId, fileId, null, null);
653+
assertEquals(1, referenceResponseList.getData().size());
654+
assertEquals(referenceId, referenceResponseList.getData().get(0).getData().getId());
655+
assertEquals(referenceName, referenceResponseList.getData().get(0).getData().getName());
656+
}
657+
658+
@Test
659+
public void addAssetReferenceTest() {
660+
AddAssetReferenceRequest request = new AddAssetReferenceRequest();
661+
request.setName(referenceName);
662+
request.setStorageId(67890L);
663+
ResponseObject<AssetReference> assetReferenceObject = this.getSourceFilesApi().addAssetReference(projectId, fileId, request);
664+
assertEquals(referenceId, assetReferenceObject.getData().getId());
665+
assertEquals(referenceName, assetReferenceObject.getData().getName());
666+
}
667+
668+
@Test
669+
public void getAssetReferenceTest() {
670+
TimeZone.setDefault(tz);
671+
ResponseObject<AssetReference> assetReferenceObject = this.getSourceFilesApi().getAssetReference(projectId, fileId, referenceId);
672+
assertEquals(referenceId, assetReferenceObject.getData().getId());
673+
assertEquals(referenceName, assetReferenceObject.getData().getName());
674+
assertEquals(new Date(119,Calendar.SEPTEMBER, 20, 11, 5, 24), assetReferenceObject.getData().getCreatedAt());
675+
}
676+
677+
@Test
678+
public void deleteAssetReferenceTest() {
679+
this.getSourceFilesApi().deleteAssetReference(projectId, fileId, referenceId);
680+
}
643681
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"storageId": 67890,
3+
"name": "design_reference.png"
4+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"data": {
3+
"id": 123,
4+
"name": "design_reference.png",
5+
"url": "https://example.com/reference/design_reference.png",
6+
"user": {
7+
"id": 1,
8+
"username": "john_doe",
9+
"fullName": "John Smith",
10+
"avatarUrl": ""
11+
},
12+
"createdAt": "2019-09-20T11:05:24+00:00",
13+
"mimeType": "image/png"
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"data": [
3+
{
4+
"data": {
5+
"id": 123,
6+
"name": "design_reference.png",
7+
"url": "https://example.com/reference/design_reference.png",
8+
"user": {
9+
"id": 1,
10+
"username": "john_doe",
11+
"fullName": "John Smith",
12+
"avatarUrl": ""
13+
},
14+
"createdAt": "2019-09-20T11:05:24+00:00",
15+
"mimeType": "image/png"
16+
}
17+
}
18+
],
19+
"pagination": {
20+
"offset": 0,
21+
"limit": 25
22+
}
23+
}

0 commit comments

Comments
 (0)