|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from mailtrap.http import HttpClient |
| 4 | +from mailtrap.models.common import DeletedObject |
| 5 | +from mailtrap.models.contacts import ContactList |
| 6 | +from mailtrap.models.contacts import ContactListParams |
| 7 | + |
| 8 | + |
| 9 | +class ContactListsApi: |
| 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[ContactList]: |
| 15 | + response = self._client.get(self._api_path()) |
| 16 | + return [ContactList(**field) for field in response] |
| 17 | + |
| 18 | + def get_by_id(self, list_id: int) -> ContactList: |
| 19 | + response = self._client.get(self._api_path(list_id)) |
| 20 | + return ContactList(**response) |
| 21 | + |
| 22 | + def create(self, list_params: ContactListParams) -> ContactList: |
| 23 | + response = self._client.post( |
| 24 | + self._api_path(), |
| 25 | + json=list_params.api_data, |
| 26 | + ) |
| 27 | + return ContactList(**response) |
| 28 | + |
| 29 | + def update(self, list_id: int, list_params: ContactListParams) -> ContactList: |
| 30 | + response = self._client.patch( |
| 31 | + self._api_path(list_id), |
| 32 | + json=list_params.api_data, |
| 33 | + ) |
| 34 | + return ContactList(**response) |
| 35 | + |
| 36 | + def delete(self, list_id: int) -> DeletedObject: |
| 37 | + self._client.delete(self._api_path(list_id)) |
| 38 | + return DeletedObject(list_id) |
| 39 | + |
| 40 | + def _api_path(self, list_id: Optional[int] = None) -> str: |
| 41 | + path = f"/api/accounts/{self._account_id}/contacts/lists" |
| 42 | + if list_id is not None: |
| 43 | + return f"{path}/{list_id}" |
| 44 | + return path |
0 commit comments