From d272c23b1ea3badd3163e6d8ac3b88f7c4a77cfb Mon Sep 17 00:00:00 2001 From: Maciej Walusiak Date: Thu, 9 Jul 2026 12:22:03 +0200 Subject: [PATCH 1/2] MT-22678: Add optional search name filter to contact lists get_list --- README.md | 2 +- examples/contacts/contact_lists.py | 8 ++++++++ mailtrap/api/resources/contact_lists.py | 13 ++++++++++--- mailtrap/models/contacts.py | 6 ++++++ tests/unit/api/contacts/test_contact_lists.py | 18 ++++++++++++++++++ 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index db927e6..abdafde 100644 --- a/README.md +++ b/README.md @@ -232,7 +232,7 @@ The same situation applies to both `client.batch_send()` and `client.sending_api ### Contacts API: - Contacts management – [`contacts/contacts.py`](examples/contacts/contacts.py) -- Contact Lists management – [`contacts/contact_lists.py`](examples/contacts/contact_lists.py) +- Contact Lists management (list with optional `search` name filter) – [`contacts/contact_lists.py`](examples/contacts/contact_lists.py) - Contact Fields management – [`contacts/contact_fields.py`](examples/contacts/contact_fields.py) - Contact Events – [`contacts/contact_events.py`](examples/contacts/contact_events.py) - Contact Exports – [`contacts/contact_exports.py`](examples/contacts/contact_exports.py) diff --git a/examples/contacts/contact_lists.py b/examples/contacts/contact_lists.py index 4623d23..99bd29b 100644 --- a/examples/contacts/contact_lists.py +++ b/examples/contacts/contact_lists.py @@ -23,6 +23,11 @@ def list_contact_lists() -> list[ContactList]: return contact_lists_api.get_list() +def search_contact_lists(search: str) -> list[ContactList]: + # Filter lists by name (case-insensitive prefix match). + return contact_lists_api.get_list(search=search) + + def get_contact_list(contact_list_id: int) -> ContactList: return contact_lists_api.get_by_id(contact_list_id) @@ -38,6 +43,9 @@ def delete_contact_list(contact_list_id: int) -> DeletedObject: lists = list_contact_lists() print(lists) + matching_lists = search_contact_lists(search="news") + print(matching_lists) + contact_list = get_contact_list(contact_list_id=created.id) print(contact_list) diff --git a/mailtrap/api/resources/contact_lists.py b/mailtrap/api/resources/contact_lists.py index cfd873b..07b8bf0 100644 --- a/mailtrap/api/resources/contact_lists.py +++ b/mailtrap/api/resources/contact_lists.py @@ -4,6 +4,7 @@ from mailtrap.models.common import DeletedObject from mailtrap.models.contacts import ContactList from mailtrap.models.contacts import ContactListParams +from mailtrap.models.contacts import ContactListsFilterParams class ContactListsApi: @@ -11,9 +12,15 @@ def __init__(self, client: HttpClient, account_id: str) -> None: self._account_id = account_id self._client = client - def get_list(self) -> list[ContactList]: - """Get all contact lists existing in your account.""" - response = self._client.get(self._api_path()) + def get_list(self, search: Optional[str] = None) -> list[ContactList]: + """ + Get all contact lists existing in your account. + + :param search: Optionally filter lists by name + (case-insensitive prefix match). + """ + params = ContactListsFilterParams(search=search) + response = self._client.get(self._api_path(), params=params.api_query_params) return [ContactList(**field) for field in response] def get_by_id(self, list_id: int) -> ContactList: diff --git a/mailtrap/models/contacts.py b/mailtrap/models/contacts.py index 5a4fcea..8bbf219 100644 --- a/mailtrap/models/contacts.py +++ b/mailtrap/models/contacts.py @@ -38,6 +38,12 @@ class ContactListParams(RequestParams): name: str +@dataclass +class ContactListsFilterParams(RequestParams): + # Filter contact lists by name (case-insensitive prefix match). + search: Optional[str] = None + + @dataclass class ContactList: id: int diff --git a/tests/unit/api/contacts/test_contact_lists.py b/tests/unit/api/contacts/test_contact_lists.py index bed2057..7baa0ad 100644 --- a/tests/unit/api/contacts/test_contact_lists.py +++ b/tests/unit/api/contacts/test_contact_lists.py @@ -106,6 +106,24 @@ def test_get_contact_lists_should_return_contact_list_list( ) assert contact_lists[0].id == LIST_ID + @responses.activate + def test_get_contact_lists_with_search_should_filter_by_name( + self, contact_lists_api: ContactListsApi, sample_contact_list_dict: dict + ) -> None: + search = "news" + responses.get( + BASE_CONTACT_LISTS_URL, + json=[sample_contact_list_dict], + status=200, + match=[responses.matchers.query_param_matcher({"search": search})], + ) + + contact_lists = contact_lists_api.get_list(search=search) + + assert isinstance(contact_lists, list) + assert contact_lists[0].id == LIST_ID + assert responses.calls[0].request.params["search"] == search + @pytest.mark.parametrize( "status_code,response_json,expected_error_message", [ From 59f5e94f845599a654f2ee6c8f7757842e234246 Mon Sep 17 00:00:00 2001 From: Maciej Walusiak Date: Fri, 10 Jul 2026 10:24:02 +0200 Subject: [PATCH 2/2] MT-22678: Drop search filter call-out from README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index abdafde..db927e6 100644 --- a/README.md +++ b/README.md @@ -232,7 +232,7 @@ The same situation applies to both `client.batch_send()` and `client.sending_api ### Contacts API: - Contacts management – [`contacts/contacts.py`](examples/contacts/contacts.py) -- Contact Lists management (list with optional `search` name filter) – [`contacts/contact_lists.py`](examples/contacts/contact_lists.py) +- Contact Lists management – [`contacts/contact_lists.py`](examples/contacts/contact_lists.py) - Contact Fields management – [`contacts/contact_fields.py`](examples/contacts/contact_fields.py) - Contact Events – [`contacts/contact_events.py`](examples/contacts/contact_events.py) - Contact Exports – [`contacts/contact_exports.py`](examples/contacts/contact_exports.py)