-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented Email Templates API #36
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
73 changes: 73 additions & 0 deletions
73
examples/java/io/mailtrap/examples/emailtemplates/EmailTemplates.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,73 @@ | ||
| package io.mailtrap.examples.emailtemplates; | ||
|
|
||
| import io.mailtrap.config.MailtrapConfig; | ||
| import io.mailtrap.factory.MailtrapClientFactory; | ||
| import io.mailtrap.model.request.emailtemplates.CreateEmailTemplateRequest; | ||
| import io.mailtrap.model.request.emailtemplates.EmailTemplate; | ||
| import io.mailtrap.model.request.emailtemplates.UpdateEmailTemplateRequest; | ||
| import io.mailtrap.model.response.emailtemplates.EmailTemplateResponse; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class EmailTemplates { | ||
|
|
||
| private static final String TOKEN = "<YOUR MAILTRAP TOKEN>"; | ||
| private static final long ACCOUNT_ID = 1L; | ||
| private static final String EMAIL_TEMPLATE_NAME = "My Email Template"; | ||
| private static final String EMAIL_TEMPLATE_CATEGORY = "Promotion"; | ||
| private static final String EMAIL_TEMPLATE_SUBJECT = "Promotion Template subject"; | ||
| private static final String EMAIL_TEMPLATE_BODY_TEXT = "Promotion Text body"; | ||
| private static final String EMAIL_TEMPLATE_BODY_HTML = "<div>Promotion body</div>"; | ||
| private static final String UPDATED_EMAIL_TEMPLATE_NAME = "My Updated Email Template"; | ||
|
|
||
| public static void main(String[] args) { | ||
| final var config = new MailtrapConfig.Builder() | ||
| .token(TOKEN) | ||
| .build(); | ||
|
|
||
| new EmailTemplate(EMAIL_TEMPLATE_NAME, EMAIL_TEMPLATE_CATEGORY, EMAIL_TEMPLATE_SUBJECT, EMAIL_TEMPLATE_BODY_TEXT, EMAIL_TEMPLATE_BODY_HTML); | ||
|
|
||
| final var client = MailtrapClientFactory.createMailtrapClient(config); | ||
|
|
||
| final var createRequest = new CreateEmailTemplateRequest( | ||
| new EmailTemplate( | ||
| EMAIL_TEMPLATE_NAME, | ||
| EMAIL_TEMPLATE_CATEGORY, | ||
| EMAIL_TEMPLATE_SUBJECT, | ||
| EMAIL_TEMPLATE_BODY_TEXT, | ||
| EMAIL_TEMPLATE_BODY_HTML | ||
| )); | ||
|
|
||
| final var createdEmailTemplate = client.emailTemplatesApi().emailTemplates() | ||
| .createEmailTemplate(ACCOUNT_ID, createRequest); | ||
|
|
||
| System.out.println(createdEmailTemplate); | ||
|
|
||
| final var allTemplates = client.emailTemplatesApi().emailTemplates() | ||
| .getAllTemplates(ACCOUNT_ID); | ||
|
|
||
| System.out.println(allTemplates); | ||
|
|
||
| final var emailTemplate = client.emailTemplatesApi().emailTemplates() | ||
| .getEmailTemplate(ACCOUNT_ID, createdEmailTemplate.getId()); | ||
|
|
||
| System.out.println(emailTemplate); | ||
|
|
||
| final var updateRequest = new UpdateEmailTemplateRequest( | ||
| new EmailTemplate( | ||
| UPDATED_EMAIL_TEMPLATE_NAME, | ||
| EMAIL_TEMPLATE_CATEGORY, | ||
| EMAIL_TEMPLATE_SUBJECT, | ||
| EMAIL_TEMPLATE_BODY_TEXT, | ||
| EMAIL_TEMPLATE_BODY_HTML | ||
| )); | ||
|
|
||
| final var updatedEmailTemplate = client.emailTemplatesApi().emailTemplates() | ||
| .updateEmailTemplate(ACCOUNT_ID, createdEmailTemplate.getId(), updateRequest); | ||
|
|
||
| System.out.println(updatedEmailTemplate); | ||
|
|
||
| client.emailTemplatesApi().emailTemplates() | ||
| .deleteEmailTemplate(ACCOUNT_ID, updatedEmailTemplate.getId()); | ||
| } | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
src/main/java/io/mailtrap/api/emailtemplates/EmailTemplates.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,55 @@ | ||
| package io.mailtrap.api.emailtemplates; | ||
|
|
||
| import io.mailtrap.model.request.emailtemplates.CreateEmailTemplateRequest; | ||
| import io.mailtrap.model.request.emailtemplates.UpdateEmailTemplateRequest; | ||
| import io.mailtrap.model.response.emailtemplates.EmailTemplateResponse; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface EmailTemplates { | ||
|
|
||
| /** | ||
| * Get all email templates existing in your account | ||
| * | ||
| * @param accountId unique account ID | ||
| * @return list of existing email templates | ||
| */ | ||
| List<EmailTemplateResponse> getAllTemplates(long accountId); | ||
|
|
||
| /** | ||
| * Create a new email template | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param request email template create payload | ||
| * @return created email template | ||
| */ | ||
| EmailTemplateResponse createEmailTemplate(long accountId, CreateEmailTemplateRequest request); | ||
|
|
||
| /** | ||
| * Get an email template by ID | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param emailTemplateId unique email template ID | ||
| * @return email template attributes | ||
| */ | ||
| EmailTemplateResponse getEmailTemplate(long accountId, long emailTemplateId); | ||
|
|
||
| /** | ||
| * Update an email template | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param emailTemplateId unique email template ID | ||
| * @param request email template update payload | ||
| * @return updated email template | ||
| */ | ||
| EmailTemplateResponse updateEmailTemplate(long accountId, long emailTemplateId, UpdateEmailTemplateRequest request); | ||
|
|
||
| /** | ||
| * Delete an email template | ||
| * | ||
| * @param accountId unique account ID | ||
| * @param emailTemplateId unique email template ID | ||
| */ | ||
| void deleteEmailTemplate(long accountId, long emailTemplateId); | ||
|
|
||
| } |
76 changes: 76 additions & 0 deletions
76
src/main/java/io/mailtrap/api/emailtemplates/EmailTemplatesImpl.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,76 @@ | ||
| package io.mailtrap.api.emailtemplates; | ||
|
|
||
| import io.mailtrap.Constants; | ||
| import io.mailtrap.CustomValidator; | ||
| import io.mailtrap.api.apiresource.ApiResourceWithValidation; | ||
| import io.mailtrap.config.MailtrapConfig; | ||
| import io.mailtrap.http.RequestData; | ||
| import io.mailtrap.model.request.emailtemplates.CreateEmailTemplateRequest; | ||
| import io.mailtrap.model.request.emailtemplates.UpdateEmailTemplateRequest; | ||
| import io.mailtrap.model.response.emailtemplates.EmailTemplateResponse; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class EmailTemplatesImpl extends ApiResourceWithValidation implements EmailTemplates { | ||
|
|
||
| public EmailTemplatesImpl(MailtrapConfig config, CustomValidator customValidator) { | ||
| super(config, customValidator); | ||
| this.apiHost = Constants.GENERAL_HOST; | ||
| } | ||
|
|
||
| @Override | ||
| public List<EmailTemplateResponse> getAllTemplates(long accountId) { | ||
| return | ||
| httpClient.getList( | ||
| String.format(apiHost + "/api/accounts/%d/email_templates", accountId), | ||
| new RequestData(), | ||
| EmailTemplateResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public EmailTemplateResponse createEmailTemplate(long accountId, CreateEmailTemplateRequest request) { | ||
| validateRequestBodyAndThrowException(request); | ||
|
|
||
| return | ||
| httpClient.post( | ||
| String.format(apiHost + "/api/accounts/%d/email_templates", accountId), | ||
| request, | ||
| new RequestData(), | ||
| EmailTemplateResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public EmailTemplateResponse getEmailTemplate(long accountId, long emailTemplateId) { | ||
| return | ||
| httpClient.get( | ||
| String.format(apiHost + "/api/accounts/%d/email_templates/%d", accountId, emailTemplateId), | ||
| new RequestData(), | ||
| EmailTemplateResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public EmailTemplateResponse updateEmailTemplate(long accountId, long emailTemplateId, UpdateEmailTemplateRequest request) { | ||
| validateRequestBodyAndThrowException(request); | ||
|
|
||
| return | ||
| httpClient.patch( | ||
| String.format(apiHost + "/api/accounts/%d/email_templates/%d", accountId, emailTemplateId), | ||
| request, | ||
| new RequestData(), | ||
| EmailTemplateResponse.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteEmailTemplate(long accountId, long emailTemplateId) { | ||
| httpClient | ||
| .delete( | ||
| String.format(apiHost + "/api/accounts/%d/email_templates/%d", accountId, emailTemplateId), | ||
| new RequestData(), | ||
| Void.class | ||
| ); | ||
| } | ||
| } |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/io/mailtrap/client/api/MailtrapEmailTemplatesApi.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,13 @@ | ||
| package io.mailtrap.client.api; | ||
|
|
||
| import io.mailtrap.api.emailtemplates.EmailTemplates; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.experimental.Accessors; | ||
|
|
||
| @Getter | ||
| @Accessors(fluent = true) | ||
| @RequiredArgsConstructor | ||
| public class MailtrapEmailTemplatesApi { | ||
| private final EmailTemplates emailTemplates; | ||
| } |
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
19 changes: 19 additions & 0 deletions
19
src/main/java/io/mailtrap/model/request/emailtemplates/CreateEmailTemplateRequest.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,19 @@ | ||
| package io.mailtrap.model.request.emailtemplates; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import io.mailtrap.model.AbstractModel; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class CreateEmailTemplateRequest extends AbstractModel { | ||
|
|
||
| @Valid | ||
| @NotNull | ||
| @JsonProperty("email_template") | ||
| private EmailTemplate emailTemplate; | ||
|
|
||
| } |
33 changes: 33 additions & 0 deletions
33
src/main/java/io/mailtrap/model/request/emailtemplates/EmailTemplate.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,33 @@ | ||
| package io.mailtrap.model.request.emailtemplates; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class EmailTemplate { | ||
|
|
||
| @NotNull | ||
| @Size(min = 1, max = 255) | ||
| private String name; | ||
|
|
||
| @NotNull | ||
| @Size(min = 1, max = 255) | ||
| private String category; | ||
|
|
||
| @NotNull | ||
| @Size(min = 1, max = 255) | ||
| private String subject; | ||
|
|
||
| @Size(max = 10_000_000) | ||
| @JsonProperty("body_text") | ||
| private String bodyText; | ||
|
|
||
| @Size(max = 10_000_000) | ||
| @JsonProperty("body_html") | ||
| private String bodyHtml; | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/io/mailtrap/model/request/emailtemplates/UpdateEmailTemplateRequest.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,21 @@ | ||
| package io.mailtrap.model.request.emailtemplates; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import io.mailtrap.model.AbstractModel; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public class UpdateEmailTemplateRequest extends AbstractModel { | ||
|
|
||
| @Valid | ||
| @NotNull | ||
| @JsonProperty("email_template") | ||
| private EmailTemplate emailTemplate; | ||
|
|
||
| } |
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
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
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.