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
38 changes: 38 additions & 0 deletions mpt_api_client/resources/notifications/accounts.py
Original file line number Diff line number Diff line change
@@ -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")
61 changes: 55 additions & 6 deletions mpt_api_client/resources/notifications/notifications.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
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,
CategoriesService,
)
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:
Expand All @@ -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."""
Expand All @@ -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:
Expand All @@ -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."""
Expand All @@ -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)
28 changes: 28 additions & 0 deletions tests/resources/notifications/test_accounts.py
Original file line number Diff line number Diff line change
@@ -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")
25 changes: 25 additions & 0 deletions tests/resources/notifications/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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,
CategoriesService,
)
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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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}