Skip to content

Commit 9a7ca5f

Browse files
Ihor BilousIhor Bilous
authored andcommitted
Fix issue #20: Add ContactListsApi, related models, tests, examples
1 parent d3cac02 commit 9a7ca5f

File tree

8 files changed

+430
-0
lines changed

8 files changed

+430
-0
lines changed

examples/contacts/contact_lists.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import mailtrap as mt
2+
from mailtrap.models.common import DeletedObject
3+
from mailtrap.models.contacts import ContactField
4+
5+
API_TOKEN = "YOU_API_TOKEN"
6+
ACCOUNT_ID = "YOU_ACCOUNT_ID"
7+
8+
client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID)
9+
contact_lists_api = client.contacts_api.contact_lists
10+
11+
12+
def create_contact_list(name: str) -> ContactField:
13+
params = mt.ContactListParams(name=name)
14+
return contact_lists_api.create(params)
15+
16+
17+
def update_contact_list(contact_list_id: int, name: str) -> ContactField:
18+
params = mt.ContactListParams(name=name)
19+
return contact_lists_api.update(contact_list_id, params)
20+
21+
22+
def list_contact_lists() -> list[ContactField]:
23+
return contact_lists_api.get_list()
24+
25+
26+
def get_contact_list(contact_list_id: int) -> ContactField:
27+
return contact_lists_api.get_by_id(contact_list_id)
28+
29+
30+
def delete_contact_list(contact_list_id: int) -> DeletedObject:
31+
return contact_lists_api.delete(contact_list_id)
32+
33+
34+
if __name__ == "__main__":
35+
print(list_contact_lists())

mailtrap/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .exceptions import ClientConfigurationError
66
from .exceptions import MailtrapError
77
from .models.contacts import ContactField
8+
from .models.contacts import ContactListParams
89
from .models.contacts import CreateContactFieldParams
910
from .models.contacts import UpdateContactFieldParams
1011
from .models.mail import Address

mailtrap/api/contacts.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from mailtrap.api.resources.contact_fields import ContactFieldsApi
2+
from mailtrap.api.resources.contact_lists import ContactListsApi
23
from mailtrap.http import HttpClient
34

45

@@ -10,3 +11,7 @@ def __init__(self, client: HttpClient, account_id: str) -> None:
1011
@property
1112
def contact_fields(self) -> ContactFieldsApi:
1213
return ContactFieldsApi(account_id=self._account_id, client=self._client)
14+
15+
@property
16+
def contact_lists(self) -> ContactListsApi:
17+
return ContactListsApi(account_id=self._account_id, client=self._client)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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:
43+
return f"{path}/{list_id}"
44+
return path

mailtrap/models/contacts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,14 @@ class ContactField:
2828
name: str
2929
data_type: str
3030
merge_tag: str
31+
32+
33+
@dataclass
34+
class ContactListParams(RequestParams):
35+
name: str
36+
37+
38+
@dataclass
39+
class ContactList:
40+
id: int
41+
name: str

0 commit comments

Comments
 (0)