From b80e2ceee90cdac2aaa0b860062c3aa42eb1619d Mon Sep 17 00:00:00 2001 From: sugan0tech Date: Sat, 28 Oct 2023 14:09:45 +0530 Subject: [PATCH] add support to applications api closes #200 --- src/main/java/com/crowdin/client/Client.java | 4 + .../client/applications/ApplicationsApi.java | 97 +++++++++++++++++++ .../model/AddApplicationDataRequest.java | 10 ++ .../applications/model/ApplicationData.java | 10 ++ .../model/ApplicationDataResponseObject.java | 8 ++ .../model/EditApplicationDataRequest.java | 11 +++ ...UpdateOrRestoreApplicationDataRequest.java | 10 ++ .../applications/ApplicationsApiTest.java | 76 +++++++++++++++ .../api/applications/addApplicationData.json | 6 ++ .../api/applications/applicationData.json | 6 ++ .../applications/applicationDataRequest.json | 6 ++ .../api/applications/editApplicationData.json | 5 + .../updateOrRestoreApplicationData.json | 5 + 13 files changed, 254 insertions(+) create mode 100644 src/main/java/com/crowdin/client/applications/ApplicationsApi.java create mode 100644 src/main/java/com/crowdin/client/applications/model/AddApplicationDataRequest.java create mode 100644 src/main/java/com/crowdin/client/applications/model/ApplicationData.java create mode 100644 src/main/java/com/crowdin/client/applications/model/ApplicationDataResponseObject.java create mode 100644 src/main/java/com/crowdin/client/applications/model/EditApplicationDataRequest.java create mode 100644 src/main/java/com/crowdin/client/applications/model/UpdateOrRestoreApplicationDataRequest.java create mode 100644 src/test/java/com/crowdin/client/applications/ApplicationsApiTest.java create mode 100644 src/test/resources/api/applications/addApplicationData.json create mode 100644 src/test/resources/api/applications/applicationData.json create mode 100644 src/test/resources/api/applications/applicationDataRequest.json create mode 100644 src/test/resources/api/applications/editApplicationData.json create mode 100644 src/test/resources/api/applications/updateOrRestoreApplicationData.json diff --git a/src/main/java/com/crowdin/client/Client.java b/src/main/java/com/crowdin/client/Client.java index d2d726868..f33621274 100644 --- a/src/main/java/com/crowdin/client/Client.java +++ b/src/main/java/com/crowdin/client/Client.java @@ -1,5 +1,6 @@ package com.crowdin.client; +import com.crowdin.client.applications.ApplicationsApi; import com.crowdin.client.bundles.BundlesApi; import com.crowdin.client.core.CrowdinApi; import com.crowdin.client.core.model.ClientConfig; @@ -62,6 +63,7 @@ public class Client extends CrowdinApi { private final StringCommentsApi stringCommentsApi; private final BundlesApi bundlesApi; private final NotificationsApi notificationsApi; + private final ApplicationsApi applicationsApi; public Client(Credentials credentials) { super(credentials); @@ -92,6 +94,7 @@ public Client(Credentials credentials) { this.stringCommentsApi = new StringCommentsApi(credentials); this.bundlesApi = new BundlesApi(credentials); this.notificationsApi = new NotificationsApi(credentials); + this.applicationsApi = new ApplicationsApi(credentials); } public Client(Credentials credentials, ClientConfig clientConfig) { @@ -123,6 +126,7 @@ public Client(Credentials credentials, ClientConfig clientConfig) { this.stringCommentsApi = new StringCommentsApi(credentials, clientConfig); this.bundlesApi = new BundlesApi(credentials, clientConfig); this.notificationsApi = new NotificationsApi(credentials, clientConfig); + this.applicationsApi = new ApplicationsApi(credentials, clientConfig); } } diff --git a/src/main/java/com/crowdin/client/applications/ApplicationsApi.java b/src/main/java/com/crowdin/client/applications/ApplicationsApi.java new file mode 100644 index 000000000..97390e44b --- /dev/null +++ b/src/main/java/com/crowdin/client/applications/ApplicationsApi.java @@ -0,0 +1,97 @@ +package com.crowdin.client.applications; + +import com.crowdin.client.applications.model.*; +import com.crowdin.client.core.CrowdinApi; +import com.crowdin.client.core.http.HttpRequestConfig; +import com.crowdin.client.core.http.exceptions.HttpBadRequestException; +import com.crowdin.client.core.http.exceptions.HttpException; +import com.crowdin.client.core.model.ClientConfig; +import com.crowdin.client.core.model.Credentials; +import com.crowdin.client.core.model.ResponseObject; + + +public class ApplicationsApi extends CrowdinApi { + public ApplicationsApi(Credentials credentials) { + super(credentials); + } + + public ApplicationsApi(Credentials credentials, ClientConfig clientConfig) { + super(credentials, clientConfig); + } + + /** + * @param applicationIdentifier identifier of the application + * @param path The path is implemented by the application + * @return application data + * @see + */ + public ResponseObject getApplicationData(String applicationIdentifier, String path) throws HttpException, HttpBadRequestException { + String builtUrl = String.format("%s/applications/%s/api/%s", this.url, applicationIdentifier, path); + ApplicationDataResponseObject response = this.httpClient.get(builtUrl, new HttpRequestConfig(), ApplicationDataResponseObject.class); + return ResponseObject.of(response.getData()); + } + + /** + * @param applicationIdentifier identifier of the application + * @param path The path is implemented by the application + * @return application data + * @param request request object + * @see + */ + public ResponseObject updateOrReplaceApplicationData(String applicationIdentifier, String path, UpdateOrRestoreApplicationDataRequest request) throws HttpException, HttpBadRequestException { + String builtUrl = String.format("%s/applications/%s/api/%s", this.url, applicationIdentifier, path); + ApplicationDataResponseObject response = this.httpClient.put(builtUrl, request, new HttpRequestConfig(), ApplicationDataResponseObject.class); + return ResponseObject.of(response.getData()); + } + + /** + * @param applicationIdentifier identifier of the application + * @param path The path is implemented by the application + * @param request request object + * @return application data + * @see + */ + public ResponseObject addApplicationData(String applicationIdentifier, String path, AddApplicationDataRequest request) throws HttpException, HttpBadRequestException { + String builtUrl = String.format("%s/applications/%s/api/%s", this.url, applicationIdentifier, path); + ApplicationDataResponseObject response = this.httpClient.post(builtUrl, request, new HttpRequestConfig(), ApplicationDataResponseObject.class); + return ResponseObject.of(response.getData()); + } + + /** + * @param applicationIdentifier identifier of the application + * @param path The path is implemented by the application + * @see + */ + public void deleteApplicationData(String applicationIdentifier, String path) throws HttpException, HttpBadRequestException { + String builtUrl = String.format("%s/applications/%s/api/%s", this.url, applicationIdentifier, path); + this.httpClient.delete(builtUrl, new HttpRequestConfig(), Void.class); + } + + /** + * @param applicationIdentifier identifier of the application + * @param path The path is implemented by the application + * @param request request object + * @return application data + * @see + */ + public ResponseObject editApplicationData(String applicationIdentifier, String path, EditApplicationDataRequest request) throws HttpException, HttpBadRequestException { + String builtUrl = String.format("%s/applications/%s/api/%s", this.url, applicationIdentifier, path); + ApplicationDataResponseObject response = this.httpClient.patch(builtUrl, request, new HttpRequestConfig(), ApplicationDataResponseObject.class); + return ResponseObject.of(response.getData()); + } +} diff --git a/src/main/java/com/crowdin/client/applications/model/AddApplicationDataRequest.java b/src/main/java/com/crowdin/client/applications/model/AddApplicationDataRequest.java new file mode 100644 index 000000000..b9564ce24 --- /dev/null +++ b/src/main/java/com/crowdin/client/applications/model/AddApplicationDataRequest.java @@ -0,0 +1,10 @@ +package com.crowdin.client.applications.model; + +import lombok.Data; + +import java.util.Map; + +@Data +public class AddApplicationDataRequest { + private Map data; +} diff --git a/src/main/java/com/crowdin/client/applications/model/ApplicationData.java b/src/main/java/com/crowdin/client/applications/model/ApplicationData.java new file mode 100644 index 000000000..201f82e1e --- /dev/null +++ b/src/main/java/com/crowdin/client/applications/model/ApplicationData.java @@ -0,0 +1,10 @@ +package com.crowdin.client.applications.model; + +import lombok.Data; + +import java.util.Map; + +@Data +public class ApplicationData { + private Map data; +} diff --git a/src/main/java/com/crowdin/client/applications/model/ApplicationDataResponseObject.java b/src/main/java/com/crowdin/client/applications/model/ApplicationDataResponseObject.java new file mode 100644 index 000000000..841f87f10 --- /dev/null +++ b/src/main/java/com/crowdin/client/applications/model/ApplicationDataResponseObject.java @@ -0,0 +1,8 @@ +package com.crowdin.client.applications.model; + +import lombok.Data; + +@Data +public class ApplicationDataResponseObject { + private ApplicationData data; +} diff --git a/src/main/java/com/crowdin/client/applications/model/EditApplicationDataRequest.java b/src/main/java/com/crowdin/client/applications/model/EditApplicationDataRequest.java new file mode 100644 index 000000000..e0f7141d2 --- /dev/null +++ b/src/main/java/com/crowdin/client/applications/model/EditApplicationDataRequest.java @@ -0,0 +1,11 @@ +package com.crowdin.client.applications.model; + +import lombok.Data; + +import java.util.Map; + +@Data +public class EditApplicationDataRequest { + private Map data; + +} diff --git a/src/main/java/com/crowdin/client/applications/model/UpdateOrRestoreApplicationDataRequest.java b/src/main/java/com/crowdin/client/applications/model/UpdateOrRestoreApplicationDataRequest.java new file mode 100644 index 000000000..bb6319456 --- /dev/null +++ b/src/main/java/com/crowdin/client/applications/model/UpdateOrRestoreApplicationDataRequest.java @@ -0,0 +1,10 @@ +package com.crowdin.client.applications.model; + +import lombok.Data; + +import java.util.Map; + +@Data +public class UpdateOrRestoreApplicationDataRequest { + private Map data; +} diff --git a/src/test/java/com/crowdin/client/applications/ApplicationsApiTest.java b/src/test/java/com/crowdin/client/applications/ApplicationsApiTest.java new file mode 100644 index 000000000..8699c909f --- /dev/null +++ b/src/test/java/com/crowdin/client/applications/ApplicationsApiTest.java @@ -0,0 +1,76 @@ +package com.crowdin.client.applications; + +import com.crowdin.client.applications.model.*; +import com.crowdin.client.core.model.ResponseObject; +import com.crowdin.client.framework.RequestMock; +import com.crowdin.client.framework.TestClient; +import org.apache.http.client.methods.*; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class ApplicationsApiTest extends TestClient { + + private final String applicationIdentifier = "identifier"; + private final String path = "path"; + private final String builtUrl = this.url + "/applications/" + applicationIdentifier + "/api/" + path; + + @Override + public List getMocks() { + return Arrays.asList( + RequestMock.build(builtUrl, HttpGet.METHOD_NAME, "api/applications/applicationData.json"), + RequestMock.build(builtUrl, HttpPut.METHOD_NAME, "api/applications/updateOrRestoreApplicationData.json", "api/applications/applicationData.json"), + RequestMock.build(builtUrl, HttpPost.METHOD_NAME, "api/applications/addApplicationData.json", "api/applications/applicationData.json"), + RequestMock.build(builtUrl, HttpDelete.METHOD_NAME), + RequestMock.build(builtUrl, HttpPatch.METHOD_NAME, "api/applications/editApplicationData.json", "api/applications/applicationData.json") + ); + } + + @Test + void getApplicationData() { + ResponseObject response = this.getApplicationsApi().getApplicationData(applicationIdentifier, path); + assertNotNull(response); + } + + @Test + void updateOrReplaceApplicationData() { + Map applicationDataPayload = new HashMap<>(); + applicationDataPayload.put("updateKey", "value"); + UpdateOrRestoreApplicationDataRequest updateOrRestoreApplicationDataRequest = new UpdateOrRestoreApplicationDataRequest(); + updateOrRestoreApplicationDataRequest.setData(applicationDataPayload); + ResponseObject response = this.getApplicationsApi().updateOrReplaceApplicationData(applicationIdentifier, path, updateOrRestoreApplicationDataRequest); + assertNotNull(response); + } + + @Test + void addApplicationData() { + Map addApplicationDataPayload = new HashMap<>(); + addApplicationDataPayload.put("Key1", "value"); + addApplicationDataPayload.put("Key2", "value"); + AddApplicationDataRequest applicationDataRequest = new AddApplicationDataRequest(); + applicationDataRequest.setData(addApplicationDataPayload); + ResponseObject response = this.getApplicationsApi().addApplicationData(applicationIdentifier, path, applicationDataRequest); + assertNotNull(response); + } + + @Test + void deleteApplicationData() { + this.getApplicationsApi().deleteApplicationData(applicationIdentifier, path); + } + + @Test + void editApplicationData() { + Map request = new HashMap<>(); + request.put("editKey", "editedValue"); + EditApplicationDataRequest editApplicationDataRequest = new EditApplicationDataRequest(); + editApplicationDataRequest.setData(request); + ResponseObject response = this.getApplicationsApi().editApplicationData(applicationIdentifier, path, editApplicationDataRequest); + assertNotNull(response); + } + +} \ No newline at end of file diff --git a/src/test/resources/api/applications/addApplicationData.json b/src/test/resources/api/applications/addApplicationData.json new file mode 100644 index 000000000..fcda50b6b --- /dev/null +++ b/src/test/resources/api/applications/addApplicationData.json @@ -0,0 +1,6 @@ +{ + "data": { + "Key1":"value", + "Key2":"value" + } +} \ No newline at end of file diff --git a/src/test/resources/api/applications/applicationData.json b/src/test/resources/api/applications/applicationData.json new file mode 100644 index 000000000..166eb27b9 --- /dev/null +++ b/src/test/resources/api/applications/applicationData.json @@ -0,0 +1,6 @@ +{ + "data": { + "customKey1": "value", + "customKey2": "value" + } +} diff --git a/src/test/resources/api/applications/applicationDataRequest.json b/src/test/resources/api/applications/applicationDataRequest.json new file mode 100644 index 000000000..166eb27b9 --- /dev/null +++ b/src/test/resources/api/applications/applicationDataRequest.json @@ -0,0 +1,6 @@ +{ + "data": { + "customKey1": "value", + "customKey2": "value" + } +} diff --git a/src/test/resources/api/applications/editApplicationData.json b/src/test/resources/api/applications/editApplicationData.json new file mode 100644 index 000000000..96207544b --- /dev/null +++ b/src/test/resources/api/applications/editApplicationData.json @@ -0,0 +1,5 @@ +{ + "data": { + "editKey": "editedValue" + } +} diff --git a/src/test/resources/api/applications/updateOrRestoreApplicationData.json b/src/test/resources/api/applications/updateOrRestoreApplicationData.json new file mode 100644 index 000000000..3945962e8 --- /dev/null +++ b/src/test/resources/api/applications/updateOrRestoreApplicationData.json @@ -0,0 +1,5 @@ +{ + "data": { + "updateKey": "value" + } +}