-
Notifications
You must be signed in to change notification settings - Fork 8
Fix issue #18: Add api for EmailTemplates, add tests and examples #34
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| from typing import Optional | ||
|
|
||
| import mailtrap as mt | ||
| from mailtrap.models.common import DeletedObject | ||
| from mailtrap.models.templates import EmailTemplate | ||
|
|
||
| API_TOKEN = "YOU_API_TOKEN" | ||
| ACCOUNT_ID = "YOU_ACCOUNT_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID) | ||
| templates_api = client.email_templates_api.templates | ||
|
|
||
|
|
||
| def list_templates() -> list[EmailTemplate]: | ||
| return templates_api.get_list() | ||
|
|
||
|
|
||
| def create_template( | ||
| name: str, | ||
| subject: str, | ||
| category: str, | ||
| body_text: Optional[str] = None, | ||
| body_html: Optional[str] = None, | ||
| ) -> EmailTemplate: | ||
| params = mt.CreateEmailTemplateParams( | ||
| name=name, | ||
| subject=subject, | ||
| category=category, | ||
| body_text=body_text, | ||
| body_html=body_html, | ||
| ) | ||
| return templates_api.create(params) | ||
|
|
||
|
|
||
| def get_template(template_id: str) -> EmailTemplate: | ||
| return templates_api.get_by_id(template_id) | ||
|
|
||
|
|
||
| def update_template( | ||
| template_id: str, | ||
| name: Optional[str] = None, | ||
| subject: Optional[str] = None, | ||
| category: Optional[str] = None, | ||
| body_text: Optional[str] = None, | ||
| body_html: Optional[str] = None, | ||
| ) -> EmailTemplate: | ||
| params = mt.UpdateEmailTemplateParams( | ||
| name=name, | ||
| subject=subject, | ||
| category=category, | ||
| body_text=body_text, | ||
| body_html=body_html, | ||
| ) | ||
| return templates_api.update(template_id, params) | ||
|
|
||
|
|
||
| def delete_template(template_id: str) -> DeletedObject: | ||
| return templates_api.delete(template_id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(list_templates()) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.common import DeletedObject | ||
| from mailtrap.models.templates import CreateEmailTemplateParams | ||
| from mailtrap.models.templates import EmailTemplate | ||
| from mailtrap.models.templates import UpdateEmailTemplateParams | ||
|
|
||
|
|
||
| class TemplatesApi: | ||
| def __init__(self, client: HttpClient, account_id: str) -> None: | ||
| self._account_id = account_id | ||
| self._client = client | ||
|
|
||
| def get_list(self) -> list[EmailTemplate]: | ||
| response = self._client.get(f"/api/accounts/{self._account_id}/email_templates") | ||
| return [EmailTemplate(**template) for template in response] | ||
|
|
||
| def get_by_id(self, template_id: int) -> EmailTemplate: | ||
| response = self._client.get( | ||
| f"/api/accounts/{self._account_id}/email_templates/{template_id}" | ||
| ) | ||
| return EmailTemplate(**response) | ||
|
|
||
| def create(self, template_params: CreateEmailTemplateParams) -> EmailTemplate: | ||
| response = self._client.post( | ||
| f"/api/accounts/{self._account_id}/email_templates", | ||
| json={"email_template": template_params.api_data}, | ||
| ) | ||
| return EmailTemplate(**response) | ||
|
|
||
| def update( | ||
| self, template_id: int, template_params: UpdateEmailTemplateParams | ||
| ) -> EmailTemplate: | ||
| response = self._client.patch( | ||
| f"/api/accounts/{self._account_id}/email_templates/{template_id}", | ||
| json={"email_template": template_params.api_data}, | ||
| ) | ||
| return EmailTemplate(**response) | ||
|
|
||
| def delete(self, template_id: int) -> DeletedObject: | ||
| self._client.delete( | ||
| f"/api/accounts/{self._account_id}/email_templates/{template_id}" | ||
| ) | ||
| return DeletedObject(template_id) | ||
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,12 @@ | ||
| from mailtrap.api.resources.templates import TemplatesApi | ||
| from mailtrap.http import HttpClient | ||
|
|
||
|
|
||
| class EmailTemplatesApi: | ||
| def __init__(self, client: HttpClient, account_id: str) -> None: | ||
| self._account_id = account_id | ||
| self._client = client | ||
|
|
||
| @property | ||
| def templates(self) -> TemplatesApi: | ||
| return TemplatesApi(account_id=self._account_id, client=self._client) |
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
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
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
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,49 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic.dataclasses import dataclass | ||
|
|
||
| from mailtrap.models.common import RequestParams | ||
|
|
||
|
|
||
| @dataclass | ||
| class CreateEmailTemplateParams(RequestParams): | ||
| name: str | ||
| subject: str | ||
| category: str | ||
| body_text: Optional[str] = None | ||
| body_html: Optional[str] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class UpdateEmailTemplateParams(RequestParams): | ||
| name: Optional[str] = None | ||
| subject: Optional[str] = None | ||
| category: Optional[str] = None | ||
| body_text: Optional[str] = None | ||
| body_html: Optional[str] = None | ||
|
|
||
| def __post_init__(self) -> None: | ||
| if all( | ||
| value is None | ||
| for value in [ | ||
| self.name, | ||
| self.subject, | ||
| self.category, | ||
| self.body_text, | ||
| self.body_html, | ||
| ] | ||
| ): | ||
| raise ValueError("At least one field must be provided for update action") | ||
|
|
||
|
|
||
| @dataclass | ||
| class EmailTemplate: | ||
| id: int | ||
| name: str | ||
| uuid: str | ||
| category: str | ||
| subject: str | ||
| body_text: Optional[str] | ||
| body_html: Optional[str] | ||
| created_at: str | ||
| updated_at: str |
Oops, something went wrong.
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.