Skip to content

Commit 6e002ac

Browse files
Ihor BilousIhor Bilous
authored andcommitted
Fix issue #19: Add ContactFieldsApi, related models, tests, examples
1 parent d139f84 commit 6e002ac

File tree

8 files changed

+535
-0
lines changed

8 files changed

+535
-0
lines changed

examples/contacts/contact_fields.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import Optional
2+
3+
import mailtrap as mt
4+
from mailtrap.models.common import DeletedObject
5+
from mailtrap.models.contacts import ContactField
6+
7+
API_TOKEN = "YOU_API_TOKEN"
8+
ACCOUNT_ID = "YOU_ACCOUNT_ID"
9+
10+
client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID)
11+
contact_fields_api = client.contacts_api.contact_fields
12+
13+
14+
def create_contact_field(
15+
name: str,
16+
data_type: str,
17+
merge_tag: str,
18+
) -> ContactField:
19+
params = mt.CreateContactFieldParams(
20+
name=name,
21+
data_type=data_type,
22+
merge_tag=merge_tag,
23+
)
24+
return contact_fields_api.create(params)
25+
26+
27+
def update_contact_field(
28+
contact_field_id: str,
29+
name: Optional[str] = None,
30+
merge_tag: Optional[str] = None,
31+
) -> ContactField:
32+
params = mt.UpdateContactFieldParams(name=name, merge_tag=merge_tag)
33+
return contact_fields_api.update(contact_field_id, params)
34+
35+
36+
def list_contact_fields() -> list[ContactField]:
37+
return contact_fields_api.get_list()
38+
39+
40+
def get_contact_field(contact_field_id: str) -> ContactField:
41+
return contact_fields_api.get_by_id(contact_field_id)
42+
43+
44+
def delete_contact_field(contact_field_id: str) -> DeletedObject:
45+
return contact_fields_api.delete(contact_field_id)
46+
47+
48+
if __name__ == "__main__":
49+
print(list_contact_fields())

mailtrap/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from .exceptions import AuthorizationError
55
from .exceptions import ClientConfigurationError
66
from .exceptions import MailtrapError
7+
from .models.contacts import ContactField
8+
from .models.contacts import CreateContactFieldParams
9+
from .models.contacts import UpdateContactFieldParams
710
from .models.mail import Address
811
from .models.mail import Attachment
912
from .models.mail import BaseMail

mailtrap/api/contacts.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from mailtrap.api.resources.contact_fields import ContactFieldsApi
2+
from mailtrap.http import HttpClient
3+
4+
5+
class ContactsBaseApi:
6+
def __init__(self, client: HttpClient, account_id: str) -> None:
7+
self._account_id = account_id
8+
self._client = client
9+
10+
@property
11+
def contact_fields(self) -> ContactFieldsApi:
12+
return ContactFieldsApi(account_id=self._account_id, client=self._client)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from mailtrap.http import HttpClient
2+
from mailtrap.models.common import DeletedObject
3+
from mailtrap.models.contacts import ContactField
4+
from mailtrap.models.contacts import CreateContactFieldParams
5+
from mailtrap.models.contacts import UpdateContactFieldParams
6+
7+
8+
class ContactFieldsApi:
9+
def __init__(self, client: HttpClient, account_id: str) -> None:
10+
self._account_id = account_id
11+
self._client = client
12+
13+
def get_list(self) -> list[ContactField]:
14+
response = self._client.get(f"/api/accounts/{self._account_id}/contacts/fields")
15+
return [ContactField(**field) for field in response]
16+
17+
def get_by_id(self, field_id: int) -> ContactField:
18+
response = self._client.get(
19+
f"/api/accounts/{self._account_id}/contacts/fields/{field_id}"
20+
)
21+
return ContactField(**response)
22+
23+
def create(self, field_params: CreateContactFieldParams) -> ContactField:
24+
response = self._client.post(
25+
f"/api/accounts/{self._account_id}/contacts/fields",
26+
json=field_params.api_data,
27+
)
28+
return ContactField(**response)
29+
30+
def update(
31+
self, field_id: int, field_params: UpdateContactFieldParams
32+
) -> ContactField:
33+
response = self._client.patch(
34+
f"/api/accounts/{self._account_id}/contacts/fields/{field_id}",
35+
json=field_params.api_data,
36+
)
37+
return ContactField(**response)
38+
39+
def delete(self, field_id: int) -> DeletedObject:
40+
self._client.delete(
41+
f"/api/accounts/{self._account_id}/contacts/fields/{field_id}"
42+
)
43+
return DeletedObject(field_id)

mailtrap/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from pydantic import TypeAdapter
77

8+
from mailtrap.api.contacts import ContactsBaseApi
89
from mailtrap.api.sending import SendingApi
910
from mailtrap.api.templates import EmailTemplatesApi
1011
from mailtrap.api.testing import TestingApi
@@ -63,6 +64,14 @@ def email_templates_api(self) -> EmailTemplatesApi:
6364
client=HttpClient(host=GENERAL_HOST, headers=self.headers),
6465
)
6566

67+
@property
68+
def contacts_api(self) -> ContactsBaseApi:
69+
self._validate_account_id()
70+
return ContactsBaseApi(
71+
account_id=cast(str, self.account_id),
72+
client=HttpClient(host=GENERAL_HOST, headers=self.headers),
73+
)
74+
6675
@property
6776
def sending_api(self) -> SendingApi:
6877
http_client = HttpClient(host=self._sending_api_host, headers=self.headers)

mailtrap/models/contacts.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import Optional
2+
3+
from pydantic.dataclasses import dataclass
4+
5+
from mailtrap.models.common import RequestParams
6+
7+
8+
@dataclass
9+
class CreateContactFieldParams(RequestParams):
10+
name: str
11+
data_type: str
12+
merge_tag: str
13+
14+
15+
@dataclass
16+
class UpdateContactFieldParams(RequestParams):
17+
name: Optional[str] = None
18+
merge_tag: Optional[str] = None
19+
20+
def __post_init__(self) -> None:
21+
if all(value is None for value in [self.name, self.merge_tag]):
22+
raise ValueError("At least one field must be provided for update action")
23+
24+
25+
@dataclass
26+
class ContactField:
27+
id: int
28+
name: str
29+
data_type: str
30+
merge_tag: str

0 commit comments

Comments
 (0)