diff --git a/src/main/java/com/crowdin/client/ai/AIApi.java b/src/main/java/com/crowdin/client/ai/AIApi.java index ea59f5d7..76e08b77 100644 --- a/src/main/java/com/crowdin/client/ai/AIApi.java +++ b/src/main/java/com/crowdin/client/ai/AIApi.java @@ -599,7 +599,18 @@ public ResponseList listSupportedAiProviderModels(Long userId, public ResponseObject 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 API Documentation + */ + public ResponseObject> 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()); } @@ -618,6 +629,19 @@ public ResponseObject 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 API Documentation + */ + public ResponseObject> aiGatewayPost(final Long userId, final Long aiProviderId, final String path, final Map 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 @@ -633,6 +657,19 @@ public ResponseObject 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 API Documentation + */ + public ResponseObject> aiGatewayPut(final Long userId, final Long aiProviderId, final String path, final Map 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 @@ -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 API Documentation + */ + 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 @@ -676,6 +724,19 @@ public ResponseObject 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 API Documentation + */ + public ResponseObject> aiGatewayPatch(final Long userId, final Long aiProviderId, final String path, final Map 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); } diff --git a/src/test/java/com/crowdin/client/ai/AIApiTest.java b/src/test/java/com/crowdin/client/ai/AIApiTest.java index d516532d..5e591fbe 100644 --- a/src/test/java/com/crowdin/client/ai/AIApiTest.java +++ b/src/test/java/com/crowdin/client/ai/AIApiTest.java @@ -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.*; @@ -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 getMocks() { @@ -117,7 +120,12 @@ public List 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") ); } @@ -618,4 +626,39 @@ public void downloadAiFileTranslationStringsTest() { assertNotNull(response.getData().getUrl()); assertFalse(response.getData().getUrl().isEmpty()); } + + @Test + public void aiGatewayGetTest() { + ResponseObject> response = this.getAiApi().aiGatewayGet(userId, 1L, gatewayPath); + assertNotNull(response.getData()); + } + + @Test + public void aiGatewayPostTest() { + Map req = new HashMap<>(); + req.put("model", "string"); + ResponseObject> response = this.getAiApi().aiGatewayPost(userId, 1L, gatewayPath, req); + assertNotNull(response.getData()); + } + + @Test + public void aiGatewayPutTest() { + Map req = new HashMap<>(); + req.put("model", "string"); + ResponseObject> 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 req = new HashMap<>(); + req.put("model", "string"); + ResponseObject> response = this.getAiApi().aiGatewayPatch(userId, 1L, gatewayPath, req); + assertNotNull(response.getData()); + } } diff --git a/src/test/resources/api/ai/aiGatewayRequest.json b/src/test/resources/api/ai/aiGatewayRequest.json new file mode 100644 index 00000000..4a550eaa --- /dev/null +++ b/src/test/resources/api/ai/aiGatewayRequest.json @@ -0,0 +1,3 @@ +{ + "model": "string" +} diff --git a/src/test/resources/api/ai/aiGatewayResponse.json b/src/test/resources/api/ai/aiGatewayResponse.json new file mode 100644 index 00000000..73cd5c32 --- /dev/null +++ b/src/test/resources/api/ai/aiGatewayResponse.json @@ -0,0 +1,3 @@ +{ + "data": {} +}