Skip to content

Commit 254252b

Browse files
committed
Cover webhook endpoints
1 parent f15224b commit 254252b

23 files changed

Lines changed: 623 additions & 1 deletion
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.mailtrap.api.webhooks;
2+
3+
import io.mailtrap.model.request.webhooks.CreateWebhookRequest;
4+
import io.mailtrap.model.request.webhooks.UpdateWebhookRequest;
5+
import io.mailtrap.model.response.webhooks.CreateWebhookResponse;
6+
import io.mailtrap.model.response.webhooks.ListWebhooksResponse;
7+
import io.mailtrap.model.response.webhooks.WebhookResponse;
8+
9+
public interface Webhooks {
10+
11+
/**
12+
* Create a new webhook for the account. The response includes the
13+
* {@code signing_secret} used to verify webhook signatures (only returned on creation).
14+
*
15+
* @param accountId unique account ID
16+
* @param request webhook configuration
17+
* @return created webhook including the signing secret
18+
*/
19+
CreateWebhookResponse createWebhook(long accountId, CreateWebhookRequest request);
20+
21+
/**
22+
* List all webhooks for the account.
23+
*
24+
* @param accountId unique account ID
25+
* @return webhooks
26+
*/
27+
ListWebhooksResponse getAllWebhooks(long accountId);
28+
29+
/**
30+
* Get a single webhook by ID.
31+
*
32+
* @param accountId unique account ID
33+
* @param webhookId webhook ID
34+
* @return webhook details
35+
*/
36+
WebhookResponse getWebhook(long accountId, long webhookId);
37+
38+
/**
39+
* Update an existing webhook.
40+
*
41+
* @param accountId unique account ID
42+
* @param webhookId webhook ID
43+
* @param request fields to update
44+
* @return updated webhook
45+
*/
46+
WebhookResponse updateWebhook(long accountId, long webhookId, UpdateWebhookRequest request);
47+
48+
/**
49+
* Permanently delete a webhook.
50+
*
51+
* @param accountId unique account ID
52+
* @param webhookId webhook ID
53+
* @return the deleted webhook
54+
*/
55+
WebhookResponse deleteWebhook(long accountId, long webhookId);
56+
57+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.mailtrap.api.webhooks;
2+
3+
import io.mailtrap.Constants;
4+
import io.mailtrap.api.apiresource.ApiResource;
5+
import io.mailtrap.config.MailtrapConfig;
6+
import io.mailtrap.http.RequestData;
7+
import io.mailtrap.model.request.webhooks.CreateWebhookRequest;
8+
import io.mailtrap.model.request.webhooks.UpdateWebhookRequest;
9+
import io.mailtrap.model.response.webhooks.CreateWebhookResponse;
10+
import io.mailtrap.model.response.webhooks.ListWebhooksResponse;
11+
import io.mailtrap.model.response.webhooks.WebhookResponse;
12+
13+
public class WebhooksImpl extends ApiResource implements Webhooks {
14+
15+
public WebhooksImpl(final MailtrapConfig config) {
16+
super(config);
17+
this.apiHost = Constants.GENERAL_HOST;
18+
}
19+
20+
@Override
21+
public CreateWebhookResponse createWebhook(final long accountId, final CreateWebhookRequest request) {
22+
return httpClient.post(
23+
String.format(apiHost + "/api/accounts/%d/webhooks", accountId),
24+
request,
25+
new RequestData(),
26+
CreateWebhookResponse.class
27+
);
28+
}
29+
30+
@Override
31+
public ListWebhooksResponse getAllWebhooks(final long accountId) {
32+
return httpClient.get(
33+
String.format(apiHost + "/api/accounts/%d/webhooks", accountId),
34+
new RequestData(),
35+
ListWebhooksResponse.class
36+
);
37+
}
38+
39+
@Override
40+
public WebhookResponse getWebhook(final long accountId, final long webhookId) {
41+
return httpClient.get(
42+
String.format(apiHost + "/api/accounts/%d/webhooks/%d", accountId, webhookId),
43+
new RequestData(),
44+
WebhookResponse.class
45+
);
46+
}
47+
48+
@Override
49+
public WebhookResponse updateWebhook(final long accountId, final long webhookId, final UpdateWebhookRequest request) {
50+
return httpClient.patch(
51+
String.format(apiHost + "/api/accounts/%d/webhooks/%d", accountId, webhookId),
52+
request,
53+
new RequestData(),
54+
WebhookResponse.class
55+
);
56+
}
57+
58+
@Override
59+
public WebhookResponse deleteWebhook(final long accountId, final long webhookId) {
60+
return httpClient.delete(
61+
String.format(apiHost + "/api/accounts/%d/webhooks/%d", accountId, webhookId),
62+
new RequestData(),
63+
WebhookResponse.class
64+
);
65+
}
66+
}

src/main/java/io/mailtrap/client/api/MailtrapGeneralApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.mailtrap.api.apitokens.ApiTokens;
66
import io.mailtrap.api.billing.Billing;
77
import io.mailtrap.api.permissions.Permissions;
8+
import io.mailtrap.api.webhooks.Webhooks;
89
import lombok.Getter;
910
import lombok.RequiredArgsConstructor;
1011
import lombok.experimental.Accessors;
@@ -21,4 +22,5 @@ public class MailtrapGeneralApi {
2122
private final ApiTokens apiTokens;
2223
private final Billing billing;
2324
private final Permissions permissions;
25+
private final Webhooks webhooks;
2426
}

src/main/java/io/mailtrap/factory/MailtrapClientFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.mailtrap.api.subaccounts.SubAccountsImpl;
2626
import io.mailtrap.api.suppressions.SuppressionsImpl;
2727
import io.mailtrap.api.testingemails.TestingEmailsImpl;
28+
import io.mailtrap.api.webhooks.WebhooksImpl;
2829
import io.mailtrap.client.MailtrapClient;
2930
import io.mailtrap.client.api.*;
3031
import io.mailtrap.config.MailtrapConfig;
@@ -103,8 +104,9 @@ private static MailtrapGeneralApi createGeneralApi(final MailtrapConfig config)
103104
final var apiTokens = new ApiTokensImpl(config);
104105
final var billing = new BillingImpl(config);
105106
final var permissions = new PermissionsImpl(config);
107+
final var webhooks = new WebhooksImpl(config);
106108

107-
return new MailtrapGeneralApi(accountAccess, accounts, apiTokens, billing, permissions);
109+
return new MailtrapGeneralApi(accountAccess, accounts, apiTokens, billing, permissions, webhooks);
108110
}
109111

110112
private static MailtrapEmailSendingApi createSendingApi(final MailtrapConfig config) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.mailtrap.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public enum WebhookEventType {
7+
DELIVERY("delivery"),
8+
SOFT_BOUNCE("soft_bounce"),
9+
BOUNCE("bounce"),
10+
SUSPENSION("suspension"),
11+
UNSUBSCRIBE("unsubscribe"),
12+
OPEN("open"),
13+
SPAM_COMPLAINT("spam_complaint"),
14+
CLICK("click"),
15+
REJECT("reject");
16+
17+
private final String value;
18+
19+
WebhookEventType(String value) {
20+
this.value = value;
21+
}
22+
23+
@JsonValue
24+
public String getValue() {
25+
return value;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return value;
31+
}
32+
33+
@JsonCreator
34+
public static WebhookEventType fromValue(String value) {
35+
for (WebhookEventType type : WebhookEventType.values()) {
36+
if (type.value.equalsIgnoreCase(value)) {
37+
return type;
38+
}
39+
}
40+
throw new IllegalArgumentException("Unknown value: " + value);
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.mailtrap.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public enum WebhookPayloadFormat {
7+
JSON("json"),
8+
JSONLINES("jsonlines");
9+
10+
private final String value;
11+
12+
WebhookPayloadFormat(String value) {
13+
this.value = value;
14+
}
15+
16+
@JsonValue
17+
public String getValue() {
18+
return value;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return value;
24+
}
25+
26+
@JsonCreator
27+
public static WebhookPayloadFormat fromValue(String value) {
28+
for (WebhookPayloadFormat format : WebhookPayloadFormat.values()) {
29+
if (format.value.equalsIgnoreCase(value)) {
30+
return format;
31+
}
32+
}
33+
throw new IllegalArgumentException("Unknown value: " + value);
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.mailtrap.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public enum WebhookType {
7+
EMAIL_SENDING("email_sending"),
8+
AUDIT_LOG("audit_log"),
9+
CAMPAIGNS("campaigns");
10+
11+
private final String value;
12+
13+
WebhookType(String value) {
14+
this.value = value;
15+
}
16+
17+
@JsonValue
18+
public String getValue() {
19+
return value;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return value;
25+
}
26+
27+
@JsonCreator
28+
public static WebhookType fromValue(String value) {
29+
for (WebhookType type : WebhookType.values()) {
30+
if (type.value.equalsIgnoreCase(value)) {
31+
return type;
32+
}
33+
}
34+
throw new IllegalArgumentException("Unknown value: " + value);
35+
}
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.mailtrap.model.request.webhooks;
2+
3+
import io.mailtrap.model.AbstractModel;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
9+
@Getter
10+
@Setter
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
public class CreateWebhookRequest extends AbstractModel {
14+
15+
private WebhookInput webhook;
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.mailtrap.model.request.webhooks;
2+
3+
import io.mailtrap.model.AbstractModel;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
9+
@Getter
10+
@Setter
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
public class UpdateWebhookRequest extends AbstractModel {
14+
15+
private WebhookInput webhook;
16+
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.mailtrap.model.request.webhooks;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import io.mailtrap.model.SendingStream;
6+
import io.mailtrap.model.WebhookEventType;
7+
import io.mailtrap.model.WebhookPayloadFormat;
8+
import io.mailtrap.model.WebhookType;
9+
import lombok.AllArgsConstructor;
10+
import lombok.Builder;
11+
import lombok.Data;
12+
import lombok.NoArgsConstructor;
13+
14+
import java.util.List;
15+
16+
@Data
17+
@Builder
18+
@NoArgsConstructor
19+
@AllArgsConstructor
20+
@JsonInclude(JsonInclude.Include.NON_NULL)
21+
public class WebhookInput {
22+
23+
private String url;
24+
25+
@JsonProperty("webhook_type")
26+
private WebhookType webhookType;
27+
28+
private Boolean active;
29+
30+
@JsonProperty("payload_format")
31+
private WebhookPayloadFormat payloadFormat;
32+
33+
@JsonProperty("sending_stream")
34+
private SendingStream sendingStream;
35+
36+
@JsonProperty("event_types")
37+
private List<WebhookEventType> eventTypes;
38+
39+
@JsonProperty("domain_id")
40+
private Long domainId;
41+
42+
}

0 commit comments

Comments
 (0)