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
137 changes: 137 additions & 0 deletions src/main/java/com/resend/services/contacts/ContactSegments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.resend.services.contacts;

import com.resend.core.exception.ResendException;
import com.resend.core.helper.URLHelper;
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rule violated: Initialisms and Acronyms Naming Conventions

Rename URLHelper to use camel-case for the acronym (e.g., UrlHelper) to comply with the Initialisms and Acronyms Naming Conventions rule.

Prompt for AI agents
Address the following comment on src/main/java/com/resend/services/contacts/ContactSegments.java at line 4:

<comment>Rename `URLHelper` to use camel-case for the acronym (e.g., `UrlHelper`) to comply with the Initialisms and Acronyms Naming Conventions rule.</comment>

<file context>
@@ -0,0 +1,137 @@
+package com.resend.services.contacts;
+
+import com.resend.core.exception.ResendException;
+import com.resend.core.helper.URLHelper;
+import com.resend.core.net.AbstractHttpResponse;
+import com.resend.core.net.HttpMethod;
</file context>
Fix with Cubic

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.contacts.model.*;
import okhttp3.MediaType;

/**
* Represents the Contact Segments sub-service.
* Handles operations for managing contact membership in segments.
*/
public class ContactSegments extends BaseService {

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

/**
* Adds an existing contact to a segment.
*
* @param options The options containing the contact identifier (id or email) and segment ID.
* @return The AddContactToSegmentResponseSuccess with the segment ID.
* @throws ResendException If an error occurs during the segment addition process.
*/
public AddContactToSegmentResponseSuccess add(AddContactToSegmentOptions options) throws ResendException {
if ((options.getId() == null && options.getEmail() == null) ||
(options.getId() != null && options.getEmail() != null)) {
throw new IllegalArgumentException("Either 'id' or 'email' must be provided, but not both.");
}

if (options.getSegmentId() == null || options.getSegmentId().isEmpty()) {
throw new IllegalArgumentException("Segment ID must be provided");
}

String contactIdOrEmail = options.getId() != null ? options.getId() : options.getEmail();
String endpoint = "/contacts/" + contactIdOrEmail + "/segments/" + options.getSegmentId();

AbstractHttpResponse<String> response = httpClient.perform(endpoint, 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, AddContactToSegmentResponseSuccess.class);
}

/**
* Removes an existing contact from a segment.
*
* @param options The options containing the contact identifier (id or email) and segment ID.
* @return The RemoveContactFromSegmentResponseSuccess with the segment ID and deletion status.
* @throws ResendException If an error occurs during the segment removal process.
*/
public RemoveContactFromSegmentResponseSuccess remove(RemoveContactFromSegmentOptions options) throws ResendException {
if ((options.getId() == null && options.getEmail() == null) ||
(options.getId() != null && options.getEmail() != null)) {
throw new IllegalArgumentException("Either 'id' or 'email' must be provided, but not both.");
}

if (options.getSegmentId() == null || options.getSegmentId().isEmpty()) {
throw new IllegalArgumentException("Segment ID must be provided");
}

String contactIdOrEmail = options.getId() != null ? options.getId() : options.getEmail();
String endpoint = "/contacts/" + contactIdOrEmail + "/segments/" + options.getSegmentId();

AbstractHttpResponse<String> response = httpClient.perform(endpoint, super.apiKey, HttpMethod.DELETE, "", null);

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

String responseBody = response.getBody();

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

/**
* Retrieves a list of segments that a contact belongs to.
*
* @param contactIdOrEmail The contact ID or email address.
* @return The ListContactSegmentsResponseSuccess with the list of segments.
* @throws ResendException If an error occurs during the segment list retrieval process.
*/
public ListContactSegmentsResponseSuccess list(String contactIdOrEmail) throws ResendException {
if (contactIdOrEmail == null || contactIdOrEmail.isEmpty()) {
throw new IllegalArgumentException("Contact ID or email must be provided");
}

String endpoint = "/contacts/" + contactIdOrEmail + "/segments";
AbstractHttpResponse<String> response = httpClient.perform(endpoint, 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, ListContactSegmentsResponseSuccess.class);
}

/**
* Retrieves a paginated list of segments that a contact belongs to.
*
* @param contactId The contact ID.
* @param params The params used to customize the list.
* @return The ListContactSegmentsResponseSuccess with the paginated list of segments.
* @throws ResendException If an error occurs during the segment list retrieval process.
*/
public ListContactSegmentsResponseSuccess list(String contactId, ListParams params) throws ResendException {
if (contactId == null || contactId.isEmpty()) {
throw new IllegalArgumentException("Contact ID must be provided");
}

String pathWithQuery = "/contacts/" + contactId + "/segments" + URLHelper.parse(params);
AbstractHttpResponse<String> response = 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, ListContactSegmentsResponseSuccess.class);
}
}
108 changes: 108 additions & 0 deletions src/main/java/com/resend/services/contacts/ContactTopics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.resend.services.contacts;

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.contacts.model.ListContactTopicsResponse;
import com.resend.services.contacts.model.UpdateContactTopicsOptions;
import com.resend.services.contacts.model.UpdateContactTopicsResponse;
import okhttp3.MediaType;

/**
* Represents the Contact Topics sub-service.
* Handles operations for managing contact topic subscriptions.
*/
public class ContactTopics extends BaseService {

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

/**
* Retrieves a list of topic subscriptions for a contact.
*
* @param contactIdOrEmail The contact ID or email address.
* @return A ListContactTopicsResponse containing the list of topic subscriptions.
* @throws ResendException If an error occurs during the topic list retrieval process.
*/
public ListContactTopicsResponse list(String contactIdOrEmail) throws ResendException {
if (contactIdOrEmail == null || contactIdOrEmail.isEmpty()) {
throw new IllegalArgumentException("Contact ID or email must be provided");
}

AbstractHttpResponse<String> response = this.httpClient.perform("/contacts/" + contactIdOrEmail + "/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, ListContactTopicsResponse.class);
}

/**
* Retrieves a paginated list of topic subscriptions for a contact.
*
* @param contactIdOrEmail The contact ID or email address.
* @param params The params used to customize the list.
* @return A ListContactTopicsResponse containing the paginated list of topic subscriptions.
* @throws ResendException If an error occurs during the topic list retrieval process.
*/
public ListContactTopicsResponse list(String contactIdOrEmail, ListParams params) throws ResendException {
if (contactIdOrEmail == null || contactIdOrEmail.isEmpty()) {
throw new IllegalArgumentException("Contact ID or email must be provided");
}

String pathWithQuery = "/contacts/" + contactIdOrEmail + "/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, ListContactTopicsResponse.class);
}

/**
* Updates topic subscriptions for a contact.
*
* @param options The options containing the contact identifier and topic updates.
* @return The UpdateContactTopicsResponse with the contact ID.
* @throws ResendException If an error occurs during the topic update process.
*/
public UpdateContactTopicsResponse update(UpdateContactTopicsOptions options) throws ResendException {
if ((options.getId() == null && options.getEmail() == null) ||
(options.getId() != null && options.getEmail() != null)) {
throw new IllegalArgumentException("Either 'id' or 'email' must be provided, but not both.");
}

if (options.getTopics() == null || options.getTopics().isEmpty()) {
throw new IllegalArgumentException("Topics list must not be empty");
}

String contactIdOrEmail = options.getId() != null ? options.getId() : options.getEmail();

// Serialize just the topics array (not the whole options object)
String payload = super.resendMapper.writeValue(options.getTopics());
AbstractHttpResponse<String> response = httpClient.perform("/contacts/" + contactIdOrEmail + "/topics", 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, UpdateContactTopicsResponse.class);
}
}
Loading