diff --git a/src/main/java/com/resend/services/broadcasts/Broadcasts.java b/src/main/java/com/resend/services/broadcasts/Broadcasts.java index 7723067..945ec9f 100644 --- a/src/main/java/com/resend/services/broadcasts/Broadcasts.java +++ b/src/main/java/com/resend/services/broadcasts/Broadcasts.java @@ -86,6 +86,24 @@ public SendBroadcastResponseSuccess send(SendBroadcastOptions sendBroadcastOptio return resendMapper.readValue(responseBody, SendBroadcastResponseSuccess.class); } + /** + * Cancels a queued or scheduled broadcast. + * + * @param id The unique identifier of the broadcast to cancel. + * @return The CancelBroadcastResponseSuccess with the details of the canceled broadcast. + * @throws ResendException If an error occurs during the broadcast cancellation process. + */ + public CancelBroadcastResponseSuccess cancel(String id) throws ResendException { + AbstractHttpResponse response = httpClient.perform("/broadcasts/" + id + "/cancel", super.apiKey, HttpMethod.POST, "", MediaType.get("application/json")); + + if (!response.isSuccessful()) { + throw new ResendException(response.getCode(), response.getBody()); + } + + String responseBody = response.getBody(); + return resendMapper.readValue(responseBody, CancelBroadcastResponseSuccess.class); + } + /** * Deletes a broadcast based on the provided broadcast ID. * diff --git a/src/main/java/com/resend/services/broadcasts/model/CancelBroadcastResponseSuccess.java b/src/main/java/com/resend/services/broadcasts/model/CancelBroadcastResponseSuccess.java new file mode 100644 index 0000000..b5c4280 --- /dev/null +++ b/src/main/java/com/resend/services/broadcasts/model/CancelBroadcastResponseSuccess.java @@ -0,0 +1,39 @@ +package com.resend.services.broadcasts.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Represents the response for a successful broadcast cancellation. + * Extends the BaseBroadcastResponse class. + */ +public class CancelBroadcastResponseSuccess extends BaseBroadcastResponse { + @JsonProperty("object") + private String object; + + /** + * Default constructor + */ + public CancelBroadcastResponseSuccess() { + + } + + /** + * Constructs a successful response for cancelling a broadcast. + * + * @param id The ID of the broadcast. + * @param object The object of the broadcast. + */ + public CancelBroadcastResponseSuccess(String id, String object) { + super(id); + this.object = object; + } + + /** + * Get the object. + * + * @return The type of the data. + */ + public String getObject() { + return object; + } +} diff --git a/src/test/java/com/resend/services/broadcasts/BroadcastsTest.java b/src/test/java/com/resend/services/broadcasts/BroadcastsTest.java index f88acf7..52bbb66 100644 --- a/src/test/java/com/resend/services/broadcasts/BroadcastsTest.java +++ b/src/test/java/com/resend/services/broadcasts/BroadcastsTest.java @@ -42,6 +42,9 @@ public class BroadcastsTest { private static final String REMOVE_RESPONSE_JSON = "{\"id\":\"" + GET_BROADCAST_ID + "\",\"object\":\"object\",\"deleted\":true}"; + private static final String CANCEL_RESPONSE_JSON = + "{\"id\":\"" + GET_BROADCAST_ID + "\",\"object\":\"broadcast\"}"; + private static final String LIST_RESPONSE_JSON = "{\"object\":\"list\",\"has_more\":true,\"data\":[" + "{\"id\":\"1\",\"audience_id\":\"" + AUDIENCE_ID + "\",\"status\":\"draft\",\"created_at\":\"2024-12-01 19:32:22.98+00\"}," + @@ -136,6 +139,20 @@ public void testDeleteBroadcast_Success() throws ResendException { assertTrue(response.isDeleted()); } + @Test + public void testCancelBroadcast_Success() throws ResendException { + AbstractHttpResponse httpResponse = new AbstractHttpResponse<>(200, CANCEL_RESPONSE_JSON, true); + + when(httpClient.perform(eq("/broadcasts/" + GET_BROADCAST_ID + "/cancel"), anyString(), eq(HttpMethod.POST), eq(""), any(MediaType.class))) + .thenReturn(httpResponse); + + CancelBroadcastResponseSuccess response = broadcasts.cancel(GET_BROADCAST_ID); + + assertNotNull(response); + assertEquals(GET_BROADCAST_ID, response.getId()); + assertEquals("broadcast", response.getObject()); + } + @Test public void testListBroadcasts_Success() throws ResendException { AbstractHttpResponse httpResponse = new AbstractHttpResponse<>(200, LIST_RESPONSE_JSON, true);