|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from mailtrap.http import HttpClient |
| 4 | +from mailtrap.models.common import DeletedObject |
| 5 | +from mailtrap.models.paginated_templates import CreateTemplateParams |
| 6 | +from mailtrap.models.paginated_templates import Template |
| 7 | +from mailtrap.models.paginated_templates import TemplateListParams |
| 8 | +from mailtrap.models.paginated_templates import TemplateListResponse |
| 9 | +from mailtrap.models.paginated_templates import TemplateResponse |
| 10 | +from mailtrap.models.paginated_templates import UpdateTemplateParams |
| 11 | + |
| 12 | + |
| 13 | +class PaginatedTemplatesApi: |
| 14 | + """ |
| 15 | + Templates API. The ``/api/templates`` endpoints are experimental: their |
| 16 | + request and response shapes may change before general availability. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, client: HttpClient, account_id: str) -> None: |
| 20 | + self._account_id = account_id |
| 21 | + self._client = client |
| 22 | + |
| 23 | + def get_list( |
| 24 | + self, params: Optional[TemplateListParams] = None |
| 25 | + ) -> TemplateListResponse: |
| 26 | + """ |
| 27 | + List email templates in the account, one page at a time. Unlike the |
| 28 | + ``email_templates_api`` list, it does not return every template: pass |
| 29 | + ``pagination.next_token`` with the same ``per_page`` to get the next |
| 30 | + page. Omit ``params`` for the first page with API defaults. |
| 31 | + """ |
| 32 | + query_params = params.api_query_params if params else None |
| 33 | + response = self._client.get(self._api_path(), params=query_params or None) |
| 34 | + return TemplateListResponse(**response) |
| 35 | + |
| 36 | + def get_by_id(self, template_id: int) -> Template: |
| 37 | + """Get an email template by ID.""" |
| 38 | + response = self._client.get(self._api_path(template_id)) |
| 39 | + return TemplateResponse(**response).data |
| 40 | + |
| 41 | + def create(self, template_params: CreateTemplateParams) -> Template: |
| 42 | + """Create a new email template.""" |
| 43 | + response = self._client.post(self._api_path(), json=template_params.api_data) |
| 44 | + return TemplateResponse(**response).data |
| 45 | + |
| 46 | + def update(self, template_id: int, template_params: UpdateTemplateParams) -> Template: |
| 47 | + """Update an email template. Only the supplied fields are changed.""" |
| 48 | + response = self._client.patch( |
| 49 | + self._api_path(template_id), json=template_params.api_data |
| 50 | + ) |
| 51 | + return TemplateResponse(**response).data |
| 52 | + |
| 53 | + def delete(self, template_id: int) -> DeletedObject: |
| 54 | + """Delete an email template.""" |
| 55 | + self._client.delete(self._api_path(template_id)) |
| 56 | + return DeletedObject(template_id) |
| 57 | + |
| 58 | + def _api_path(self, template_id: Optional[int] = None) -> str: |
| 59 | + path = f"/api/accounts/{self._account_id}/templates" |
| 60 | + if template_id is not None: |
| 61 | + return f"{path}/{template_id}" |
| 62 | + return path |
0 commit comments