-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add get, remove, create, send and list broadcast endpoints (#38)
- Loading branch information
1 parent
e2dc273
commit 8eb3e06
Showing
14 changed files
with
1,001 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/com/resend/services/broadcasts/Broadcasts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/resend/services/broadcasts/model/BaseBroadcastResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
96
src/main/java/com/resend/services/broadcasts/model/Broadcast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.