From e7e6b45867c3915295729159719fd61a1e6ed567 Mon Sep 17 00:00:00 2001 From: Albert Sola Date: Mon, 6 Oct 2025 16:06:46 +0100 Subject: [PATCH] MPT-14078 Add notifications accounts categories contact --- .../resources/notifications/accounts.py | 38 ++++++++++++ .../resources/notifications/notifications.py | 61 +++++++++++++++++-- .../resources/notifications/test_accounts.py | 28 +++++++++ .../notifications/test_notifications.py | 25 ++++++++ 4 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 mpt_api_client/resources/notifications/accounts.py create mode 100644 tests/resources/notifications/test_accounts.py diff --git a/mpt_api_client/resources/notifications/accounts.py b/mpt_api_client/resources/notifications/accounts.py new file mode 100644 index 00000000..d26a8746 --- /dev/null +++ b/mpt_api_client/resources/notifications/accounts.py @@ -0,0 +1,38 @@ +from typing import override + +from mpt_api_client.http import AsyncService, Service +from mpt_api_client.models import Model + + +class MethodNotAllowedError(Exception): + """Method not allowed error.""" + + +class Contact(Model): + """Account resource.""" + + +class AccountsServiceConfig: + """Accounts service config.""" + + _endpoint = "/public/v1/commerce/accounts/{account_id}/categories/{category_id}/contacts" + _model_class = Contact + _collection_key = "data" + + +class AccountsService(Service[Contact], AccountsServiceConfig): + """Accounts service.""" + + @override + def get(self, resource_id: str, select: list[str] | str | None = None) -> Contact: + # TODO: delete. This method does not exist in the api + raise MethodNotAllowedError("Operation not allowed") + + +class AsyncAccountsService(AsyncService[Contact], AccountsServiceConfig): + """Async Accounts service.""" + + @override + async def get(self, resource_id: str, select: list[str] | str | None = None) -> Contact: + # TODO: delete. This method does not exist in the api + raise MethodNotAllowedError("Operation not allowed") diff --git a/mpt_api_client/resources/notifications/notifications.py b/mpt_api_client/resources/notifications/notifications.py index cc4a1cde..1ffae9f5 100644 --- a/mpt_api_client/resources/notifications/notifications.py +++ b/mpt_api_client/resources/notifications/notifications.py @@ -1,4 +1,5 @@ from mpt_api_client.http import AsyncHTTPClient, HTTPClient +from mpt_api_client.resources.notifications.accounts import AccountsService, AsyncAccountsService from mpt_api_client.resources.notifications.batches import AsyncBatchesService, BatchesService from mpt_api_client.resources.notifications.categories import ( AsyncCategoriesService, @@ -6,6 +7,10 @@ ) from mpt_api_client.resources.notifications.contacts import AsyncContactsService, ContactsService from mpt_api_client.resources.notifications.messages import AsyncMessagesService, MessagesService +from mpt_api_client.resources.notifications.subscribers import ( + AsyncSubscribersService, + SubscribersService, +) class Notifications: @@ -14,6 +19,28 @@ class Notifications: def __init__(self, http_client: HTTPClient): self.http_client = http_client + def accounts(self, account_id: str, category_id: str) -> AccountsService: + """Accounts service. + + Returns contacts, which are configured to receive notifications. + + Parms: + account_id: Account ID + category_id: Category ID + + Returns: + AccountsService + """ + return AccountsService( + http_client=self.http_client, + endpoint_params={"account_id": account_id, "category_id": category_id}, + ) + + @property + def batches(self) -> BatchesService: + """Batches service.""" + return BatchesService(http_client=self.http_client) + @property def categories(self) -> CategoriesService: """Categories service.""" @@ -30,9 +57,9 @@ def messages(self) -> MessagesService: return MessagesService(http_client=self.http_client) @property - def batches(self) -> BatchesService: - """Batches service.""" - return BatchesService(http_client=self.http_client) + def subscribers(self) -> SubscribersService: + """Subscriptions service.""" + return SubscribersService(http_client=self.http_client) class AsyncNotifications: @@ -41,6 +68,28 @@ class AsyncNotifications: def __init__(self, http_client: AsyncHTTPClient): self.http_client = http_client + def accounts(self, account_id: str, category_id: str) -> AsyncAccountsService: + """Async Accounts service. + + Returns contacts, which are configured to receive notifications. + + Parms: + account_id: Account ID + category_id: Category ID + + Returns: + AccountsService + """ + return AsyncAccountsService( + http_client=self.http_client, + endpoint_params={"account_id": account_id, "category_id": category_id}, + ) + + @property + def batches(self) -> AsyncBatchesService: + """Batches service.""" + return AsyncBatchesService(http_client=self.http_client) + @property def categories(self) -> AsyncCategoriesService: """Categories service.""" @@ -57,6 +106,6 @@ def messages(self) -> AsyncMessagesService: return AsyncMessagesService(http_client=self.http_client) @property - def batches(self) -> AsyncBatchesService: - """Async Batches service.""" - return AsyncBatchesService(http_client=self.http_client) + def subscribers(self) -> AsyncSubscribersService: + """Subscriptions service.""" + return AsyncSubscribersService(http_client=self.http_client) diff --git a/tests/resources/notifications/test_accounts.py b/tests/resources/notifications/test_accounts.py new file mode 100644 index 00000000..dff8ab58 --- /dev/null +++ b/tests/resources/notifications/test_accounts.py @@ -0,0 +1,28 @@ +import pytest + +from mpt_api_client.resources.notifications.accounts import ( + AccountsService, + AsyncAccountsService, + MethodNotAllowedError, +) + + +@pytest.fixture +def accounts_service(http_client): + return AccountsService(http_client=http_client) + + +@pytest.fixture +def async_accounts_service(async_http_client): + return AsyncAccountsService(http_client=async_http_client) + + +def test_accounts_service_get_raises(accounts_service): + with pytest.raises(MethodNotAllowedError): + accounts_service.get("CONTACT-123") + + +@pytest.mark.asyncio +async def test_async_accounts_service_get_raises(async_accounts_service): + with pytest.raises(MethodNotAllowedError): + await async_accounts_service.get("CONTACT-123") diff --git a/tests/resources/notifications/test_notifications.py b/tests/resources/notifications/test_notifications.py index 7f22eea5..16344157 100644 --- a/tests/resources/notifications/test_notifications.py +++ b/tests/resources/notifications/test_notifications.py @@ -1,6 +1,7 @@ import pytest from mpt_api_client.resources import AsyncNotifications, Notifications +from mpt_api_client.resources.notifications.accounts import AccountsService, AsyncAccountsService from mpt_api_client.resources.notifications.batches import AsyncBatchesService, BatchesService from mpt_api_client.resources.notifications.categories import ( AsyncCategoriesService, @@ -8,6 +9,10 @@ ) from mpt_api_client.resources.notifications.contacts import AsyncContactsService, ContactsService from mpt_api_client.resources.notifications.messages import AsyncMessagesService, MessagesService +from mpt_api_client.resources.notifications.subscribers import ( + AsyncSubscribersService, + SubscribersService, +) def test_notifications_init(http_client): @@ -31,6 +36,7 @@ def test_async_notifications_init(async_http_client): ("contacts", ContactsService), ("messages", MessagesService), ("batches", BatchesService), + ("subscribers", SubscribersService), ], ) def test_notifications_properties(http_client, attr_name, expected): @@ -48,6 +54,7 @@ def test_notifications_properties(http_client, attr_name, expected): ("contacts", AsyncContactsService), ("messages", AsyncMessagesService), ("batches", AsyncBatchesService), + ("subscribers", AsyncSubscribersService), ], ) def test_async_notifications_properties(http_client, attr_name, expected): @@ -56,3 +63,21 @@ def test_async_notifications_properties(http_client, attr_name, expected): service = getattr(notifications, attr_name) assert isinstance(service, expected) + + +def test_notifications_accounts(http_client): + notifications = Notifications(http_client=http_client) + account_id = "ACC-123" + category_id = "CAT-456" + service = notifications.accounts(account_id, category_id) + assert isinstance(service, AccountsService) + assert service.endpoint_params == {"account_id": account_id, "category_id": category_id} + + +def test_async_notifications_accounts(async_http_client): + notifications = AsyncNotifications(http_client=async_http_client) + account_id = "ACC-123" + category_id = "CAT-456" + service = notifications.accounts(account_id, category_id) + assert isinstance(service, AsyncAccountsService) + assert service.endpoint_params == {"account_id": account_id, "category_id": category_id}