Skip to content

Commit dacc95c

Browse files
committed
feat: add AI Gateway provider methods
Adds GET, POST, and PUT operations for the AI Gateway endpoints.
1 parent df1c08e commit dacc95c

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,68 @@ public ResponseList<AiSupportedModel> listSupportedAiProviderModels(Long userId,
587587
return AiSupportedModelResponseList.to(responseList);
588588
}
589589

590+
/**
591+
* @param userId user identifier
592+
* @param aiProviderId id of AiProvider
593+
* @param path downstream endpoint path (e.g. chat/completions)
594+
* @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>
595+
*/
596+
public ResponseObject<Map<String, Object>> aiGatewayGet(final Long userId, final Long aiProviderId, final String path) {
597+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
598+
ChatCompletionResponseObject response = this.httpClient.get(url, new HttpRequestConfig(), ChatCompletionResponseObject.class);
599+
return ResponseObject.of(response.getData());
600+
}
601+
602+
/**
603+
* @param userId user identifier
604+
* @param aiProviderId id of AiProvider
605+
* @param path downstream endpoint path
606+
* @param request request body
607+
* @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>
608+
*/
609+
public ResponseObject<Map<String, Object>> aiGatewayPost(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
610+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
611+
ChatCompletionResponseObject response = this.httpClient.post(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
612+
return ResponseObject.of(response.getData());
613+
}
614+
615+
/**
616+
* @param userId user identifier
617+
* @param aiProviderId id of AiProvider
618+
* @param path downstream endpoint path
619+
* @param request request body
620+
* @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>
621+
*/
622+
public ResponseObject<Map<String, Object>> aiGatewayPut(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
623+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
624+
ChatCompletionResponseObject response = this.httpClient.put(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
625+
return ResponseObject.of(response.getData());
626+
}
627+
628+
/**
629+
* @param userId user identifier
630+
* @param aiProviderId id of AiProvider
631+
* @param path downstream endpoint path
632+
* @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>
633+
*/
634+
public void aiGatewayDelete(final Long userId, final Long aiProviderId, final String path) {
635+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
636+
this.httpClient.delete(url, new HttpRequestConfig(), Void.class);
637+
}
638+
639+
/**
640+
* @param userId user identifier
641+
* @param aiProviderId id of AiProvider
642+
* @param path downstream endpoint path
643+
* @param request request body
644+
* @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>
645+
*/
646+
public ResponseObject<Map<String, Object>> aiGatewayPatch(final Long userId, final Long aiProviderId, final String path, final Map<String, Object> request) {
647+
String url = getAIPath(userId, "ai/providers/" + aiProviderId + "/gateway/" + path);
648+
ChatCompletionResponseObject response = this.httpClient.patch(url, request, new HttpRequestConfig(), ChatCompletionResponseObject.class);
649+
return ResponseObject.of(response.getData());
650+
}
651+
590652
private String getAIPath(Long userId, String path) {
591653
return userId != null ? String.format("%s/users/%d/%s", this.url, userId, path) : String.format("%s/%s", this.url, path);
592654
}

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.*;
@@ -66,6 +67,8 @@ public class AIApiTest extends TestClient {
6667
private static final String AI_PROMPT_COMPLETION_DOWNLOAD = "%s/users/%d/ai/prompts/%d/completions/%s/download";
6768
private static final String PROXY_CHAT = "%s/users/%d/ai/providers/%d/chat/completions";
6869
private static final String LIST_SUPPORTED_AI_PROVIDER_MODELS = "%s/users/%d/ai/providers/supported-models";
70+
private static final String AI_GATEWAY = "%s/users/%d/ai/providers/%d/gateway/chat/completions";
71+
private static final String gatewayPath = "chat/completions";
6972

7073
@Override
7174
public List<RequestMock> getMocks() {
@@ -106,7 +109,12 @@ public List<RequestMock> getMocks() {
106109
RequestMock.build(String.format(AI_PROMPT, this.url, userId, aiPromptId), HttpDelete.METHOD_NAME),
107110
RequestMock.build(String.format(AI_PROMPT, this.url, userId, aiPromptId), HttpPatch.METHOD_NAME, "api/ai/editPromptRequest.json", "api/ai/promptResponse.json"),
108111
RequestMock.build(String.format(PROXY_CHAT, this.url, userId, aiPromptId), HttpPost.METHOD_NAME, "api/ai/proxyChatCompletionRequest.json", "api/ai/proxyChatCompletionResponse.json"),
109-
RequestMock.build(String.format(LIST_SUPPORTED_AI_PROVIDER_MODELS, this.url, userId), HttpGet.METHOD_NAME, "api/ai/listSupportedAiProviderModels.json")
112+
RequestMock.build(String.format(LIST_SUPPORTED_AI_PROVIDER_MODELS, this.url, userId), HttpGet.METHOD_NAME, "api/ai/listSupportedAiProviderModels.json"),
113+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpGet.METHOD_NAME, "api/ai/aiGatewayResponse.json"),
114+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPost.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json"),
115+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPut.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json"),
116+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpDelete.METHOD_NAME),
117+
RequestMock.build(String.format(AI_GATEWAY, this.url, userId, 1), HttpPatch.METHOD_NAME, "api/ai/aiGatewayRequest.json", "api/ai/aiGatewayResponse.json")
110118
);
111119
}
112120

@@ -531,4 +539,39 @@ public void listSupportedAiProviderModelsTest() {
531539
assertTrue(response.getData().get(0).getData().getModalities().getOutput().getImage());
532540
assertEquals(0.1, response.getData().get(0).getData().getPrice().getInput());
533541
}
542+
543+
@Test
544+
public void aiGatewayGetTest() {
545+
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayGet(userId, 1L, gatewayPath);
546+
assertNotNull(response.getData());
547+
}
548+
549+
@Test
550+
public void aiGatewayPostTest() {
551+
Map<String, Object> req = new HashMap<>();
552+
req.put("model", "string");
553+
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPost(userId, 1L, gatewayPath, req);
554+
assertNotNull(response.getData());
555+
}
556+
557+
@Test
558+
public void aiGatewayPutTest() {
559+
Map<String, Object> req = new HashMap<>();
560+
req.put("model", "string");
561+
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPut(userId, 1L, gatewayPath, req);
562+
assertNotNull(response.getData());
563+
}
564+
565+
@Test
566+
public void aiGatewayDeleteTest() {
567+
this.getAiApi().aiGatewayDelete(userId, 1L, gatewayPath);
568+
}
569+
570+
@Test
571+
public void aiGatewayPatchTest() {
572+
Map<String, Object> req = new HashMap<>();
573+
req.put("model", "string");
574+
ResponseObject<Map<String, Object>> response = this.getAiApi().aiGatewayPatch(userId, 1L, gatewayPath, req);
575+
assertNotNull(response.getData());
576+
}
534577
}
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)