|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from mailtrap.http import HttpClient |
| 4 | +from mailtrap.models.inboxes import CreateInboxParams |
| 5 | +from mailtrap.models.inboxes import Inbox |
| 6 | +from mailtrap.models.inboxes import UpdateInboxParams |
| 7 | + |
| 8 | + |
| 9 | +class InboxesApi: |
| 10 | + def __init__(self, client: HttpClient, account_id: str) -> None: |
| 11 | + self._account_id = account_id |
| 12 | + self._client = client |
| 13 | + |
| 14 | + def get_list(self) -> list[Inbox]: |
| 15 | + """Get a list of inboxes.""" |
| 16 | + response = self._client.get(self._api_path()) |
| 17 | + return [Inbox(**inbox) for inbox in response] |
| 18 | + |
| 19 | + def get_by_id(self, inbox_id: int) -> Inbox: |
| 20 | + """Get inbox attributes by inbox id.""" |
| 21 | + response = self._client.get(self._api_path(inbox_id)) |
| 22 | + return Inbox(**response) |
| 23 | + |
| 24 | + def create(self, project_id: int, inbox_params: CreateInboxParams) -> Inbox: |
| 25 | + """Create an inbox in a project.""" |
| 26 | + response = self._client.post( |
| 27 | + f"/api/accounts/{self._account_id}/projects/{project_id}/inboxes", |
| 28 | + json={"inbox": inbox_params.api_data}, |
| 29 | + ) |
| 30 | + return Inbox(**response) |
| 31 | + |
| 32 | + def update(self, inbox_id: int, inbox_params: UpdateInboxParams) -> Inbox: |
| 33 | + """Update inbox name, inbox email username.""" |
| 34 | + response = self._client.patch( |
| 35 | + self._api_path(inbox_id), |
| 36 | + json={"inbox": inbox_params.api_data}, |
| 37 | + ) |
| 38 | + return Inbox(**response) |
| 39 | + |
| 40 | + def delete(self, inbox_id: int) -> Inbox: |
| 41 | + """Delete an inbox with all its emails.""" |
| 42 | + response = self._client.delete(self._api_path(inbox_id)) |
| 43 | + return Inbox(**response) |
| 44 | + |
| 45 | + def clean(self, inbox_id: int) -> Inbox: |
| 46 | + """Delete all messages (emails) from inbox.""" |
| 47 | + response = self._client.patch(f"{self._api_path(inbox_id)}/clean") |
| 48 | + return Inbox(**response) |
| 49 | + |
| 50 | + def mark_as_read(self, inbox_id: int) -> Inbox: |
| 51 | + """Mark all messages in the inbox as read.""" |
| 52 | + response = self._client.patch(f"{self._api_path(inbox_id)}/all_read") |
| 53 | + return Inbox(**response) |
| 54 | + |
| 55 | + def reset_credentials(self, inbox_id: int) -> Inbox: |
| 56 | + """Reset SMTP credentials of the inbox.""" |
| 57 | + response = self._client.patch(f"{self._api_path(inbox_id)}/reset_credentials") |
| 58 | + return Inbox(**response) |
| 59 | + |
| 60 | + def enable_email_address(self, inbox_id: int) -> Inbox: |
| 61 | + """Turn the email address of the inbox on/off.""" |
| 62 | + response = self._client.patch(f"{self._api_path(inbox_id)}/toggle_email_username") |
| 63 | + return Inbox(**response) |
| 64 | + |
| 65 | + def reset_email_username(self, inbox_id: int) -> Inbox: |
| 66 | + """Reset username of email address per inbox.""" |
| 67 | + response = self._client.patch(f"{self._api_path(inbox_id)}/reset_email_username") |
| 68 | + return Inbox(**response) |
| 69 | + |
| 70 | + def _api_path(self, inbox_id: Optional[int] = None) -> str: |
| 71 | + path = f"/api/accounts/{self._account_id}/inboxes" |
| 72 | + if inbox_id: |
| 73 | + return f"{path}/{inbox_id}" |
| 74 | + return path |
0 commit comments