|
| 1 | +package com.resend.services.contacts; |
| 2 | + |
| 3 | +import com.resend.core.exception.ResendException; |
| 4 | +import com.resend.core.helper.URLHelper; |
| 5 | +import com.resend.core.net.AbstractHttpResponse; |
| 6 | +import com.resend.core.net.HttpMethod; |
| 7 | +import com.resend.core.net.ListParams; |
| 8 | +import com.resend.core.service.BaseService; |
| 9 | +import com.resend.services.contacts.model.*; |
| 10 | +import okhttp3.MediaType; |
| 11 | + |
| 12 | +/** |
| 13 | + * Represents the Contact Segments sub-service. |
| 14 | + * Handles operations for managing contact membership in segments. |
| 15 | + */ |
| 16 | +public class ContactSegments extends BaseService { |
| 17 | + |
| 18 | + /** |
| 19 | + * Constructs an instance of the {@code ContactSegments} class. |
| 20 | + * |
| 21 | + * @param apiKey The apiKey used for authentication. |
| 22 | + */ |
| 23 | + public ContactSegments(final String apiKey) { |
| 24 | + super(apiKey); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Adds an existing contact to a segment. |
| 29 | + * |
| 30 | + * @param options The options containing the contact identifier (id or email) and segment ID. |
| 31 | + * @return The AddContactToSegmentResponseSuccess with the segment ID. |
| 32 | + * @throws ResendException If an error occurs during the segment addition process. |
| 33 | + */ |
| 34 | + public AddContactToSegmentResponseSuccess add(AddContactToSegmentOptions options) throws ResendException { |
| 35 | + if ((options.getId() == null && options.getEmail() == null) || |
| 36 | + (options.getId() != null && options.getEmail() != null)) { |
| 37 | + throw new IllegalArgumentException("Either 'id' or 'email' must be provided, but not both."); |
| 38 | + } |
| 39 | + |
| 40 | + if (options.getSegmentId() == null || options.getSegmentId().isEmpty()) { |
| 41 | + throw new IllegalArgumentException("Segment ID must be provided"); |
| 42 | + } |
| 43 | + |
| 44 | + String contactIdOrEmail = options.getId() != null ? options.getId() : options.getEmail(); |
| 45 | + String endpoint = "/contacts/" + contactIdOrEmail + "/segments/" + options.getSegmentId(); |
| 46 | + |
| 47 | + AbstractHttpResponse<String> response = httpClient.perform(endpoint, super.apiKey, HttpMethod.POST, "", MediaType.get("application/json")); |
| 48 | + |
| 49 | + if (!response.isSuccessful()) { |
| 50 | + throw new ResendException(response.getCode(), response.getBody()); |
| 51 | + } |
| 52 | + |
| 53 | + String responseBody = response.getBody(); |
| 54 | + |
| 55 | + return resendMapper.readValue(responseBody, AddContactToSegmentResponseSuccess.class); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Removes an existing contact from a segment. |
| 60 | + * |
| 61 | + * @param options The options containing the contact identifier (id or email) and segment ID. |
| 62 | + * @return The RemoveContactFromSegmentResponseSuccess with the segment ID and deletion status. |
| 63 | + * @throws ResendException If an error occurs during the segment removal process. |
| 64 | + */ |
| 65 | + public RemoveContactFromSegmentResponseSuccess remove(RemoveContactFromSegmentOptions options) throws ResendException { |
| 66 | + if ((options.getId() == null && options.getEmail() == null) || |
| 67 | + (options.getId() != null && options.getEmail() != null)) { |
| 68 | + throw new IllegalArgumentException("Either 'id' or 'email' must be provided, but not both."); |
| 69 | + } |
| 70 | + |
| 71 | + if (options.getSegmentId() == null || options.getSegmentId().isEmpty()) { |
| 72 | + throw new IllegalArgumentException("Segment ID must be provided"); |
| 73 | + } |
| 74 | + |
| 75 | + String contactIdOrEmail = options.getId() != null ? options.getId() : options.getEmail(); |
| 76 | + String endpoint = "/contacts/" + contactIdOrEmail + "/segments/" + options.getSegmentId(); |
| 77 | + |
| 78 | + AbstractHttpResponse<String> response = httpClient.perform(endpoint, super.apiKey, HttpMethod.DELETE, "", null); |
| 79 | + |
| 80 | + if (!response.isSuccessful()) { |
| 81 | + throw new ResendException(response.getCode(), response.getBody()); |
| 82 | + } |
| 83 | + |
| 84 | + String responseBody = response.getBody(); |
| 85 | + |
| 86 | + return resendMapper.readValue(responseBody, RemoveContactFromSegmentResponseSuccess.class); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Retrieves a list of segments that a contact belongs to. |
| 91 | + * |
| 92 | + * @param contactIdOrEmail The contact ID or email address. |
| 93 | + * @return The ListContactSegmentsResponseSuccess with the list of segments. |
| 94 | + * @throws ResendException If an error occurs during the segment list retrieval process. |
| 95 | + */ |
| 96 | + public ListContactSegmentsResponseSuccess list(String contactIdOrEmail) throws ResendException { |
| 97 | + if (contactIdOrEmail == null || contactIdOrEmail.isEmpty()) { |
| 98 | + throw new IllegalArgumentException("Contact ID or email must be provided"); |
| 99 | + } |
| 100 | + |
| 101 | + String endpoint = "/contacts/" + contactIdOrEmail + "/segments"; |
| 102 | + AbstractHttpResponse<String> response = httpClient.perform(endpoint, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json")); |
| 103 | + |
| 104 | + if (!response.isSuccessful()) { |
| 105 | + throw new ResendException(response.getCode(), response.getBody()); |
| 106 | + } |
| 107 | + |
| 108 | + String responseBody = response.getBody(); |
| 109 | + |
| 110 | + return resendMapper.readValue(responseBody, ListContactSegmentsResponseSuccess.class); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Retrieves a paginated list of segments that a contact belongs to. |
| 115 | + * |
| 116 | + * @param contactId The contact ID. |
| 117 | + * @param params The params used to customize the list. |
| 118 | + * @return The ListContactSegmentsResponseSuccess with the paginated list of segments. |
| 119 | + * @throws ResendException If an error occurs during the segment list retrieval process. |
| 120 | + */ |
| 121 | + public ListContactSegmentsResponseSuccess list(String contactId, ListParams params) throws ResendException { |
| 122 | + if (contactId == null || contactId.isEmpty()) { |
| 123 | + throw new IllegalArgumentException("Contact ID must be provided"); |
| 124 | + } |
| 125 | + |
| 126 | + String pathWithQuery = "/contacts/" + contactId + "/segments" + URLHelper.parse(params); |
| 127 | + AbstractHttpResponse<String> response = httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json")); |
| 128 | + |
| 129 | + if (!response.isSuccessful()) { |
| 130 | + throw new ResendException(response.getCode(), response.getBody()); |
| 131 | + } |
| 132 | + |
| 133 | + String responseBody = response.getBody(); |
| 134 | + |
| 135 | + return resendMapper.readValue(responseBody, ListContactSegmentsResponseSuccess.class); |
| 136 | + } |
| 137 | +} |
0 commit comments