Skip to content

Commit

Permalink
refactor(api): Organization Webhooks: refactoring before merge
Browse files Browse the repository at this point in the history
  • Loading branch information
innomaxx committed Jul 26, 2023
1 parent 757cbc6 commit ca10ce8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 95 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/crowdin/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.crowdin.client.translationstatus.TranslationStatusApi;
import com.crowdin.client.users.UsersApi;
import com.crowdin.client.vendors.VendorsApi;
import com.crowdin.client.webhooks.OrganizationWebhooksApi;
import com.crowdin.client.webhooks.WebhooksApi;
import com.crowdin.client.workflows.WorkflowsApi;
import lombok.Getter;
Expand All @@ -52,6 +53,7 @@ public class Client extends CrowdinApi {
private final UsersApi usersApi;
private final VendorsApi vendorsApi;
private final WebhooksApi webhooksApi;
private final OrganizationWebhooksApi organizationWebhooksApi;
private final TeamsApi teamsApi;
private final DistributionsApi distributionsApi;
private final DictionariesApi dictionariesApi;
Expand Down Expand Up @@ -80,6 +82,7 @@ public Client(Credentials credentials) {
this.usersApi = new UsersApi(credentials);
this.vendorsApi = new VendorsApi(credentials);
this.webhooksApi = new WebhooksApi(credentials);
this.organizationWebhooksApi = new OrganizationWebhooksApi(credentials);
this.teamsApi = new TeamsApi(credentials);
this.distributionsApi = new DistributionsApi(credentials);
this.dictionariesApi = new DictionariesApi(credentials);
Expand Down Expand Up @@ -109,6 +112,7 @@ public Client(Credentials credentials, ClientConfig clientConfig) {
this.usersApi = new UsersApi(credentials, clientConfig);
this.vendorsApi = new VendorsApi(credentials, clientConfig);
this.webhooksApi = new WebhooksApi(credentials, clientConfig);
this.organizationWebhooksApi = new OrganizationWebhooksApi(credentials, clientConfig);
this.teamsApi = new TeamsApi(credentials, clientConfig);
this.distributionsApi = new DistributionsApi(credentials, clientConfig);
this.dictionariesApi = new DictionariesApi(credentials, clientConfig);
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/com/crowdin/client/webhooks/OrganizationWebhooksApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.crowdin.client.webhooks;

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.PatchRequest;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.webhooks.model.AddOrganizationWebhookRequest;
import com.crowdin.client.webhooks.model.OrgWebhookResponseList;
import com.crowdin.client.webhooks.model.OrgWebhookResponseObject;
import com.crowdin.client.webhooks.model.OrganizationWebhook;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public class OrganizationWebhooksApi extends CrowdinApi {
public OrganizationWebhooksApi(Credentials credentials) {
super(credentials);
}

public OrganizationWebhooksApi(Credentials credentials, ClientConfig clientConfig) {
super(credentials, clientConfig);
}

private final String baseUrl = "/webhooks";

/**
* @param limit maximum number of items to retrieve (default 25)
* @param offset starting offset in the collection (default 0)
* @return list of organization webhooks
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.getMany" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.getMany" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<OrganizationWebhook> listWebhooks(Integer limit, Integer offset) throws HttpException, HttpBadRequestException {
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
"limit", Optional.ofNullable(limit),
"offset", Optional.ofNullable(offset)
);
OrgWebhookResponseList responseList = this.httpClient.get(baseUrl, new HttpRequestConfig(queryParams), OrgWebhookResponseList.class);
return OrgWebhookResponseList.to(responseList);
}

/**
* @param request request object
* @return newly created organization webhook
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.post" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.post" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<OrganizationWebhook> addWebhook(AddOrganizationWebhookRequest request) throws HttpException, HttpBadRequestException {
OrgWebhookResponseObject responseObject = this.httpClient.post(baseUrl, request, new HttpRequestConfig(), OrgWebhookResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

/**
* @param organizationWebhookId organization webhook identifier
* @return organization webhook
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.get" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.get" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<OrganizationWebhook> getWebhook(Long organizationWebhookId) throws HttpException, HttpBadRequestException {
String url = formUrl_webhookId(organizationWebhookId);
OrgWebhookResponseObject responseObject = this.httpClient.get(url, new HttpRequestConfig(), OrgWebhookResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

/**
* @param organizationWebhookId organization webhook identifier
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.delete" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.delete" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public void deleteWebhook(Long organizationWebhookId) throws HttpException, HttpBadRequestException {
String url = formUrl_webhookId(organizationWebhookId);
this.httpClient.delete(url, new HttpRequestConfig(), Void.class);
}

/**
* @param organizationWebhookId organization webhook identifier
* @param request request object
* @return updated organization webhook
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.patch" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.patch" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<OrganizationWebhook> editWebhook(Long organizationWebhookId, List<PatchRequest> request) throws HttpException, HttpBadRequestException {
String url = formUrl_webhookId(organizationWebhookId);
OrgWebhookResponseObject responseObject = this.httpClient.patch(url, request, new HttpRequestConfig(), OrgWebhookResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

private String formUrl_webhookId(Long organizationWebhookId) {
return this.url + "/webhooks/" + organizationWebhookId;
}
}
86 changes: 0 additions & 86 deletions src/main/java/com/crowdin/client/webhooks/WebhooksApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
import com.crowdin.client.core.model.PatchRequest;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.webhooks.model.AddOrgWebhookRequest;
import com.crowdin.client.webhooks.model.AddWebhookRequest;
import com.crowdin.client.webhooks.model.OrgWebhookResponseList;
import com.crowdin.client.webhooks.model.OrgWebhookResponseObject;
import com.crowdin.client.webhooks.model.OrganizationWebhook;
import com.crowdin.client.webhooks.model.Webhook;
import com.crowdin.client.webhooks.model.WebhookResponseList;
import com.crowdin.client.webhooks.model.WebhookResponseObject;
Expand Down Expand Up @@ -104,86 +100,4 @@ public ResponseObject<Webhook> editWebhook(Long projectId, Long webhookId, List<
WebhookResponseObject webhookResponseObject = this.httpClient.patch(this.url + "/projects/" + projectId + "/webhooks/" + webhookId, request, new HttpRequestConfig(), WebhookResponseObject.class);
return ResponseObject.of(webhookResponseObject.getData());
}

//<editor-fold desc="Organization Webhooks">

private final String orgWebhooksBaseUrl = "/webhooks";

/**
* @param limit maximum number of items to retrieve (default 25)
* @param offset starting offset in the collection (default 0)
* @return list of organization webhooks
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.getMany" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.getMany" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<OrganizationWebhook> listOrgWebhooks(Integer limit, Integer offset) throws HttpException, HttpBadRequestException {
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
"limit", Optional.ofNullable(limit),
"offset", Optional.ofNullable(offset)
);
OrgWebhookResponseList responseList = this.httpClient.get(orgWebhooksBaseUrl, new HttpRequestConfig(queryParams), OrgWebhookResponseList.class);
return OrgWebhookResponseList.to(responseList);
}

/**
* @param request request object
* @return newly created organization webhook
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.post" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.post" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<OrganizationWebhook> addOrgWebhook(AddOrgWebhookRequest request) throws HttpException, HttpBadRequestException {
OrgWebhookResponseObject responseObject = this.httpClient.post(orgWebhooksBaseUrl, request, new HttpRequestConfig(), OrgWebhookResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

/**
* @param organizationWebhookId organization webhook identifier
* @return organization webhook
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.get" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.get" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<OrganizationWebhook> getOrgWebhook(Long organizationWebhookId) throws HttpException, HttpBadRequestException {
String url = formUrl_orgWebhookId(organizationWebhookId);
OrgWebhookResponseObject responseObject = this.httpClient.get(url, new HttpRequestConfig(), OrgWebhookResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

/**
* @param organizationWebhookId organization webhook identifier
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.delete" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.delete" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public void deleteOrgWebhook(Long organizationWebhookId) throws HttpException, HttpBadRequestException {
String url = formUrl_orgWebhookId(organizationWebhookId);
this.httpClient.delete(url, new HttpRequestConfig(), Void.class);
}

/**
* @param organizationWebhookId organization webhook identifier
* @param request request object
* @return updated organization webhook
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.webhooks.patch" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.webhooks.patch" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<OrganizationWebhook> editOrgWebhook(Long organizationWebhookId, List<PatchRequest> request) throws HttpException, HttpBadRequestException {
String url = formUrl_orgWebhookId(organizationWebhookId);
OrgWebhookResponseObject responseObject = this.httpClient.patch(url, request, new HttpRequestConfig(), OrgWebhookResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

private String formUrl_orgWebhookId(Long organizationWebhookId) {
return this.url + "/webhooks/" + organizationWebhookId;
}

//</editor-fold>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Map;

@Data
public class AddOrgWebhookRequest {
public class AddOrganizationWebhookRequest {
private String name;
private String url;
private List<OrganizationEvent> events;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import com.crowdin.client.webhooks.model.AddOrgWebhookRequest;
import com.crowdin.client.webhooks.model.AddOrganizationWebhookRequest;
import com.crowdin.client.webhooks.model.ContentType;
import com.crowdin.client.webhooks.model.OrganizationEvent;
import com.crowdin.client.webhooks.model.OrganizationWebhook;
Expand Down Expand Up @@ -78,13 +78,13 @@ private String formUrl_orgWebhookId(Long organizationWebhookId) {

@Test
public void listWebhooksTest() {
ResponseList<OrganizationWebhook> responseList = this.getWebhooksApi().listOrgWebhooks(20, 10);
ResponseList<OrganizationWebhook> responseList = this.getOrganizationWebhooksApi().listWebhooks(20, 10);
assertWebhook(responseList.getData().get(0).getData());
}

@Test
public void addWebhookTest() {
AddOrgWebhookRequest request = new AddOrgWebhookRequest() {{
AddOrganizationWebhookRequest request = new AddOrganizationWebhookRequest() {{
setName("Proofread");
setUrl("https://test.com");
setEvents(new ArrayList<OrganizationEvent>() {{
Expand All @@ -100,22 +100,21 @@ public void addWebhookTest() {
setHeaders(new HashMap<String, String>() {{
put("Authorization", "key");
}});
// setPayload();
}};

ResponseObject<OrganizationWebhook> response = this.getWebhooksApi().addOrgWebhook(request);
ResponseObject<OrganizationWebhook> response = this.getOrganizationWebhooksApi().addWebhook(request);
assertWebhook(response.getData());
}

@Test
public void getWebhookTest() {
ResponseObject<OrganizationWebhook> response = this.getWebhooksApi().getOrgWebhook(organizationWebhookId);
ResponseObject<OrganizationWebhook> response = this.getOrganizationWebhooksApi().getWebhook(organizationWebhookId);
assertWebhook(response.getData());
}

@Test
public void deleteWebhookTest() {
this.getWebhooksApi().deleteOrgWebhook(organizationWebhookId);
this.getOrganizationWebhooksApi().deleteWebhook(organizationWebhookId);
}

@Test
Expand Down Expand Up @@ -146,7 +145,7 @@ public void editWebhookTest() {
}});
}};

ResponseObject<OrganizationWebhook> response = this.getWebhooksApi().editOrgWebhook(organizationWebhookId, request);
ResponseObject<OrganizationWebhook> response = this.getOrganizationWebhooksApi().editWebhook(organizationWebhookId, request);
assertWebhook(response.getData());
}

Expand Down

0 comments on commit ca10ce8

Please sign in to comment.