Skip to content

Commit 0d912e8

Browse files
feat: Implement templates module (beta) (#63)
1 parent 19acb26 commit 0d912e8

16 files changed

+2367
-0
lines changed

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

Lines changed: 10 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.templates.Templates;
1011

1112
/**
1213
* The Resend class provides a facade for the Domains and Emails services.
@@ -89,4 +90,13 @@ public Batch batch() {
8990
public Broadcasts broadcasts() {
9091
return new Broadcasts(apiKey);
9192
}
93+
94+
/**
95+
* Returns a Templates object that can be used to interact with the Templates service.
96+
*
97+
* @return A Templates object.
98+
*/
99+
public Templates templates() {
100+
return new Templates(apiKey);
101+
}
92102
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package com.resend.services.templates;
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.templates.model.*;
10+
import okhttp3.MediaType;
11+
12+
/**
13+
* Represents the Resend Templates module.
14+
*/
15+
public final class Templates extends BaseService {
16+
17+
/**
18+
* Constructs an instance of the {@code Templates} class.
19+
*
20+
* @param apiKey The apiKey used for authentication.
21+
*/
22+
public Templates(final String apiKey) {
23+
super(apiKey);
24+
}
25+
26+
/**
27+
* Creates a new template.
28+
*
29+
* @param options The options for creating the template.
30+
* @return The response indicating the status of the template creation.
31+
* @throws ResendException If an error occurs while creating the template.
32+
*/
33+
public CreateTemplateResponseSuccess create(CreateTemplateOptions options) throws ResendException {
34+
String payload = super.resendMapper.writeValue(options);
35+
AbstractHttpResponse<String> response = super.httpClient.perform("/templates", super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json"));
36+
37+
if (!response.isSuccessful()) {
38+
throw new ResendException(response.getCode(), response.getBody());
39+
}
40+
41+
String responseBody = response.getBody();
42+
43+
return resendMapper.readValue(responseBody, CreateTemplateResponseSuccess.class);
44+
}
45+
46+
/**
47+
* Retrieves a template by its unique identifier or alias.
48+
*
49+
* @param templateId The unique identifier or alias of the template.
50+
* @return The retrieved template's details.
51+
* @throws ResendException If an error occurs while retrieving the template.
52+
*/
53+
public GetTemplateResponseSuccess get(String templateId) throws ResendException {
54+
AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
55+
56+
if (!response.isSuccessful()) {
57+
throw new ResendException(response.getCode(), response.getBody());
58+
}
59+
60+
String responseBody = response.getBody();
61+
62+
return resendMapper.readValue(responseBody, GetTemplateResponseSuccess.class);
63+
}
64+
65+
/**
66+
* Retrieves a list of templates.
67+
*
68+
* @return A ListTemplatesResponse containing the list of templates.
69+
* @throws ResendException If an error occurs during the templates list retrieval process.
70+
*/
71+
public ListTemplatesResponseSuccess list() throws ResendException {
72+
AbstractHttpResponse<String> response = this.httpClient.perform("/templates", super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
73+
74+
if (!response.isSuccessful()) {
75+
throw new ResendException(response.getCode(), response.getBody());
76+
}
77+
78+
String responseBody = response.getBody();
79+
80+
return resendMapper.readValue(responseBody, ListTemplatesResponseSuccess.class);
81+
}
82+
83+
/**
84+
* Retrieves a paginated list of templates.
85+
*
86+
* @param params The params used to customize the list.
87+
* @return A ListTemplatesResponse containing the paginated list of templates.
88+
* @throws ResendException If an error occurs during the templates list retrieval process.
89+
*/
90+
public ListTemplatesResponseSuccess list(ListParams params) throws ResendException {
91+
String pathWithQuery = "/templates" + URLHelper.parse(params);
92+
AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json"));
93+
94+
if (!response.isSuccessful()) {
95+
throw new ResendException(response.getCode(), response.getBody());
96+
}
97+
98+
String responseBody = response.getBody();
99+
100+
return resendMapper.readValue(responseBody, ListTemplatesResponseSuccess.class);
101+
}
102+
103+
/**
104+
* Updates a template by its unique identifier or alias.
105+
*
106+
* @param templateId The unique identifier or alias of the template.
107+
* @param options The options for updating the template.
108+
* @return The response indicating the status of the template update.
109+
* @throws ResendException If an error occurs while updating the template.
110+
*/
111+
public UpdateTemplateResponseSuccess update(String templateId, UpdateTemplateOptions options) throws ResendException {
112+
String payload = super.resendMapper.writeValue(options);
113+
AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId, super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json"));
114+
115+
if (!response.isSuccessful()) {
116+
throw new ResendException(response.getCode(), response.getBody());
117+
}
118+
119+
String responseBody = response.getBody();
120+
121+
return resendMapper.readValue(responseBody, UpdateTemplateResponseSuccess.class);
122+
}
123+
124+
/**
125+
* Deletes a template by its unique identifier or alias.
126+
*
127+
* @param templateId The unique identifier or alias of the template.
128+
* @return The response indicating the status of the template deletion.
129+
* @throws ResendException If an error occurs while deleting the template.
130+
*/
131+
public DeleteTemplateResponseSuccess remove(String templateId) throws ResendException {
132+
AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId, super.apiKey, HttpMethod.DELETE, null, MediaType.get("application/json"));
133+
134+
if (!response.isSuccessful()) {
135+
throw new ResendException(response.getCode(), response.getBody());
136+
}
137+
138+
String responseBody = response.getBody();
139+
140+
return resendMapper.readValue(responseBody, DeleteTemplateResponseSuccess.class);
141+
}
142+
143+
/**
144+
* Duplicates a template by its unique identifier or alias.
145+
*
146+
* @param templateId The unique identifier or alias of the template.
147+
* @return The response indicating the status of the template duplication.
148+
* @throws ResendException If an error occurs while duplicating the template.
149+
*/
150+
public DuplicateTemplateResponseSuccess duplicate(String templateId) throws ResendException {
151+
AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId + "/duplicate", super.apiKey, HttpMethod.POST, "", MediaType.get("application/json"));
152+
153+
if (!response.isSuccessful()) {
154+
throw new ResendException(response.getCode(), response.getBody());
155+
}
156+
157+
String responseBody = response.getBody();
158+
159+
return resendMapper.readValue(responseBody, DuplicateTemplateResponseSuccess.class);
160+
}
161+
162+
/**
163+
* Publishes a template by its unique identifier or alias.
164+
*
165+
* @param templateId The unique identifier or alias of the template.
166+
* @return The response indicating the status of the template publication.
167+
* @throws ResendException If an error occurs while publishing the template.
168+
*/
169+
public PublishTemplateResponseSuccess publish(String templateId) throws ResendException {
170+
AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId + "/publish", super.apiKey, HttpMethod.POST, "", MediaType.get("application/json"));
171+
172+
if (!response.isSuccessful()) {
173+
throw new ResendException(response.getCode(), response.getBody());
174+
}
175+
176+
String responseBody = response.getBody();
177+
178+
return resendMapper.readValue(responseBody, PublishTemplateResponseSuccess.class);
179+
}
180+
}

0 commit comments

Comments
 (0)