|
| 1 | +package io.mailtrap.examples.webhooks; |
| 2 | + |
| 3 | +import io.mailtrap.config.MailtrapConfig; |
| 4 | +import io.mailtrap.factory.MailtrapClientFactory; |
| 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 io.mailtrap.model.request.webhooks.CreateWebhookRequest; |
| 10 | +import io.mailtrap.model.request.webhooks.UpdateWebhookRequest; |
| 11 | +import io.mailtrap.model.request.webhooks.WebhookInput; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class WebhooksExample { |
| 16 | + |
| 17 | + private static final String TOKEN = "<YOUR MAILTRAP TOKEN>"; |
| 18 | + private static final long ACCOUNT_ID = 1L; |
| 19 | + private static final String WEBHOOK_URL = "https://example.com/webhooks/mailtrap"; |
| 20 | + private static final String UPDATED_WEBHOOK_URL = "https://example.com/webhooks/mailtrap/v2"; |
| 21 | + |
| 22 | + public static void main(String[] args) { |
| 23 | + final var config = new MailtrapConfig.Builder() |
| 24 | + .token(TOKEN) |
| 25 | + .build(); |
| 26 | + |
| 27 | + final var client = MailtrapClientFactory.createMailtrapClient(config); |
| 28 | + |
| 29 | + final var createRequest = new CreateWebhookRequest( |
| 30 | + WebhookInput.builder() |
| 31 | + .url(WEBHOOK_URL) |
| 32 | + .webhookType(WebhookType.EMAIL_SENDING) |
| 33 | + .active(true) |
| 34 | + .payloadFormat(WebhookPayloadFormat.JSON) |
| 35 | + .sendingStream(SendingStream.TRANSACTIONAL) |
| 36 | + .eventTypes(List.of( |
| 37 | + WebhookEventType.DELIVERY, |
| 38 | + WebhookEventType.BOUNCE, |
| 39 | + WebhookEventType.OPEN, |
| 40 | + WebhookEventType.CLICK, |
| 41 | + WebhookEventType.UNSUBSCRIBE)) |
| 42 | + .build()); |
| 43 | + |
| 44 | + // `signing_secret` is only returned on creation — store it securely. |
| 45 | + final var createResponse = client.generalApi().webhooks() |
| 46 | + .createWebhook(ACCOUNT_ID, createRequest); |
| 47 | + System.out.println(createResponse); |
| 48 | + |
| 49 | + final var webhookId = createResponse.getData().getId(); |
| 50 | + |
| 51 | + final var allWebhooks = client.generalApi().webhooks().getAllWebhooks(ACCOUNT_ID); |
| 52 | + System.out.println(allWebhooks); |
| 53 | + |
| 54 | + final var webhook = client.generalApi().webhooks().getWebhook(ACCOUNT_ID, webhookId); |
| 55 | + System.out.println(webhook); |
| 56 | + |
| 57 | + final var updateRequest = new UpdateWebhookRequest( |
| 58 | + WebhookInput.builder() |
| 59 | + .url(UPDATED_WEBHOOK_URL) |
| 60 | + .active(false) |
| 61 | + .eventTypes(List.of(WebhookEventType.DELIVERY, WebhookEventType.BOUNCE)) |
| 62 | + .build()); |
| 63 | + |
| 64 | + final var updateResponse = client.generalApi().webhooks() |
| 65 | + .updateWebhook(ACCOUNT_ID, webhookId, updateRequest); |
| 66 | + System.out.println(updateResponse); |
| 67 | + |
| 68 | + final var deleteResponse = client.generalApi().webhooks().deleteWebhook(ACCOUNT_ID, webhookId); |
| 69 | + System.out.println(deleteResponse); |
| 70 | + } |
| 71 | +} |
0 commit comments