Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add Organization Webhooks APIs support #161

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.OrganizationWebhookResponseList;
import com.crowdin.client.webhooks.model.OrganizationWebhookResponseObject;
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)
);
OrganizationWebhookResponseList responseList = this.httpClient.get(baseUrl, new HttpRequestConfig(queryParams), OrganizationWebhookResponseList.class);
return OrganizationWebhookResponseList.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 {
OrganizationWebhookResponseObject responseObject = this.httpClient.post(baseUrl, request, new HttpRequestConfig(), OrganizationWebhookResponseObject.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);
OrganizationWebhookResponseObject responseObject = this.httpClient.get(url, new HttpRequestConfig(), OrganizationWebhookResponseObject.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);
OrganizationWebhookResponseObject responseObject = this.httpClient.patch(url, request, new HttpRequestConfig(), OrganizationWebhookResponseObject.class);
return ResponseObject.of(responseObject.getData());
}

private String formUrl_webhookId(Long organizationWebhookId) {
return this.url + "/webhooks/" + organizationWebhookId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.crowdin.client.webhooks.model;

import lombok.Data;

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

@Data
public class AddOrganizationWebhookRequest {
private String name;
private String url;
private List<OrganizationEvent> events;
private RequestType requestType;
private Boolean isActive;
private Boolean batchingEnabled;
private ContentType contentType;
private Map<String, String> headers;
private Object payload;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.crowdin.client.webhooks.model;

import com.crowdin.client.core.model.EnumConverter;

public enum OrganizationEvent implements EnumConverter<OrganizationEvent> {
GROUP_CREATED, GROUP_DELETED,
PROJECT_CREATED, PROJECT_DELETED;

public static OrganizationEvent from(String value) {
return OrganizationEvent.valueOf(value.toUpperCase().replace(".", "_"));
}

@Override
public Object to(OrganizationEvent v) {
return v.name().toLowerCase().replace("_", ".");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.crowdin.client.webhooks.model;

import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
public class OrganizationWebhook {
private Long id;
private String name;
private String url;
private List<OrganizationEvent> events;
private Map<String, String> headers;
private Object payload;
private Boolean isActive;
private Boolean batchingEnabled;
private RequestType requestType;
private ContentType contentType;
private Date createdAt;
private Date updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.crowdin.client.webhooks.model;

import com.crowdin.client.core.model.Pagination;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class OrganizationWebhookResponseList {
private List<OrganizationWebhookResponseObject> data;
private Pagination pagination;

public static ResponseList<OrganizationWebhook> to(OrganizationWebhookResponseList responseList) {
return ResponseList.of(
responseList.getData().stream()
.map(OrganizationWebhookResponseObject::getData)
.map(ResponseObject::of)
.collect(Collectors.toList()),
responseList.getPagination()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.crowdin.client.webhooks.model;

import lombok.Data;

@Data
public class OrganizationWebhookResponseObject {
private OrganizationWebhook data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.crowdin.client.core.model.EscapeQuotesMode;
import com.crowdin.client.core.model.EscapeSpecialCharsMode;
import com.crowdin.client.core.model.JsonFileType;
import com.crowdin.client.webhooks.model.OrganizationEvent;

import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -55,6 +56,19 @@ public void jsonFileType() {
deserializeAndCompare("nestjs_i18n", JsonFileType.NESTJS_I18N);
}

@Test
public void webhooks_OrganizationEvent() {
serializeAndCompare(OrganizationEvent.GROUP_CREATED, "group.created");
serializeAndCompare(OrganizationEvent.GROUP_DELETED, "group.deleted");
serializeAndCompare(OrganizationEvent.PROJECT_CREATED, "project.created");
serializeAndCompare(OrganizationEvent.PROJECT_DELETED, "project.deleted");

deserializeAndCompare("group.created", OrganizationEvent.GROUP_CREATED);
deserializeAndCompare("group.deleted", OrganizationEvent.GROUP_DELETED);
deserializeAndCompare("project.created", OrganizationEvent.PROJECT_CREATED);
deserializeAndCompare("project.deleted", OrganizationEvent.PROJECT_DELETED);
}

//</editor-fold>

@SneakyThrows
Expand Down
Loading