From b818b6a47bbd6cc8b898dd99b35335ac87c409a9 Mon Sep 17 00:00:00 2001 From: vedantnd111 <48761376+vedantnd111@users.noreply.github.com> Date: Mon, 21 Oct 2024 12:42:38 +0530 Subject: [PATCH] feat: Add AI Setting Methods (#281) --- .../java/com/crowdin/client/ai/AIApi.java | 32 +++++++++++++++++++ .../crowdin/client/ai/model/AiSetting.java | 21 ++++++++++++ .../client/ai/model/AiSettingResponse.java | 9 ++++++ .../java/com/crowdin/client/ai/AIApiTest.java | 30 ++++++++++++++++- .../api/ai/editAiSettingRequest.json | 6 ++++ .../api/ai/getAiSettingResponse.json | 13 ++++++++ 6 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/crowdin/client/ai/model/AiSetting.java create mode 100644 src/main/java/com/crowdin/client/ai/model/AiSettingResponse.java create mode 100644 src/test/resources/api/ai/editAiSettingRequest.json create mode 100644 src/test/resources/api/ai/getAiSettingResponse.json diff --git a/src/main/java/com/crowdin/client/ai/AIApi.java b/src/main/java/com/crowdin/client/ai/AIApi.java index 4c1a386d..3df3a56d 100644 --- a/src/main/java/com/crowdin/client/ai/AIApi.java +++ b/src/main/java/com/crowdin/client/ai/AIApi.java @@ -1,5 +1,7 @@ package com.crowdin.client.ai; +import com.crowdin.client.ai.model.AiSettingResponse; +import com.crowdin.client.ai.model.AiSetting; import com.crowdin.client.ai.model.FineTuningDatasetDownload; import com.crowdin.client.ai.model.FineTuningDatasetDownloadResponse; import com.crowdin.client.ai.model.FineTuningDatasetRequest; @@ -15,8 +17,11 @@ import com.crowdin.client.core.http.HttpRequestConfig; import com.crowdin.client.core.model.ClientConfig; import com.crowdin.client.core.model.Credentials; +import com.crowdin.client.core.model.PatchRequest; import com.crowdin.client.core.model.ResponseList; import com.crowdin.client.core.model.ResponseObject; + +import java.util.List; import java.util.Map; import java.util.Optional; @@ -150,6 +155,33 @@ public ResponseObject downloadFineTuningDataset(final return ResponseObject.of(response.getData()); } + /** + * @param userId user identifier + * @return AI settings + * @see + */ + public ResponseObject getAiSetting(Long userId) { + String url = getAIPath(userId, "ai/settings"); + AiSettingResponse aiSettingResponse = this.httpClient.get(url, new HttpRequestConfig(), AiSettingResponse.class); + return ResponseObject.of(aiSettingResponse.getData()); + } + + /** + * @param userId user identifier + * @return updated AI settings + * @see + */ + public ResponseObject editAiSetting(Long userId, List request) { + String url = getAIPath(userId, "ai/settings"); + AiSettingResponse aiSettingResponse = this.httpClient.patch(url, request, new HttpRequestConfig(), AiSettingResponse.class); + return ResponseObject.of(aiSettingResponse.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/main/java/com/crowdin/client/ai/model/AiSetting.java b/src/main/java/com/crowdin/client/ai/model/AiSetting.java new file mode 100644 index 00000000..976f7a13 --- /dev/null +++ b/src/main/java/com/crowdin/client/ai/model/AiSetting.java @@ -0,0 +1,21 @@ +package com.crowdin.client.ai.model; + +import lombok.Data; + +import java.util.List; + +@Data +public class AiSetting { + + private Integer assistActionAiPromptId; + private Integer editorSuggestionAiPromptId; + + @Data + private static class ShortCut { + private String name; + private String prompt; + private Boolean isEnabled; + } + + private List shortcuts; +} diff --git a/src/main/java/com/crowdin/client/ai/model/AiSettingResponse.java b/src/main/java/com/crowdin/client/ai/model/AiSettingResponse.java new file mode 100644 index 00000000..58f1a12a --- /dev/null +++ b/src/main/java/com/crowdin/client/ai/model/AiSettingResponse.java @@ -0,0 +1,9 @@ +package com.crowdin.client.ai.model; + +import lombok.Data; + +@Data +public class AiSettingResponse { + + private AiSetting data; +} diff --git a/src/test/java/com/crowdin/client/ai/AIApiTest.java b/src/test/java/com/crowdin/client/ai/AIApiTest.java index a572df1f..676c6948 100644 --- a/src/test/java/com/crowdin/client/ai/AIApiTest.java +++ b/src/test/java/com/crowdin/client/ai/AIApiTest.java @@ -1,5 +1,6 @@ package com.crowdin.client.ai; +import com.crowdin.client.ai.model.AiSetting; import com.crowdin.client.ai.model.FineTuningDatasetDownload; import com.crowdin.client.ai.model.FineTuningDatasetRequest; import com.crowdin.client.ai.model.FineTuningDatasetResponse.FineTuningDatasetData; @@ -8,6 +9,8 @@ import com.crowdin.client.ai.model.FineTuningJobRequest; import com.crowdin.client.ai.model.FineTuningJobRequest.Hyperparameters; import com.crowdin.client.core.model.Pagination; +import com.crowdin.client.core.model.PatchRequest; +import com.crowdin.client.core.model.PatchOperation; import com.crowdin.client.core.model.ResponseList; import com.crowdin.client.core.model.ResponseObject; import com.crowdin.client.framework.RequestMock; @@ -20,6 +23,7 @@ import java.util.List; import java.util.TimeZone; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPost; import org.junit.jupiter.api.Test; @@ -53,6 +57,7 @@ public class AIApiTest extends TestClient { private static final String CREATE_FINE_TUNING_JOB_PATH = "%s/users/%d/ai/prompts/%d/fine-tuning/jobs"; private static final String GET_FINE_TUNING_JOB_STATUS_PATH = "%s/users/%d/ai/prompts/%d/fine-tuning/jobs/%s"; private static final String FINE_TUNING_DATASET_DOWNLOAD_PATH = "%s/users/%d/ai/prompts/%d/fine-tuning/datasets/%s/download"; + private static final String GET_SETTINGS = "%s/users/%d/ai/settings"; private Date getDateTime(int year, int month, int date, int hour, int minute, int second) { calendar.set(year, month, date, hour, minute, second); @@ -68,7 +73,9 @@ public List getMocks() { RequestMock.build(String.format(GET_FINE_TUNING_JOB_LIST_PATH, this.url, userId), HttpGet.METHOD_NAME, "api/ai/fineTuningJobListResponse.json"), RequestMock.build(String.format(CREATE_FINE_TUNING_JOB_PATH, this.url, userId, aiPromptId), HttpPost.METHOD_NAME, "api/ai/fineTuningJobRequest.json", "api/ai/fineTuningJobResponse.json"), RequestMock.build(String.format(GET_FINE_TUNING_JOB_STATUS_PATH, this.url, userId, aiPromptId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/fineTuningJobStatusResponse.json"), - RequestMock.build(String.format(FINE_TUNING_DATASET_DOWNLOAD_PATH, this.url, userId, aiPromptId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadFineTuningDataset.json") + RequestMock.build(String.format(FINE_TUNING_DATASET_DOWNLOAD_PATH, this.url, userId, aiPromptId, jobIdentifier), HttpGet.METHOD_NAME, "api/ai/downloadFineTuningDataset.json"), + RequestMock.build(String.format(GET_SETTINGS, this.url, userId), HttpGet.METHOD_NAME, "api/ai/getAiSettingResponse.json"), + RequestMock.build(String.format(GET_SETTINGS, this.url, userId), HttpPatch.METHOD_NAME, "api/ai/editAiSettingRequest.json", "api/ai/getAiSettingResponse.json") ); } @@ -175,4 +182,25 @@ public void downloadFineTuningDatasetTest() { assertEquals(responseObject.getData().getExpireIn(), dateCreated); assertNotNull(responseObject.getData().getUrl()); } + + @Test + public void getAiSettingTest() { + AiSetting aiSetting = this.getAiApi().getAiSetting(userId).getData(); + assertNotNull(aiSetting); + assertEquals(aiSetting.getAssistActionAiPromptId(), 2); + assertEquals(aiSetting.getEditorSuggestionAiPromptId(), 5); + assertEquals(aiSetting.getShortcuts().size(), 1); + } + + @Test + public void editAiSettingTest() { + PatchRequest request = new PatchRequest(); + request.setOp(PatchOperation.REPLACE); + request.setPath("/assistActionAiPromptId"); + ResponseObject aiSettingResponseObject = + this.getAiApi().editAiSetting(userId, Collections.singletonList(request)); + assertNotNull(aiSettingResponseObject.getData()); + assertEquals(aiSettingResponseObject.getData().getAssistActionAiPromptId(), 2); + assertEquals(aiSettingResponseObject.getData().getEditorSuggestionAiPromptId(), 5); + } } diff --git a/src/test/resources/api/ai/editAiSettingRequest.json b/src/test/resources/api/ai/editAiSettingRequest.json new file mode 100644 index 00000000..09bf655d --- /dev/null +++ b/src/test/resources/api/ai/editAiSettingRequest.json @@ -0,0 +1,6 @@ +[ + { + "op": "replace", + "path": "/assistActionAiPromptId" + } +] diff --git a/src/test/resources/api/ai/getAiSettingResponse.json b/src/test/resources/api/ai/getAiSettingResponse.json new file mode 100644 index 00000000..4b911e8c --- /dev/null +++ b/src/test/resources/api/ai/getAiSettingResponse.json @@ -0,0 +1,13 @@ +{ + "data": { + "assistActionAiPromptId": 2, + "editorSuggestionAiPromptId": 5, + "shortcuts": [ + { + "name": "My first shortcut", + "prompt": "Make translation shorter", + "enabled": true + } + ] + } +}