Skip to content

Commit 42b351b

Browse files
feat: Add update and list methods for contact topics (#66)
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
1 parent 2f964ac commit 42b351b

File tree

8 files changed

+621
-0
lines changed

8 files changed

+621
-0
lines changed

src/main/java/com/resend/services/contacts/Contacts.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,84 @@ public UpdateContactResponseSuccess update(UpdateContactOptions params) throws R
165165

166166
return resendMapper.readValue(responseBody, UpdateContactResponseSuccess.class);
167167
}
168+
169+
/**
170+
* Retrieves a list of topic subscriptions for a contact.
171+
*
172+
* @param contactIdOrEmail The contact ID or email address.
173+
* @return A ListContactTopicsResponse containing the list of topic subscriptions.
174+
* @throws ResendException If an error occurs during the topic list retrieval process.
175+
*/
176+
public ListContactTopicsResponse getTopics(String contactIdOrEmail) throws ResendException {
177+
if (contactIdOrEmail == null || contactIdOrEmail.isEmpty()) {
178+
throw new IllegalArgumentException("Contact ID or email must be provided");
179+
}
180+
181+
AbstractHttpResponse<String> response = this.httpClient.perform("/contacts/" + contactIdOrEmail + "/topics", super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
182+
183+
if (!response.isSuccessful()) {
184+
throw new ResendException(response.getCode(), response.getBody());
185+
}
186+
187+
String responseBody = response.getBody();
188+
189+
return resendMapper.readValue(responseBody, ListContactTopicsResponse.class);
190+
}
191+
192+
/**
193+
* Retrieves a paginated list of topic subscriptions for a contact.
194+
*
195+
* @param contactIdOrEmail The contact ID or email address.
196+
* @param params The params used to customize the list.
197+
* @return A ListContactTopicsResponse containing the paginated list of topic subscriptions.
198+
* @throws ResendException If an error occurs during the topic list retrieval process.
199+
*/
200+
public ListContactTopicsResponse getTopics(String contactIdOrEmail, ListParams params) throws ResendException {
201+
if (contactIdOrEmail == null || contactIdOrEmail.isEmpty()) {
202+
throw new IllegalArgumentException("Contact ID or email must be provided");
203+
}
204+
205+
String pathWithQuery = "/contacts/" + contactIdOrEmail + "/topics" + URLHelper.parse(params);
206+
AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
207+
208+
if (!response.isSuccessful()) {
209+
throw new ResendException(response.getCode(), response.getBody());
210+
}
211+
212+
String responseBody = response.getBody();
213+
214+
return resendMapper.readValue(responseBody, ListContactTopicsResponse.class);
215+
}
216+
217+
/**
218+
* Updates topic subscriptions for a contact.
219+
*
220+
* @param options The options containing the contact identifier and topic updates.
221+
* @return The UpdateContactTopicsResponse with the contact ID.
222+
* @throws ResendException If an error occurs during the topic update process.
223+
*/
224+
public UpdateContactTopicsResponse updateTopics(UpdateContactTopicsOptions options) throws ResendException {
225+
if ((options.getId() == null && options.getEmail() == null) ||
226+
(options.getId() != null && options.getEmail() != null)) {
227+
throw new IllegalArgumentException("Either 'id' or 'email' must be provided, but not both.");
228+
}
229+
230+
if (options.getTopics() == null || options.getTopics().isEmpty()) {
231+
throw new IllegalArgumentException("Topics list must not be empty");
232+
}
233+
234+
String contactIdOrEmail = options.getId() != null ? options.getId() : options.getEmail();
235+
236+
// Serialize just the topics array (not the whole options object)
237+
String payload = super.resendMapper.writeValue(options.getTopics());
238+
AbstractHttpResponse<String> response = httpClient.perform("/contacts/" + contactIdOrEmail + "/topics", super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json"));
239+
240+
if (!response.isSuccessful()) {
241+
throw new ResendException(response.getCode(), response.getBody());
242+
}
243+
244+
String responseBody = response.getBody();
245+
246+
return resendMapper.readValue(responseBody, UpdateContactTopicsResponse.class);
247+
}
168248
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.resend.services.contacts.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
/**
6+
* Represents a contact topic subscription.
7+
*/
8+
public class ContactTopic {
9+
10+
@JsonProperty("id")
11+
private String id;
12+
13+
@JsonProperty("name")
14+
private String name;
15+
16+
@JsonProperty("description")
17+
private String description;
18+
19+
@JsonProperty("subscription")
20+
private String subscription;
21+
22+
/**
23+
* Default constructor
24+
*/
25+
public ContactTopic() {
26+
}
27+
28+
/**
29+
* Creates an instance of ContactTopic with the specified attributes.
30+
*
31+
* @param id The ID of the topic.
32+
* @param name The name of the topic.
33+
* @param description The description of the topic.
34+
* @param subscription The subscription status (opt_in or opt_out).
35+
*/
36+
public ContactTopic(final String id, final String name, final String description, final String subscription) {
37+
this.id = id;
38+
this.name = name;
39+
this.description = description;
40+
this.subscription = subscription;
41+
}
42+
43+
/**
44+
* Gets the ID of the topic.
45+
*
46+
* @return The ID of the topic.
47+
*/
48+
public String getId() {
49+
return id;
50+
}
51+
52+
/**
53+
* Gets the name of the topic.
54+
*
55+
* @return The name of the topic.
56+
*/
57+
public String getName() {
58+
return name;
59+
}
60+
61+
/**
62+
* Gets the description of the topic.
63+
*
64+
* @return The description of the topic.
65+
*/
66+
public String getDescription() {
67+
return description;
68+
}
69+
70+
/**
71+
* Gets the subscription status of the topic.
72+
*
73+
* @return The subscription status (opt_in or opt_out).
74+
*/
75+
public String getSubscription() {
76+
return subscription;
77+
}
78+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.resend.services.contacts.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
/**
6+
* Represents options for a topic subscription update.
7+
*/
8+
public class ContactTopicOptions {
9+
10+
@JsonProperty("id")
11+
private String id;
12+
13+
@JsonProperty("subscription")
14+
private String subscription;
15+
16+
/**
17+
* Default constructor
18+
*/
19+
public ContactTopicOptions() {
20+
}
21+
22+
/**
23+
* Creates an instance of ContactTopicOptions.
24+
*
25+
* @param id The topic ID.
26+
* @param subscription The subscription action (opt_in or opt_out).
27+
*/
28+
public ContactTopicOptions(final String id, final String subscription) {
29+
this.id = id;
30+
this.subscription = subscription;
31+
}
32+
33+
/**
34+
* Gets the topic ID.
35+
*
36+
* @return The topic ID.
37+
*/
38+
public String getId() {
39+
return id;
40+
}
41+
42+
/**
43+
* Sets the topic ID.
44+
*
45+
* @param id The topic ID.
46+
*/
47+
public void setId(String id) {
48+
this.id = id;
49+
}
50+
51+
/**
52+
* Gets the subscription action.
53+
*
54+
* @return The subscription action (opt_in or opt_out).
55+
*/
56+
public String getSubscription() {
57+
return subscription;
58+
}
59+
60+
/**
61+
* Sets the subscription action.
62+
*
63+
* @param subscription The subscription action (opt_in or opt_out).
64+
*/
65+
public void setSubscription(String subscription) {
66+
this.subscription = subscription;
67+
}
68+
69+
/**
70+
* Creates a builder for ContactTopicOptions.
71+
*
72+
* @return A new builder instance.
73+
*/
74+
public static Builder builder() {
75+
return new Builder();
76+
}
77+
78+
/**
79+
* Builder for ContactTopicOptions.
80+
*/
81+
public static class Builder {
82+
private String id;
83+
private String subscription;
84+
85+
/**
86+
* Sets the topic ID.
87+
*
88+
* @param id The topic ID.
89+
* @return This builder instance.
90+
*/
91+
public Builder id(String id) {
92+
this.id = id;
93+
return this;
94+
}
95+
96+
/**
97+
* Sets the subscription action.
98+
*
99+
* @param subscription The subscription action (opt_in or opt_out).
100+
* @return This builder instance.
101+
*/
102+
public Builder subscription(String subscription) {
103+
this.subscription = subscription;
104+
return this;
105+
}
106+
107+
/**
108+
* Builds the ContactTopicOptions instance.
109+
*
110+
* @return A new TopicSubscriptionOptions instance.
111+
*/
112+
public ContactTopicOptions build() {
113+
return new ContactTopicOptions(id, subscription);
114+
}
115+
}
116+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.resend.services.contacts.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Represents a successful response for listing contact topics.
9+
*/
10+
public class ListContactTopicsResponse {
11+
12+
@JsonProperty("object")
13+
private String object;
14+
15+
@JsonProperty("data")
16+
private List<ContactTopic> data;
17+
18+
@JsonProperty("has_more")
19+
private Boolean hasMore;
20+
21+
/**
22+
* Default constructor
23+
*/
24+
public ListContactTopicsResponse() {
25+
}
26+
27+
/**
28+
* Constructs a successful response for listing contact topics.
29+
*
30+
* @param object The object type.
31+
* @param data The list of contact topics.
32+
* @param hasMore Whether there are more items available for pagination.
33+
*/
34+
public ListContactTopicsResponse(final String object, final List<ContactTopic> data, final Boolean hasMore) {
35+
this.object = object;
36+
this.data = data;
37+
this.hasMore = hasMore;
38+
}
39+
40+
/**
41+
* Gets the object type.
42+
*
43+
* @return The object type.
44+
*/
45+
public String getObject() {
46+
return object;
47+
}
48+
49+
/**
50+
* Gets the list of contact topics.
51+
*
52+
* @return The list of contact topics.
53+
*/
54+
public List<ContactTopic> getData() {
55+
return data;
56+
}
57+
58+
/**
59+
* Gets the indicator whether there are more items available for pagination.
60+
*
61+
* @return Whether there are more items available for pagination.
62+
*/
63+
public Boolean hasMore() {
64+
return hasMore;
65+
}
66+
}

0 commit comments

Comments
 (0)