Skip to content
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
18 changes: 18 additions & 0 deletions src/main/java/com/resend/services/broadcasts/Broadcasts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Comment thread
kewynakshlley marked this conversation as resolved.
AbstractHttpResponse<String> 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Comment thread
kewynakshlley marked this conversation as resolved.
@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;
}
}
17 changes: 17 additions & 0 deletions src/test/java/com/resend/services/broadcasts/BroadcastsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}," +
Expand Down Expand Up @@ -136,6 +139,20 @@ public void testDeleteBroadcast_Success() throws ResendException {
assertTrue(response.isDeleted());
}

@Test
public void testCancelBroadcast_Success() throws ResendException {
AbstractHttpResponse<String> 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<String> httpResponse = new AbstractHttpResponse<>(200, LIST_RESPONSE_JSON, true);
Expand Down
Loading