-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Implement templates module (beta) #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
src/main/java/com/resend/services/templates/Templates.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| package com.resend.services.templates; | ||
|
|
||
| import com.resend.core.exception.ResendException; | ||
| import com.resend.core.helper.URLHelper; | ||
| import com.resend.core.net.AbstractHttpResponse; | ||
| import com.resend.core.net.HttpMethod; | ||
| import com.resend.core.net.ListParams; | ||
| import com.resend.core.service.BaseService; | ||
| import com.resend.services.templates.model.*; | ||
| import okhttp3.MediaType; | ||
|
|
||
| /** | ||
| * Represents the Resend Templates module. | ||
| */ | ||
| public final class Templates extends BaseService { | ||
|
|
||
| /** | ||
| * Constructs an instance of the {@code Templates} class. | ||
| * | ||
| * @param apiKey The apiKey used for authentication. | ||
| */ | ||
| public Templates(final String apiKey) { | ||
| super(apiKey); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new template. | ||
| * | ||
| * @param options The options for creating the template. | ||
| * @return The response indicating the status of the template creation. | ||
| * @throws ResendException If an error occurs while creating the template. | ||
| */ | ||
| public CreateTemplateResponseSuccess create(CreateTemplateOptions options) throws ResendException { | ||
| String payload = super.resendMapper.writeValue(options); | ||
| AbstractHttpResponse<String> response = super.httpClient.perform("/templates", super.apiKey, HttpMethod.POST, payload, MediaType.get("application/json")); | ||
|
|
||
| if (!response.isSuccessful()) { | ||
| throw new ResendException(response.getCode(), response.getBody()); | ||
| } | ||
|
|
||
| String responseBody = response.getBody(); | ||
|
|
||
| return resendMapper.readValue(responseBody, CreateTemplateResponseSuccess.class); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves a template by its unique identifier or alias. | ||
| * | ||
| * @param templateId The unique identifier or alias of the template. | ||
| * @return The retrieved template's details. | ||
| * @throws ResendException If an error occurs while retrieving the template. | ||
| */ | ||
| public GetTemplateResponseSuccess get(String templateId) throws ResendException { | ||
| AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json")); | ||
|
|
||
| if (!response.isSuccessful()) { | ||
| throw new ResendException(response.getCode(), response.getBody()); | ||
| } | ||
|
|
||
| String responseBody = response.getBody(); | ||
|
|
||
| return resendMapper.readValue(responseBody, GetTemplateResponseSuccess.class); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves a list of templates. | ||
| * | ||
| * @return A ListTemplatesResponse containing the list of templates. | ||
| * @throws ResendException If an error occurs during the templates list retrieval process. | ||
| */ | ||
| public ListTemplatesResponseSuccess list() throws ResendException { | ||
| AbstractHttpResponse<String> response = this.httpClient.perform("/templates", super.apiKey, HttpMethod.GET, null, MediaType.get("application/json")); | ||
|
|
||
| if (!response.isSuccessful()) { | ||
| throw new ResendException(response.getCode(), response.getBody()); | ||
| } | ||
|
|
||
| String responseBody = response.getBody(); | ||
|
|
||
| return resendMapper.readValue(responseBody, ListTemplatesResponseSuccess.class); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves a paginated list of templates. | ||
| * | ||
| * @param params The params used to customize the list. | ||
| * @return A ListTemplatesResponse containing the paginated list of templates. | ||
| * @throws ResendException If an error occurs during the templates list retrieval process. | ||
| */ | ||
| public ListTemplatesResponseSuccess list(ListParams params) throws ResendException { | ||
| String pathWithQuery = "/templates" + URLHelper.parse(params); | ||
| AbstractHttpResponse<String> response = this.httpClient.perform(pathWithQuery, super.apiKey, HttpMethod.GET, null, MediaType.get("application/json")); | ||
|
|
||
| if (!response.isSuccessful()) { | ||
| throw new ResendException(response.getCode(), response.getBody()); | ||
| } | ||
|
|
||
| String responseBody = response.getBody(); | ||
|
|
||
| return resendMapper.readValue(responseBody, ListTemplatesResponseSuccess.class); | ||
| } | ||
|
|
||
| /** | ||
| * Updates a template by its unique identifier or alias. | ||
| * | ||
| * @param templateId The unique identifier or alias of the template. | ||
| * @param options The options for updating the template. | ||
| * @return The response indicating the status of the template update. | ||
| * @throws ResendException If an error occurs while updating the template. | ||
| */ | ||
| public UpdateTemplateResponseSuccess update(String templateId, UpdateTemplateOptions options) throws ResendException { | ||
| String payload = super.resendMapper.writeValue(options); | ||
| AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId, super.apiKey, HttpMethod.PATCH, payload, MediaType.get("application/json")); | ||
|
|
||
| if (!response.isSuccessful()) { | ||
| throw new ResendException(response.getCode(), response.getBody()); | ||
| } | ||
|
|
||
| String responseBody = response.getBody(); | ||
|
|
||
| return resendMapper.readValue(responseBody, UpdateTemplateResponseSuccess.class); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a template by its unique identifier or alias. | ||
| * | ||
| * @param templateId The unique identifier or alias of the template. | ||
| * @return The response indicating the status of the template deletion. | ||
| * @throws ResendException If an error occurs while deleting the template. | ||
| */ | ||
| public DeleteTemplateResponseSuccess remove(String templateId) throws ResendException { | ||
| AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId, super.apiKey, HttpMethod.DELETE, null, MediaType.get("application/json")); | ||
|
|
||
| if (!response.isSuccessful()) { | ||
| throw new ResendException(response.getCode(), response.getBody()); | ||
| } | ||
|
|
||
| String responseBody = response.getBody(); | ||
|
|
||
| return resendMapper.readValue(responseBody, DeleteTemplateResponseSuccess.class); | ||
| } | ||
|
|
||
| /** | ||
| * Duplicates a template by its unique identifier or alias. | ||
| * | ||
| * @param templateId The unique identifier or alias of the template. | ||
| * @return The response indicating the status of the template duplication. | ||
| * @throws ResendException If an error occurs while duplicating the template. | ||
| */ | ||
| public DuplicateTemplateResponseSuccess duplicate(String templateId) throws ResendException { | ||
| AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId + "/duplicate", super.apiKey, HttpMethod.POST, "", MediaType.get("application/json")); | ||
|
|
||
| if (!response.isSuccessful()) { | ||
| throw new ResendException(response.getCode(), response.getBody()); | ||
| } | ||
|
|
||
| String responseBody = response.getBody(); | ||
|
|
||
| return resendMapper.readValue(responseBody, DuplicateTemplateResponseSuccess.class); | ||
| } | ||
|
|
||
| /** | ||
| * Publishes a template by its unique identifier or alias. | ||
| * | ||
| * @param templateId The unique identifier or alias of the template. | ||
| * @return The response indicating the status of the template publication. | ||
| * @throws ResendException If an error occurs while publishing the template. | ||
| */ | ||
| public PublishTemplateResponseSuccess publish(String templateId) throws ResendException { | ||
| AbstractHttpResponse<String> response = this.httpClient.perform("/templates/" + templateId + "/publish", super.apiKey, HttpMethod.POST, "", MediaType.get("application/json")); | ||
|
|
||
| if (!response.isSuccessful()) { | ||
| throw new ResendException(response.getCode(), response.getBody()); | ||
| } | ||
|
|
||
| String responseBody = response.getBody(); | ||
|
|
||
| return resendMapper.readValue(responseBody, PublishTemplateResponseSuccess.class); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.