|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from mailtrap.http import HttpClient |
| 4 | +from mailtrap.models.common import DeletedObject |
| 5 | +from mailtrap.models.sending_domains import CreateSendingDomainParams |
| 6 | +from mailtrap.models.sending_domains import SendingDomain |
| 7 | +from mailtrap.models.sending_domains import SendSetupInstructionsParams |
| 8 | +from mailtrap.models.sending_domains import SendSetupInstructionsResponse |
| 9 | + |
| 10 | + |
| 11 | +class SendingDomainsApi: |
| 12 | + def __init__(self, client: HttpClient, account_id: str) -> None: |
| 13 | + self._account_id = account_id |
| 14 | + self._client = client |
| 15 | + |
| 16 | + def get_list(self) -> list[SendingDomain]: |
| 17 | + """ |
| 18 | + Get sending domains and their statuses. |
| 19 | + """ |
| 20 | + response = self._client.get(self._api_path()) |
| 21 | + domains = response.get("data", []) |
| 22 | + return [SendingDomain(**domain) for domain in domains] |
| 23 | + |
| 24 | + def get_by_id(self, sending_domain_id: int) -> SendingDomain: |
| 25 | + """ |
| 26 | + Get domain data and its status. |
| 27 | + """ |
| 28 | + response = self._client.get(self._api_path(sending_domain_id)) |
| 29 | + return SendingDomain(**response) |
| 30 | + |
| 31 | + def create(self, domain_params: CreateSendingDomainParams) -> SendingDomain: |
| 32 | + """ |
| 33 | + Create a sending domain. To later check the status of the newly created domain, |
| 34 | + review the compliance_status and dns_verified fields in the response |
| 35 | + of the Get domain by ID or Get sending domains endpoints. |
| 36 | + """ |
| 37 | + response = self._client.post( |
| 38 | + self._api_path(), json={"sending_domain": domain_params.api_data} |
| 39 | + ) |
| 40 | + return SendingDomain(**response) |
| 41 | + |
| 42 | + def delete(self, sending_domain_id: int) -> DeletedObject: |
| 43 | + """ |
| 44 | + Delete a sending domain. |
| 45 | + """ |
| 46 | + self._client.delete(self._api_path(sending_domain_id)) |
| 47 | + return DeletedObject(id=sending_domain_id) |
| 48 | + |
| 49 | + def send_setup_instructions( |
| 50 | + self, |
| 51 | + sending_domain_id: int, |
| 52 | + instructions_params: SendSetupInstructionsParams, |
| 53 | + ) -> SendSetupInstructionsResponse: |
| 54 | + """ |
| 55 | + Send sending domain setup instructions. |
| 56 | + """ |
| 57 | + self._client.post( |
| 58 | + f"{self._api_path(sending_domain_id)}/send_setup_instructions", |
| 59 | + json=instructions_params.api_data, |
| 60 | + ) |
| 61 | + return SendSetupInstructionsResponse( |
| 62 | + message="Instructions email has been sent successfully" |
| 63 | + ) |
| 64 | + |
| 65 | + def _api_path(self, sending_domain_id: Optional[int] = None) -> str: |
| 66 | + path = f"/api/accounts/{self._account_id}/sending_domains" |
| 67 | + if sending_domain_id is not None: |
| 68 | + path = f"{path}/{sending_domain_id}" |
| 69 | + return path |
0 commit comments