Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/contacts/contact_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import mailtrap as mt
from mailtrap.models.common import DeletedObject
from mailtrap.models.contacts import ContactList

API_TOKEN = "YOU_API_TOKEN"
ACCOUNT_ID = "YOU_ACCOUNT_ID"

client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID)
contact_lists_api = client.contacts_api.contact_lists


def create_contact_list(name: str) -> ContactList:
params = mt.ContactListParams(name=name)
return contact_lists_api.create(params)


def update_contact_list(contact_list_id: int, name: str) -> ContactList:
params = mt.ContactListParams(name=name)
return contact_lists_api.update(contact_list_id, params)


def list_contact_lists() -> list[ContactList]:
return contact_lists_api.get_list()


def get_contact_list(contact_list_id: int) -> ContactList:
return contact_lists_api.get_by_id(contact_list_id)


def delete_contact_list(contact_list_id: int) -> DeletedObject:
return contact_lists_api.delete(contact_list_id)


if __name__ == "__main__":
print(list_contact_lists())
1 change: 1 addition & 0 deletions mailtrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .exceptions import ClientConfigurationError
from .exceptions import MailtrapError
from .models.contacts import ContactField
from .models.contacts import ContactListParams
from .models.contacts import CreateContactFieldParams
from .models.contacts import UpdateContactFieldParams
from .models.mail import Address
Expand Down
5 changes: 5 additions & 0 deletions mailtrap/api/contacts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from mailtrap.api.resources.contact_fields import ContactFieldsApi
from mailtrap.api.resources.contact_lists import ContactListsApi
from mailtrap.http import HttpClient


Expand All @@ -10,3 +11,7 @@ def __init__(self, client: HttpClient, account_id: str) -> None:
@property
def contact_fields(self) -> ContactFieldsApi:
return ContactFieldsApi(account_id=self._account_id, client=self._client)

@property
def contact_lists(self) -> ContactListsApi:
return ContactListsApi(account_id=self._account_id, client=self._client)
2 changes: 1 addition & 1 deletion mailtrap/api/resources/contact_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def delete(self, field_id: int) -> DeletedObject:

def _api_path(self, field_id: Optional[int] = None) -> str:
path = f"/api/accounts/{self._account_id}/contacts/fields"
if field_id:
if field_id is not None:
return f"{path}/{field_id}"
return path
44 changes: 44 additions & 0 deletions mailtrap/api/resources/contact_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Optional

from mailtrap.http import HttpClient
from mailtrap.models.common import DeletedObject
from mailtrap.models.contacts import ContactList
from mailtrap.models.contacts import ContactListParams


class ContactListsApi:
def __init__(self, client: HttpClient, account_id: str) -> None:
self._account_id = account_id
self._client = client

def get_list(self) -> list[ContactList]:
response = self._client.get(self._api_path())
return [ContactList(**field) for field in response]

def get_by_id(self, list_id: int) -> ContactList:
response = self._client.get(self._api_path(list_id))
return ContactList(**response)

def create(self, list_params: ContactListParams) -> ContactList:
response = self._client.post(
self._api_path(),
json=list_params.api_data,
)
return ContactList(**response)

def update(self, list_id: int, list_params: ContactListParams) -> ContactList:
response = self._client.patch(
self._api_path(list_id),
json=list_params.api_data,
)
return ContactList(**response)

def delete(self, list_id: int) -> DeletedObject:
self._client.delete(self._api_path(list_id))
return DeletedObject(list_id)

def _api_path(self, list_id: Optional[int] = None) -> str:
path = f"/api/accounts/{self._account_id}/contacts/lists"
if list_id is not None:
return f"{path}/{list_id}"
return path
11 changes: 11 additions & 0 deletions mailtrap/models/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ class ContactField:
name: str
data_type: str
merge_tag: str


@dataclass
class ContactListParams(RequestParams):
name: str


@dataclass
class ContactList:
id: int
name: str
File renamed without changes.
Loading
Loading