Skip to content

Commit 71e2774

Browse files
committed
feat: introduce the /segment API endpoints
1 parent d18d450 commit 71e2774

19 files changed

+822
-4
lines changed

src/main/java/com/resend/Resend.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.resend.services.contacts.Contacts;
88
import com.resend.services.domains.Domains;
99
import com.resend.services.emails.Emails;
10+
import com.resend.services.segments.Segments;
1011
import com.resend.services.webhooks.Webhooks;
1112
import com.resend.services.receiving.Receiving;
1213
import com.resend.services.topics.Topics;
@@ -71,11 +72,22 @@ public Contacts contacts() {
7172
* Returns an Audience object that can be used to interact with the Audiences service.
7273
*
7374
* @return an Audiences object.
75+
* @deprecated Use {@link #segments()} instead.
7476
*/
77+
@Deprecated
7578
public Audiences audiences() {
7679
return new Audiences(apiKey);
7780
}
7881

82+
/**
83+
* Returns a Segments object that can be used to interact with the Segments service.
84+
*
85+
* @return a Segments object.
86+
*/
87+
public Segments segments() {
88+
return new Segments(apiKey);
89+
}
90+
7991
/**
8092
* Returns a Batch object that can be used to interact with the Batch service.
8193
*

src/main/java/com/resend/services/audiences/Audiences.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public Audiences(final String apiKey) {
2929
* @param createAudienceOptions The Audience details.
3030
* @return The details of the created audience.
3131
* @throws ResendException If an error occurs during the Audience creation process.
32+
* @deprecated Use {@link com.resend.services.segments.Segments#create(com.resend.services.segments.model.CreateSegmentOptions)} instead.
3233
*/
34+
@Deprecated
3335
public CreateAudienceResponseSuccess create(CreateAudienceOptions createAudienceOptions) throws ResendException {
3436
String payload = super.resendMapper.writeValue(createAudienceOptions);
3537
AbstractHttpResponse<String> response = httpClient.perform("/audiences", super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));
@@ -47,7 +49,9 @@ public CreateAudienceResponseSuccess create(CreateAudienceOptions createAudience
4749
*
4850
* @return A ListAudiencesResponseSuccess containing the list of audiences.
4951
* @throws ResendException If an error occurs during the audiences list retrieval process.
52+
* @deprecated Use {@link com.resend.services.segments.Segments#list()} instead.
5053
*/
54+
@Deprecated
5155
public ListAudiencesResponseSuccess list() throws ResendException {
5256
AbstractHttpResponse<String> response = this.httpClient.perform("/audiences", super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
5357

@@ -66,7 +70,9 @@ public ListAudiencesResponseSuccess list() throws ResendException {
6670
*
6771
* @return A ListAudiencesResponseSuccess containing the paginated list of audiences.
6872
* @throws ResendException If an error occurs during the audiences list retrieval process.
73+
* @deprecated Use {@link com.resend.services.segments.Segments#list(ListParams)} instead.
6974
*/
75+
@Deprecated
7076
public ListAudiencesResponseSuccess list(ListParams params) throws ResendException {
7177
String pathWithQuery = "/audiences" + URLHelper.parse(params);
7278
AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
@@ -86,7 +92,9 @@ public ListAudiencesResponseSuccess list(ListParams params) throws ResendExcepti
8692
* @param id The unique identifier of the audience.
8793
* @return The retrieved audience details.
8894
* @throws ResendException If an error occurs while retrieving the audience.
95+
* @deprecated Use {@link com.resend.services.segments.Segments#get(String)} instead.
8996
*/
97+
@Deprecated
9098
public GetAudienceResponseSuccess get(String id) throws ResendException {
9199
AbstractHttpResponse<String> response = this.httpClient.perform("/audiences/" +id, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
92100

@@ -105,7 +113,9 @@ public GetAudienceResponseSuccess get(String id) throws ResendException {
105113
* @param id The unique identifier of the audience to delete.
106114
* @return The RemoveAudiencesResponseSuccess with the details of the removed audience.
107115
* @throws ResendException If an error occurs during the audience deletion process.
116+
* @deprecated Use {@link com.resend.services.segments.Segments#remove(String)} instead.
108117
*/
118+
@Deprecated
109119
public RemoveAudienceResponseSuccess remove(String id) throws ResendException {
110120
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" +id, super.apiKey, HttpMethod.DELETE, "", null);
111121

src/main/java/com/resend/services/broadcasts/model/BroadcastOptions.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
*/
1010
public class BroadcastOptions {
1111
@JsonProperty("audience_id")
12+
@Deprecated
1213
private final String audienceId;
1314

15+
@JsonProperty("segment_id")
16+
private final String segmentId;
17+
1418
@JsonProperty("from")
1519
private final String from;
1620

@@ -36,6 +40,7 @@ public class BroadcastOptions {
3640
*/
3741
protected BroadcastOptions(Builder builder) {
3842
this.audienceId = builder.audienceId;
43+
this.segmentId = builder.segmentId;
3944
this.from = builder.from;
4045
this.subject = builder.subject;
4146
this.replyTo = builder.replyTo;
@@ -48,11 +53,22 @@ protected BroadcastOptions(Builder builder) {
4853
* Gets the audience ID.
4954
*
5055
* @return the unique identifier of the audience.
56+
* @deprecated Use {@link #getSegmentId()} instead.
5157
*/
58+
@Deprecated
5259
public String getAudienceId() {
5360
return audienceId;
5461
}
5562

63+
/**
64+
* Gets the segment ID.
65+
*
66+
* @return the unique identifier of the segment.
67+
*/
68+
public String getSegmentId() {
69+
return segmentId;
70+
}
71+
5672
/**
5773
* Gets the sender's email address.
5874
*
@@ -114,9 +130,16 @@ protected static abstract class Builder<T extends BroadcastOptions, B extends Bu
114130

115131
/**
116132
* The ID of the audience targeted by the broadcast.
133+
* @deprecated Use {@link #segmentId} instead.
117134
*/
135+
@Deprecated
118136
protected String audienceId;
119137

138+
/**
139+
* The ID of the segment targeted by the broadcast.
140+
*/
141+
protected String segmentId;
142+
120143
/**
121144
* The email address of the sender.
122145
*/
@@ -152,12 +175,25 @@ protected static abstract class Builder<T extends BroadcastOptions, B extends Bu
152175
*
153176
* @param audienceId the unique identifier of the audience.
154177
* @return the builder instance.
178+
* @deprecated Use {@link #segmentId(String)} instead.
155179
*/
180+
@Deprecated
156181
public B audienceId(String audienceId) {
157182
this.audienceId = audienceId;
158183
return self();
159184
}
160185

186+
/**
187+
* Sets the segment ID.
188+
*
189+
* @param segmentId the unique identifier of the segment.
190+
* @return the builder instance.
191+
*/
192+
public B segmentId(String segmentId) {
193+
this.segmentId = segmentId;
194+
return self();
195+
}
196+
161197
/**
162198
* Sets the sender's email address.
163199
*

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public Contacts(final String apiKey) {
3131
* @throws ResendException If an error occurs during the Contact creation process.
3232
*/
3333
public CreateContactResponseSuccess create(CreateContactOptions createContactOptions) throws ResendException {
34+
String segmentId = createContactOptions.getSegmentId() != null ? createContactOptions.getSegmentId() : createContactOptions.getAudienceId();
3435
String payload = super.resendMapper.writeValue(createContactOptions);
35-
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" + createContactOptions.getAudienceId()+ "/contacts" , super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));
36+
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" + segmentId + "/contacts" , super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));
3637

3738
if (!response.isSuccessful()) {
3839
throw new ResendException(response.getCode(), response.getBody());
@@ -101,8 +102,9 @@ public GetContactResponseSuccess get(GetContactOptions params) throws ResendExce
101102
}
102103

103104
String contactIdentifier = (id != null && !id.isEmpty()) ? id : email;
105+
String segmentId = params.getSegmentId() != null ? params.getSegmentId() : params.getAudienceId();
104106

105-
AbstractHttpResponse<String> response = this.httpClient.perform("/audiences/" +params.getAudienceId()+ "/contacts/" +contactIdentifier, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
107+
AbstractHttpResponse<String> response = this.httpClient.perform("/audiences/" + segmentId + "/contacts/" + contactIdentifier, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
106108

107109
if (!response.isSuccessful()) {
108110
throw new ResendException(response.getCode(), response.getBody());
@@ -127,8 +129,9 @@ public RemoveContactResponseSuccess remove(RemoveContactOptions params) throws R
127129
}
128130

129131
String pathParameter = params.getId() != null ? params.getId() : params.getEmail();
132+
String segmentId = params.getSegmentId() != null ? params.getSegmentId() : params.getAudienceId();
130133

131-
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" +params.getAudienceId()+ "/contacts/" + pathParameter, super.apiKey, HttpMethod.DELETE, "", null);
134+
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" + segmentId + "/contacts/" + pathParameter, super.apiKey, HttpMethod.DELETE, "", null);
132135

133136
if (!response.isSuccessful()) {
134137
throw new ResendException(response.getCode(), response.getBody());
@@ -153,9 +156,10 @@ public UpdateContactResponseSuccess update(UpdateContactOptions params) throws R
153156
}
154157

155158
String pathParameter = params.getId() != null ? params.getId() : params.getEmail();
159+
String segmentId = params.getSegmentId() != null ? params.getSegmentId() : params.getAudienceId();
156160

157161
String payload = super.resendMapper.writeValue(params);
158-
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" +params.getAudienceId()+ "/contacts/" + pathParameter, super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json"));
162+
AbstractHttpResponse<String> response = httpClient.perform("/audiences/" + segmentId + "/contacts/" + pathParameter, super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json"));
159163

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

src/main/java/com/resend/services/contacts/model/ContactOptions.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@ public abstract class ContactOptions {
1515

1616
/**
1717
* The audience_id of the contact options
18+
* @deprecated Use {@link #segmentId} instead.
1819
*/
20+
@Deprecated
1921
@JsonProperty("audience_id")
2022
protected final String audienceId;
2123

24+
/**
25+
* The segment_id of the contact options
26+
*/
27+
@JsonProperty("segment_id")
28+
protected final String segmentId;
29+
2230
/**
2331
* The email of the contact options
2432
*/
@@ -33,6 +41,7 @@ public abstract class ContactOptions {
3341
protected ContactOptions(Builder builder) {
3442
this.id = builder.id;
3543
this.audienceId = builder.audienceId;
44+
this.segmentId = builder.segmentId;
3645
this.email = builder.email;
3746
}
3847

@@ -49,11 +58,22 @@ public String getId() {
4958
* Get the audienceId of the ContactOptions.
5059
*
5160
* @return The audienceId of the ContactOptions.
61+
* @deprecated Use {@link #getSegmentId()} instead.
5262
*/
63+
@Deprecated
5364
public String getAudienceId() {
5465
return audienceId;
5566
}
5667

68+
/**
69+
* Get the segmentId of the ContactOptions.
70+
*
71+
* @return The segmentId of the ContactOptions.
72+
*/
73+
public String getSegmentId() {
74+
return segmentId;
75+
}
76+
5777
/**
5878
* Get the email of the ContactOptions.
5979
*
@@ -74,9 +94,16 @@ protected static abstract class Builder<T extends ContactOptions, B extends Buil
7494

7595
/**
7696
* The audienceId of the contact options builder
97+
* @deprecated Use {@link #segmentId} instead.
7798
*/
99+
@Deprecated
78100
protected String audienceId;
79101

102+
/**
103+
* The segmentId of the contact options builder
104+
*/
105+
protected String segmentId;
106+
80107
/**
81108
* The email of the contact options builder
82109
*/
@@ -98,12 +125,25 @@ public B id(String id) {
98125
*
99126
* @param audienceId The audienceId of the ContactOptions.
100127
* @return The builder instance.
128+
* @deprecated Use {@link #segmentId(String)} instead.
101129
*/
130+
@Deprecated
102131
public B audienceId(String audienceId) {
103132
this.audienceId = audienceId;
104133
return self();
105134
}
106135

136+
/**
137+
* Set the segmentId of the ContactOptions.
138+
*
139+
* @param segmentId The segmentId of the ContactOptions.
140+
* @return The builder instance.
141+
*/
142+
public B segmentId(String segmentId) {
143+
this.segmentId = segmentId;
144+
return self();
145+
}
146+
107147
/**
108148
* Set the email of the ContactOptions.
109149
*

src/main/java/com/resend/services/contacts/model/CreateContactOptions.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
public class CreateContactOptions {
99

1010
@JsonProperty("audience_id")
11+
@Deprecated
1112
private final String audienceId;
1213

14+
@JsonProperty("segment_id")
15+
private final String segmentId;
16+
1317
@JsonProperty("email")
1418
private final String email;
1519

@@ -29,6 +33,7 @@ public class CreateContactOptions {
2933
*/
3034
public CreateContactOptions(Builder builder) {
3135
this.audienceId = builder.audienceId;
36+
this.segmentId = builder.segmentId;
3237
this.email = builder.email;
3338
this.unsubscribed = builder.unsubscribed;
3439
this.firstName = builder.firstName;
@@ -39,11 +44,22 @@ public CreateContactOptions(Builder builder) {
3944
* Get the audience ID of the contact.
4045
*
4146
* @return The audience ID of the contact.
47+
* @deprecated Use {@link #getSegmentId()} instead.
4248
*/
49+
@Deprecated
4350
public String getAudienceId() {
4451
return audienceId;
4552
}
4653

54+
/**
55+
* Get the segment ID of the contact.
56+
*
57+
* @return The segment ID of the contact.
58+
*/
59+
public String getSegmentId() {
60+
return segmentId;
61+
}
62+
4763
/**
4864
* Get the email of the contact.
4965
*
@@ -94,6 +110,7 @@ public static Builder builder() {
94110
*/
95111
public static class Builder {
96112
private String audienceId;
113+
private String segmentId;
97114
private String email;
98115
private Boolean unsubscribed;
99116
private String firstName;
@@ -104,12 +121,25 @@ public static class Builder {
104121
*
105122
* @param audienceId The audience ID of the contact.
106123
* @return The builder instance.
124+
* @deprecated Use {@link #segmentId(String)} instead.
107125
*/
126+
@Deprecated
108127
public Builder audienceId(String audienceId) {
109128
this.audienceId = audienceId;
110129
return this;
111130
}
112131

132+
/**
133+
* Set the segment ID of the contact.
134+
*
135+
* @param segmentId The segment ID of the contact.
136+
* @return The builder instance.
137+
*/
138+
public Builder segmentId(String segmentId) {
139+
this.segmentId = segmentId;
140+
return this;
141+
}
142+
113143
/**
114144
* Set the email of the contact.
115145
*

0 commit comments

Comments
 (0)