-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add segments and topics to sub services #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
src/main/java/com/resend/services/contacts/ContactSegments.java
This file contains hidden or 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,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; | ||
| 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
108
src/main/java/com/resend/services/contacts/ContactTopics.java
This file contains hidden or 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,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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
URLHelperto use camel-case for the acronym (e.g.,UrlHelper) to comply with the Initialisms and Acronyms Naming Conventions rule.Prompt for AI agents