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
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 @@ -7,6 +7,7 @@
import com.resend.services.contacts.Contacts;
import com.resend.services.domains.Domains;
import com.resend.services.emails.Emails;
import com.resend.services.topics.Topics;
import com.resend.services.templates.Templates;

/**
Expand Down Expand Up @@ -91,6 +92,15 @@ public Broadcasts broadcasts() {
return new Broadcasts(apiKey);
}

/**
* Returns a Topics object that can be used to interact with the Topics service.
*
* @return A Topics object.
*/
public Topics topics() {
return new Topics(apiKey);
}

/**
* Returns a Templates object that can be used to interact with the Templates service.
*
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/com/resend/services/topics/Topics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.resend.services.topics;

import com.resend.core.exception.ResendException;
import com.resend.core.helper.URLHelper;
import com.resend.core.net.AbstractHttpResponse;
import com.resend.core.net.HttpMethod;
import com.resend.core.net.ListParams;
import com.resend.core.service.BaseService;
import com.resend.services.topics.model.*;
import okhttp3.MediaType;

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

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

/**
* Creates a new topic.
*
* @param createTopicOptions The request containing topic details.
* @return The response indicating the status of the topic creation.
* @throws ResendException If an error occurs while creating the topic.
*/
public CreateTopicResponseSuccess create(CreateTopicOptions createTopicOptions) throws ResendException {
String payload = super.resendMapper.writeValue(createTopicOptions);
AbstractHttpResponse<String> response = super.httpClient.perform("/topics", super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

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

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

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

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

/**
* Updates a topic by its unique identifier.
*
* @param topicId The unique identifier of the topic.
* @param updateTopicOptions The new data for the topic.
* @return The response indicating the status of the topic update.
* @throws ResendException If an error occurs while updating the topic.
*/
public UpdateTopicResponseSuccess update(String topicId, UpdateTopicOptions updateTopicOptions) throws ResendException {
String payload = super.resendMapper.writeValue(updateTopicOptions);
AbstractHttpResponse<String> response = this.httpClient.perform("/topics/" + topicId, super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

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

/**
* Removes a topic by its unique identifier.
*
* @param topicId The unique identifier of the topic.
* @return The response indicating the status of the topic removal.
* @throws ResendException If an error occurs while removing the topic.
*/
public RemoveTopicResponseSuccess remove(String topicId) throws ResendException {
AbstractHttpResponse<String> response = this.httpClient.perform("/topics/" + topicId, super.apiKey, HttpMethod.DELETE, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

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

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

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

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

/**
* Retrieves a paginated list of topics and returns a List.
*
* @param params The params used to customize the list.
* @return A ListTopicsResponse containing the paginated list of topics.
* @throws ResendException If an error occurs during the topics list retrieval process.
*/
public ListTopicsResponseSuccess list(ListParams params) throws ResendException {
String pathWithQuery = "/topics" + URLHelper.parse(params);
AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));

if (!response.isSuccessful()) {
throw new ResendException(response.getCode(), response.getBody());
}

String responseBody = response.getBody();
return resendMapper.readValue(responseBody, ListTopicsResponseSuccess.class);
}
}
153 changes: 153 additions & 0 deletions src/main/java/com/resend/services/topics/model/AbstractTopic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.resend.services.topics.model;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Abstract base class representing common topic attributes.
* This class contains fields shared across different topic response types.
*/
public abstract class AbstractTopic {

/**
* The unique identifier of the topic.
*/
@JsonProperty("id")
private String id;

/**
* The name of the topic.
*/
@JsonProperty("name")
private String name;

/**
* The description of the topic.
*/
@JsonProperty("description")
private String description;

/**
* The default subscription preference for new contacts.
*/
@JsonProperty("default_subscription")
private String defaultSubscription;

/**
* The creation timestamp of the topic.
*/
@JsonProperty("created_at")
private String createdAt;

/**
* Default constructor for creating an empty AbstractTopic object.
*/
public AbstractTopic() {
}

/**
* Constructs an AbstractTopic with the provided attributes.
*
* @param id The unique identifier of the topic.
* @param name The name of the topic.
* @param description The description of the topic.
* @param defaultSubscription The default subscription preference.
* @param createdAt The creation timestamp.
*/
public AbstractTopic(String id, String name, String description, String defaultSubscription, String createdAt) {
this.id = id;
this.name = name;
this.description = description;
this.defaultSubscription = defaultSubscription;
this.createdAt = createdAt;
}

/**
* Gets the unique identifier of the topic.
*
* @return The topic ID.
*/
public String getId() {
return id;
}

/**
* Sets the unique identifier of the topic.
*
* @param id The topic ID to set.
*/
public void setId(String id) {
this.id = id;
}

/**
* Gets the name of the topic.
*
* @return The topic name.
*/
public String getName() {
return name;
}

/**
* Sets the name of the topic.
*
* @param name The topic name to set.
*/
public void setName(String name) {
this.name = name;
}

/**
* Gets the description of the topic.
*
* @return The topic description.
*/
public String getDescription() {
return description;
}

/**
* Sets the description of the topic.
*
* @param description The topic description to set.
*/
public void setDescription(String description) {
this.description = description;
}

/**
* Gets the default subscription preference for new contacts.
*
* @return The default subscription preference.
*/
public String getDefaultSubscription() {
return defaultSubscription;
}

/**
* Sets the default subscription preference for new contacts.
*
* @param defaultSubscription The default subscription preference to set.
*/
public void setDefaultSubscription(String defaultSubscription) {
this.defaultSubscription = defaultSubscription;
}

/**
* Gets the creation timestamp of the topic.
*
* @return The creation timestamp.
*/
public String getCreatedAt() {
return createdAt;
}

/**
* Sets the creation timestamp of the topic.
*
* @param createdAt The creation timestamp to set.
*/
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
}
Loading