Skip to content

Commit 2393de5

Browse files
committed
feat: Add segments and topics to sub services
1 parent ca5e7c2 commit 2393de5

17 files changed

+1321
-110
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.ListContactTopicsResponse;
10+
import com.resend.services.contacts.model.UpdateContactTopicsOptions;
11+
import com.resend.services.contacts.model.UpdateContactTopicsResponse;
12+
import okhttp3.MediaType;
13+
14+
/**
15+
* Represents the Contact Topics sub-service.
16+
* Handles operations for managing contact topic subscriptions.
17+
*/
18+
public class ContactTopics extends BaseService {
19+
20+
/**
21+
* Constructs an instance of the {@code ContactTopics} class.
22+
*
23+
* @param apiKey The apiKey used for authentication.
24+
*/
25+
public ContactTopics(final String apiKey) {
26+
super(apiKey);
27+
}
28+
29+
/**
30+
* Retrieves a list of topic subscriptions for a contact.
31+
*
32+
* @param contactIdOrEmail The contact ID or email address.
33+
* @return A ListContactTopicsResponse containing the list of topic subscriptions.
34+
* @throws ResendException If an error occurs during the topic list retrieval process.
35+
*/
36+
public ListContactTopicsResponse list(String contactIdOrEmail) throws ResendException {
37+
if (contactIdOrEmail == null || contactIdOrEmail.isEmpty()) {
38+
throw new IllegalArgumentException("Contact ID or email must be provided");
39+
}
40+
41+
AbstractHttpResponse<String> response = this.httpClient.perform("/contacts/" + contactIdOrEmail + "/topics", super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
42+
43+
if (!response.isSuccessful()) {
44+
throw new ResendException(response.getCode(), response.getBody());
45+
}
46+
47+
String responseBody = response.getBody();
48+
49+
return resendMapper.readValue(responseBody, ListContactTopicsResponse.class);
50+
}
51+
52+
/**
53+
* Retrieves a paginated list of topic subscriptions for a contact.
54+
*
55+
* @param contactIdOrEmail The contact ID or email address.
56+
* @param params The params used to customize the list.
57+
* @return A ListContactTopicsResponse containing the paginated list of topic subscriptions.
58+
* @throws ResendException If an error occurs during the topic list retrieval process.
59+
*/
60+
public ListContactTopicsResponse list(String contactIdOrEmail, ListParams params) throws ResendException {
61+
if (contactIdOrEmail == null || contactIdOrEmail.isEmpty()) {
62+
throw new IllegalArgumentException("Contact ID or email must be provided");
63+
}
64+
65+
String pathWithQuery = "/contacts/" + contactIdOrEmail + "/topics" + URLHelper.parse(params);
66+
AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
67+
68+
if (!response.isSuccessful()) {
69+
throw new ResendException(response.getCode(), response.getBody());
70+
}
71+
72+
String responseBody = response.getBody();
73+
74+
return resendMapper.readValue(responseBody, ListContactTopicsResponse.class);
75+
}
76+
77+
/**
78+
* Updates topic subscriptions for a contact.
79+
*
80+
* @param options The options containing the contact identifier and topic updates.
81+
* @return The UpdateContactTopicsResponse with the contact ID.
82+
* @throws ResendException If an error occurs during the topic update process.
83+
*/
84+
public UpdateContactTopicsResponse update(UpdateContactTopicsOptions options) throws ResendException {
85+
if ((options.getId() == null && options.getEmail() == null) ||
86+
(options.getId() != null && options.getEmail() != null)) {
87+
throw new IllegalArgumentException("Either 'id' or 'email' must be provided, but not both.");
88+
}
89+
90+
if (options.getTopics() == null || options.getTopics().isEmpty()) {
91+
throw new IllegalArgumentException("Topics list must not be empty");
92+
}
93+
94+
String contactIdOrEmail = options.getId() != null ? options.getId() : options.getEmail();
95+
96+
// Serialize just the topics array (not the whole options object)
97+
String payload = super.resendMapper.writeValue(options.getTopics());
98+
AbstractHttpResponse<String> response = httpClient.perform("/contacts/" + contactIdOrEmail + "/topics", super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json"));
99+
100+
if (!response.isSuccessful()) {
101+
throw new ResendException(response.getCode(), response.getBody());
102+
}
103+
104+
String responseBody = response.getBody();
105+
106+
return resendMapper.readValue(responseBody, UpdateContactTopicsResponse.class);
107+
}
108+
}

0 commit comments

Comments
 (0)