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
61 changes: 61 additions & 0 deletions src/main/java/com/crowdin/client/ai/AIApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,18 @@ public ResponseList<AiSupportedModel> listSupportedAiProviderModels(Long userId,
public ResponseObject<AiTranslate> aiTranslateStrings(Long userId, AiTranslateRequest request) {
String url = getAIPath(userId, "ai/translate");
AiTranslateResponse response = this.httpClient.post(url, request, new HttpRequestConfig(), AiTranslateResponse.class);
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @param aiProviderId id of AiProvider
* @param path downstream endpoint path (e.g. chat/completions)
* @see <a href="https://support.crowdin.com/developer/api/v2/#tag/AI-Gateway/operation/api.ai.providers.gateway.crowdin.get" target="_blank"><b>API Documentation</b></a>
*/
public ResponseObject<Map<String, Object>> aiGatewayGet(final Long userId, final Long aiProviderId, final String path) {
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
ChatCompletionResponseObject response = this.httpClient.get(url, new HttpRequestConfig(), ChatCompletionResponseObject.class);
return ResponseObject.of(response.getData());
}

Expand All @@ -618,6 +629,19 @@ public ResponseObject<AiFileTranslation> addAiFileTranslation(Long userId, AiFil
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @param aiProviderId id of AiProvider
* @param path downstream endpoint path
* @param request request body
* @see <a href="https://support.crowdin.com/developer/api/v2/#tag/AI-Gateway/operation/api.ai.providers.gateway.crowdin.post" target="_blank"><b>API Documentation</b></a>
*/
public ResponseObject<Map<String, Object>> aiGatewayPost(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
ChatCompletionResponseObject response = this.httpClient.post(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @param jobIdentifier AI file translation job identifier
Expand All @@ -633,6 +657,19 @@ public ResponseObject<AiFileTranslation> getAiFileTranslationStatus(Long userId,
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @param aiProviderId id of AiProvider
* @param path downstream endpoint path
* @param request request body
* @see <a href="https://support.crowdin.com/developer/api/v2/#tag/AI-Gateway/operation/api.ai.providers.gateway.crowdin.put" target="_blank"><b>API Documentation</b></a>
*/
public ResponseObject<Map<String, Object>> aiGatewayPut(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
ChatCompletionResponseObject response = this.httpClient.put(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @param jobIdentifier AI file translation job identifier
Expand All @@ -646,6 +683,17 @@ public void cancelAiFileTranslation(Long userId, String jobIdentifier) {
this.httpClient.delete(url, new HttpRequestConfig(), Void.class);
}

/**
* @param userId user identifier
* @param aiProviderId id of AiProvider
* @param path downstream endpoint path
* @see <a href="https://support.crowdin.com/developer/api/v2/#tag/AI-Gateway/operation/api.ai.providers.gateway.crowdin.delete" target="_blank"><b>API Documentation</b></a>
*/
public void aiGatewayDelete(final Long userId, final Long aiProviderId, final String path) {
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
this.httpClient.delete(url, new HttpRequestConfig(), Void.class);
}

/**
* @param userId user identifier
* @param jobIdentifier AI file translation job identifier
Expand Down Expand Up @@ -676,6 +724,19 @@ public ResponseObject<DownloadLink> downloadAiFileTranslationStrings(Long userId
return ResponseObject.of(response.getData());
}

/**
* @param userId user identifier
* @param aiProviderId id of AiProvider
* @param path downstream endpoint path
* @param request request body
* @see <a href="https://support.crowdin.com/developer/api/v2/#tag/AI-Gateway/operation/api.ai.providers.gateway.crowdin.patch" target="_blank"><b>API Documentation</b></a>
*/
public ResponseObject<Map<String, Object>> aiGatewayPatch(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
ChatCompletionResponseObject response = this.httpClient.patch(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
return ResponseObject.of(response.getData());
}

private String getAIPath(Long userId, String path) {
return userId != null ? String.format("%s/users/%d/%s", this.url, userId, path) : String.format("%s/%s", this.url, path);
}
Expand Down
45 changes: 44 additions & 1 deletion src/test/java/com/crowdin/client/ai/AIApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.junit.jupiter.api.Test;

import java.util.*;
Expand Down Expand Up @@ -71,6 +72,8 @@ public class AIApiTest extends TestClient {
private static final String AI_FILE_TRANSLATION = "%s/users/%d/ai/file-translations/%s";
private static final String AI_FILE_TRANSLATION_DOWNLOAD = "%s/users/%d/ai/file-translations/%s/download";
private static final String AI_FILE_TRANSLATION_STRINGS = "%s/users/%d/ai/file-translations/%s/translations";
private static final String AI_GATEWAY = "%s/users/%d/ai/providers/%d/gateway/chat/completions";
private static final String gatewayPath = "chat/completions";

@Override
public List<RequestMock> getMocks() {
Expand Down Expand Up @@ -117,7 +120,12 @@ public List<RequestMock> getMocks() {
RequestMock.build(String.format(AI_FILE_TRANSLATION, this.url, userId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/aiFileTranslationResponse.json"),
RequestMock.build(String.format(AI_FILE_TRANSLATION, this.url, userId, jobIdentifier), HttpDelete.METHOD_NAME),
RequestMock.build(String.format(AI_FILE_TRANSLATION_DOWNLOAD, this.url, userId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadAiFileTranslationResponse.json"),
RequestMock.build(String.format(AI_FILE_TRANSLATION_STRINGS, this.url, userId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadAiFileTranslationStringsResponse.json")
RequestMock.build(String.format(AI_FILE_TRANSLATION_STRINGS, this.url, userId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadAiFileTranslationStringsResponse.json"),
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpGet.METHOD_NAME, "api/ai/aiGatewayResponse.json"),
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPost.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json"),
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPut.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json"),
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpDelete.METHOD_NAME),
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPatch.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json")
);
}

Expand Down Expand Up @@ -618,4 +626,39 @@ public void downloadAiFileTranslationStringsTest() {
assertNotNull(response.getData().getUrl());
assertFalse(response.getData().getUrl().isEmpty());
}

@Test
public void aiGatewayGetTest() {
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayGet(userId, 1L, gatewayPath);
assertNotNull(response.getData());
}

@Test
public void aiGatewayPostTest() {
Map<String, Object> req = new HashMap<>();
req.put("model", "string");
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPost(userId, 1L, gatewayPath, req);
assertNotNull(response.getData());
}

@Test
public void aiGatewayPutTest() {
Map<String, Object> req = new HashMap<>();
req.put("model", "string");
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPut(userId, 1L, gatewayPath, req);
assertNotNull(response.getData());
}

@Test
public void aiGatewayDeleteTest() {
this.getAiApi().aiGatewayDelete(userId, 1L, gatewayPath);
}

@Test
public void aiGatewayPatchTest() {
Map<String, Object> req = new HashMap<>();
req.put("model", "string");
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPatch(userId, 1L, gatewayPath, req);
assertNotNull(response.getData());
}
}
3 changes: 3 additions & 0 deletions src/test/resources/api/ai/aiGatewayRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"model": "string"
}
3 changes: 3 additions & 0 deletions src/test/resources/api/ai/aiGatewayResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": {}
}
Loading