Skip to content

Commit

Permalink
feat: Add get, remove, create, send and list broadcast endpoints (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
kewynakshlley authored Jan 14, 2025
1 parent e2dc273 commit 8eb3e06
Show file tree
Hide file tree
Showing 14 changed files with 1,001 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/com/resend/Resend.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.resend.services.apikeys.ApiKeys;
import com.resend.services.audiences.Audiences;
import com.resend.services.batch.Batch;
import com.resend.services.broadcasts.Broadcasts;
import com.resend.services.contacts.Contacts;
import com.resend.services.domains.Domains;
import com.resend.services.emails.Emails;
Expand Down Expand Up @@ -79,4 +80,13 @@ public Audiences audiences() {
public Batch batch() {
return new Batch(apiKey);
}

/**
* Returns a Broadcasts object that can be used to interact with the Broadcasts service.
*
* @return A Broadcasts object.
*/
public Broadcasts broadcasts() {
return new Broadcasts(apiKey);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/resend/services/batch/Batch.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class Batch extends BaseService {
/**
* Constructsan instance of the {@code Batch} class.
* Constructs an instance of the {@code Batch} class.
*
* @param apiKey The apiKey used for authentication.
*/
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/com/resend/services/broadcasts/Broadcasts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.resend.services.broadcasts;

import com.resend.core.exception.ResendException;
import com.resend.core.net.AbstractHttpResponse;
import com.resend.core.net.HttpMethod;
import com.resend.core.service.BaseService;
import com.resend.services.broadcasts.model.*;
import okhttp3.MediaType;

/**
* Represents the Resend Broadcasts module.
*/
public class Broadcasts extends BaseService {

/**
* Constructs an instance of the {@code Broadcasts} class.
*
* @param apiKey The apiKey used for authentication.
*/
public Broadcasts(final String apiKey) {
super(apiKey);

}

/**
* Creates a Broadcast.
*
* @param createBroadcastOptions The Broadcast details.
* @return The details of the created broadcast.
* @throws ResendException If an error occurs during the Broadcast creation process.
*/
public CreateBroadcastResponseSuccess create(CreateBroadcastOptions createBroadcastOptions) throws ResendException {
String payload = super.resendMapper.writeValue(createBroadcastOptions);
AbstractHttpResponse<String> response = httpClient.perform("/broadcasts", super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException("Failed to create Broadcast: " + response.getCode() + " " + response.getBody());
}

String responseBody = response.getBody();
return resendMapper.readValue(responseBody, CreateBroadcastResponseSuccess.class);
}

/**
* Retrieves a broadcast by its unique identifier.
*
* @param id The unique identifier of the broadcast.
* @return The retrieved broadcast details.
* @throws ResendException If an error occurs while retrieving the broadcast.
*/
public GetBroadcastResponseSuccess get(String id) throws ResendException {
AbstractHttpResponse<String> response = this.httpClient.perform("/broadcasts/" +id, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new RuntimeException("Failed to retrieve broadcast: " + response.getCode() + " " + response.getBody());
}

String responseBody = response.getBody();

return resendMapper.readValue(responseBody, GetBroadcastResponseSuccess.class);
}

/**
* Sends a Broadcast.
*
* @param sendBroadcastOptions The Broadcast details.
* @param broadcastId The Broadcast id.
* @return The details of the broadcast to be sent.
* @throws ResendException If an error occurs during the Broadcast creation process.
*/
public SendBroadcastResponseSuccess send(SendBroadcastOptions sendBroadcastOptions, String broadcastId) throws ResendException {
String payload = super.resendMapper.writeValue(sendBroadcastOptions);
AbstractHttpResponse<String> response = httpClient.perform("/broadcasts/" +broadcastId + "/send", super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException("Failed to send broadcast: " + response.getCode() + " " + response.getBody());
}

String responseBody = response.getBody();
return resendMapper.readValue(responseBody, SendBroadcastResponseSuccess.class);
}

/**
* Deletes a broadcast based on the provided broadcast ID.
*
* @param id The unique identifier of the broadcast to delete.
* @return The RemoveBroadcastResponseSuccess with the details of the removed broadcast.
* @throws ResendException If an error occurs during the broadcast deletion process.
*/
public RemoveBroadcastResponseSuccess remove(String id) throws ResendException {
AbstractHttpResponse<String> response = httpClient.perform("/broadcasts/" +id, super.apiKey, HttpMethod.DELETE, "", null);

if (!response.isSuccessful()) {
throw new ResendException("Failed to delete broadcast: " + response.getCode() + " " + response.getBody());
}

String responseBody = response.getBody();

return resendMapper.readValue(responseBody, RemoveBroadcastResponseSuccess.class);
}

/**
* Retrieves a list of broadcasts and returns a List.
*
* @return A ListBroadcastsResponseSuccess containing the list of broadcasts.
* @throws ResendException If an error occurs during the broadcasts list retrieval process.
*/
public ListBroadcastsResponseSuccess list() throws ResendException {
AbstractHttpResponse<String> response = this.httpClient.perform("/broadcasts", super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException("Failed to retrieve broadcasts: " + response.getCode() + " " + response.getBody());
}

String responseBody = response.getBody();

return resendMapper.readValue(responseBody, ListBroadcastsResponseSuccess.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.resend.services.broadcasts.model;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Represents a base response for a broadcast.
*/
public abstract class BaseBroadcastResponse {
@JsonProperty("id")
private String id;

/**
* Default constructor
*/
public BaseBroadcastResponse() {

}

/**
* Constructs a base response for a broadcast.
*
* @param id The ID of the broadcast.
*/
public BaseBroadcastResponse(String id) {
this.id = id;
}

/**
* Get the object.
*
* @return The type of the data.
*/
public String getId() {
return id;
}
}
96 changes: 96 additions & 0 deletions src/main/java/com/resend/services/broadcasts/model/Broadcast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.resend.services.broadcasts.model;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Represents a broadcast with its metadata and delivery status.
*/
public class Broadcast {

@JsonProperty("id")
private String id;

@JsonProperty("audience_id")
private String audienceId;

@JsonProperty("status")
private String status;

@JsonProperty("created_at")
private String createdAt;

@JsonProperty("scheduled_at")
private String scheduledAt;

@JsonProperty("sent_at")
private String sentAt;

/**
* Default constructor
*/
public Broadcast() {

}

/**
* Constructs a new Broadcast instance.
*
* @param id Unique identifier for the broadcast.
* @param audienceId Identifier for the audience associated with this broadcast.
* @param status Current status of the broadcast (e.g., draft, sent, queued).
* @param createdAt Timestamp when the broadcast was created.
* @param scheduledAt Scheduled timestamp for sending the broadcast.
* @param sentAt Timestamp when the broadcast was sent.
*/
public Broadcast(String id, String audienceId,
String status, String createdAt, String scheduledAt, String sentAt) {
this.id = id;
this.audienceId = audienceId;
this.status = status;
this.createdAt = createdAt;
this.scheduledAt = scheduledAt;
this.sentAt = sentAt;
}

/**
* @return Unique identifier for the broadcast.
*/
public String getId() {
return id;
}

/**
* @return Identifier for the audience associated with this broadcast.
*/
public String getAudienceId() {
return audienceId;
}

/**
* @return Current status of the broadcast (e.g., draft, sent, queued).
*/
public String getStatus() {
return status;
}

/**
* @return Timestamp when the broadcast was created.
*/
public String getCreatedAt() {
return createdAt;
}

/**
* @return Scheduled timestamp for sending the broadcast.
*/
public String getScheduledAt() {
return scheduledAt;
}

/**
* @return Timestamp when the broadcast was sent.
*/
public String getSentAt() {
return sentAt;
}
}
Loading

0 comments on commit 8eb3e06

Please sign in to comment.