Skip to content

Commit f204fcb

Browse files
authored
feat: add AI Gateway provider methods (#362)
1 parent a7ee1c7 commit f204fcb

4 files changed

Lines changed: 111 additions & 1 deletion

File tree

src/main/java/com/crowdin/client/ai/AIApi.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,18 @@ public ResponseList<AiSupportedModel> listSupportedAiProviderModels(Long userId,
599599
public ResponseObject<AiTranslate> aiTranslateStrings(Long userId, AiTranslateRequest request) {
600600
String url = getAIPath(userId, "ai/translate");
601601
AiTranslateResponse response = this.httpClient.post(url, request, new HttpRequestConfig(), AiTranslateResponse.class);
602+
return ResponseObject.of(response.getData());
603+
}
602604

605+
/**
606+
* @param userId user identifier
607+
* @param aiProviderId id of AiProvider
608+
* @param path downstream endpoint path (e.g. chat/completions)
609+
* @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>
610+
*/
611+
public ResponseObject<Map<String, Object>> aiGatewayGet(final Long userId, final Long aiProviderId, final String path) {
612+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
613+
ChatCompletionResponseObject response = this.httpClient.get(url, new HttpRequestConfig(), ChatCompletionResponseObject.class);
603614
return ResponseObject.of(response.getData());
604615
}
605616

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

632+
/**
633+
* @param userId user identifier
634+
* @param aiProviderId id of AiProvider
635+
* @param path downstream endpoint path
636+
* @param request request body
637+
* @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>
638+
*/
639+
public ResponseObject<Map<String, Object>> aiGatewayPost(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
640+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
641+
ChatCompletionResponseObject response = this.httpClient.post(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
642+
return ResponseObject.of(response.getData());
643+
}
644+
621645
/**
622646
* @param userId user identifier
623647
* @param jobIdentifier AI file translation job identifier
@@ -633,6 +657,19 @@ public ResponseObject<AiFileTranslation> getAiFileTranslationStatus(Long userId,
633657
return ResponseObject.of(response.getData());
634658
}
635659

660+
/**
661+
* @param userId user identifier
662+
* @param aiProviderId id of AiProvider
663+
* @param path downstream endpoint path
664+
* @param request request body
665+
* @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>
666+
*/
667+
public ResponseObject<Map<String, Object>> aiGatewayPut(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
668+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
669+
ChatCompletionResponseObject response = this.httpClient.put(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
670+
return ResponseObject.of(response.getData());
671+
}
672+
636673
/**
637674
* @param userId user identifier
638675
* @param jobIdentifier AI file translation job identifier
@@ -646,6 +683,17 @@ public void cancelAiFileTranslation(Long userId, String jobIdentifier) {
646683
this.httpClient.delete(url, new HttpRequestConfig(), Void.class);
647684
}
648685

686+
/**
687+
* @param userId user identifier
688+
* @param aiProviderId id of AiProvider
689+
* @param path downstream endpoint path
690+
* @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>
691+
*/
692+
public void aiGatewayDelete(final Long userId, final Long aiProviderId, final String path) {
693+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
694+
this.httpClient.delete(url, new HttpRequestConfig(), Void.class);
695+
}
696+
649697
/**
650698
* @param userId user identifier
651699
* @param jobIdentifier AI file translation job identifier
@@ -676,6 +724,19 @@ public ResponseObject<DownloadLink> downloadAiFileTranslationStrings(Long userId
676724
return ResponseObject.of(response.getData());
677725
}
678726

727+
/**
728+
* @param userId user identifier
729+
* @param aiProviderId id of AiProvider
730+
* @param path downstream endpoint path
731+
* @param request request body
732+
* @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>
733+
*/
734+
public ResponseObject<Map<String, Object>> aiGatewayPatch(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
735+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
736+
ChatCompletionResponseObject response = this.httpClient.patch(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
737+
return ResponseObject.of(response.getData());
738+
}
739+
679740
private String getAIPath(Long userId, String path) {
680741
return userId != null ? String.format("%s/users/%d/%s", this.url, userId, path) : String.format("%s/%s", this.url, path);
681742
}

src/test/java/com/crowdin/client/ai/AIApiTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.apache.http.client.methods.HttpGet;
1616
import org.apache.http.client.methods.HttpPatch;
1717
import org.apache.http.client.methods.HttpPost;
18+
import org.apache.http.client.methods.HttpPut;
1819
import org.junit.jupiter.api.Test;
1920

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

7578
@Override
7679
public List<RequestMock> getMocks() {
@@ -117,7 +120,12 @@ public List<RequestMock> getMocks() {
117120
RequestMock.build(String.format(AI_FILE_TRANSLATION, this.url, userId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/aiFileTranslationResponse.json"),
118121
RequestMock.build(String.format(AI_FILE_TRANSLATION, this.url, userId, jobIdentifier), HttpDelete.METHOD_NAME),
119122
RequestMock.build(String.format(AI_FILE_TRANSLATION_DOWNLOAD, this.url, userId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadAiFileTranslationResponse.json"),
120-
RequestMock.build(String.format(AI_FILE_TRANSLATION_STRINGS, this.url, userId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadAiFileTranslationStringsResponse.json")
123+
RequestMock.build(String.format(AI_FILE_TRANSLATION_STRINGS, this.url, userId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadAiFileTranslationStringsResponse.json"),
124+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpGet.METHOD_NAME, "api/ai/aiGatewayResponse.json"),
125+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPost.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json"),
126+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPut.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json"),
127+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpDelete.METHOD_NAME),
128+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPatch.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json")
121129
);
122130
}
123131

@@ -618,4 +626,39 @@ public void downloadAiFileTranslationStringsTest() {
618626
assertNotNull(response.getData().getUrl());
619627
assertFalse(response.getData().getUrl().isEmpty());
620628
}
629+
630+
@Test
631+
public void aiGatewayGetTest() {
632+
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayGet(userId, 1L, gatewayPath);
633+
assertNotNull(response.getData());
634+
}
635+
636+
@Test
637+
public void aiGatewayPostTest() {
638+
Map<String, Object> req = new HashMap<>();
639+
req.put("model", "string");
640+
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPost(userId, 1L, gatewayPath, req);
641+
assertNotNull(response.getData());
642+
}
643+
644+
@Test
645+
public void aiGatewayPutTest() {
646+
Map<String, Object> req = new HashMap<>();
647+
req.put("model", "string");
648+
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPut(userId, 1L, gatewayPath, req);
649+
assertNotNull(response.getData());
650+
}
651+
652+
@Test
653+
public void aiGatewayDeleteTest() {
654+
this.getAiApi().aiGatewayDelete(userId, 1L, gatewayPath);
655+
}
656+
657+
@Test
658+
public void aiGatewayPatchTest() {
659+
Map<String, Object> req = new HashMap<>();
660+
req.put("model", "string");
661+
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPatch(userId, 1L, gatewayPath, req);
662+
assertNotNull(response.getData());
663+
}
621664
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"model": "string"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"data": {}
3+
}

0 commit comments

Comments
 (0)