Skip to content

Commit 898faf5

Browse files
authored
MPT-14078 Add notifications accounts categories contact (#89)
2 parents f8b709b + e7e6b45 commit 898faf5

File tree

4 files changed

+146
-6
lines changed

4 files changed

+146
-6
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import override
2+
3+
from mpt_api_client.http import AsyncService, Service
4+
from mpt_api_client.models import Model
5+
6+
7+
class MethodNotAllowedError(Exception):
8+
"""Method not allowed error."""
9+
10+
11+
class Contact(Model):
12+
"""Account resource."""
13+
14+
15+
class AccountsServiceConfig:
16+
"""Accounts service config."""
17+
18+
_endpoint = "/public/v1/commerce/accounts/{account_id}/categories/{category_id}/contacts"
19+
_model_class = Contact
20+
_collection_key = "data"
21+
22+
23+
class AccountsService(Service[Contact], AccountsServiceConfig):
24+
"""Accounts service."""
25+
26+
@override
27+
def get(self, resource_id: str, select: list[str] | str | None = None) -> Contact:
28+
# TODO: delete. This method does not exist in the api
29+
raise MethodNotAllowedError("Operation not allowed")
30+
31+
32+
class AsyncAccountsService(AsyncService[Contact], AccountsServiceConfig):
33+
"""Async Accounts service."""
34+
35+
@override
36+
async def get(self, resource_id: str, select: list[str] | str | None = None) -> Contact:
37+
# TODO: delete. This method does not exist in the api
38+
raise MethodNotAllowedError("Operation not allowed")

mpt_api_client/resources/notifications/notifications.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from mpt_api_client.http import AsyncHTTPClient, HTTPClient
2+
from mpt_api_client.resources.notifications.accounts import AccountsService, AsyncAccountsService
23
from mpt_api_client.resources.notifications.batches import AsyncBatchesService, BatchesService
34
from mpt_api_client.resources.notifications.categories import (
45
AsyncCategoriesService,
56
CategoriesService,
67
)
78
from mpt_api_client.resources.notifications.contacts import AsyncContactsService, ContactsService
89
from mpt_api_client.resources.notifications.messages import AsyncMessagesService, MessagesService
10+
from mpt_api_client.resources.notifications.subscribers import (
11+
AsyncSubscribersService,
12+
SubscribersService,
13+
)
914

1015

1116
class Notifications:
@@ -14,6 +19,28 @@ class Notifications:
1419
def __init__(self, http_client: HTTPClient):
1520
self.http_client = http_client
1621

22+
def accounts(self, account_id: str, category_id: str) -> AccountsService:
23+
"""Accounts service.
24+
25+
Returns contacts, which are configured to receive notifications.
26+
27+
Parms:
28+
account_id: Account ID
29+
category_id: Category ID
30+
31+
Returns:
32+
AccountsService
33+
"""
34+
return AccountsService(
35+
http_client=self.http_client,
36+
endpoint_params={"account_id": account_id, "category_id": category_id},
37+
)
38+
39+
@property
40+
def batches(self) -> BatchesService:
41+
"""Batches service."""
42+
return BatchesService(http_client=self.http_client)
43+
1744
@property
1845
def categories(self) -> CategoriesService:
1946
"""Categories service."""
@@ -30,9 +57,9 @@ def messages(self) -> MessagesService:
3057
return MessagesService(http_client=self.http_client)
3158

3259
@property
33-
def batches(self) -> BatchesService:
34-
"""Batches service."""
35-
return BatchesService(http_client=self.http_client)
60+
def subscribers(self) -> SubscribersService:
61+
"""Subscriptions service."""
62+
return SubscribersService(http_client=self.http_client)
3663

3764

3865
class AsyncNotifications:
@@ -41,6 +68,28 @@ class AsyncNotifications:
4168
def __init__(self, http_client: AsyncHTTPClient):
4269
self.http_client = http_client
4370

71+
def accounts(self, account_id: str, category_id: str) -> AsyncAccountsService:
72+
"""Async Accounts service.
73+
74+
Returns contacts, which are configured to receive notifications.
75+
76+
Parms:
77+
account_id: Account ID
78+
category_id: Category ID
79+
80+
Returns:
81+
AccountsService
82+
"""
83+
return AsyncAccountsService(
84+
http_client=self.http_client,
85+
endpoint_params={"account_id": account_id, "category_id": category_id},
86+
)
87+
88+
@property
89+
def batches(self) -> AsyncBatchesService:
90+
"""Batches service."""
91+
return AsyncBatchesService(http_client=self.http_client)
92+
4493
@property
4594
def categories(self) -> AsyncCategoriesService:
4695
"""Categories service."""
@@ -57,6 +106,6 @@ def messages(self) -> AsyncMessagesService:
57106
return AsyncMessagesService(http_client=self.http_client)
58107

59108
@property
60-
def batches(self) -> AsyncBatchesService:
61-
"""Async Batches service."""
62-
return AsyncBatchesService(http_client=self.http_client)
109+
def subscribers(self) -> AsyncSubscribersService:
110+
"""Subscriptions service."""
111+
return AsyncSubscribersService(http_client=self.http_client)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from mpt_api_client.resources.notifications.accounts import (
4+
AccountsService,
5+
AsyncAccountsService,
6+
MethodNotAllowedError,
7+
)
8+
9+
10+
@pytest.fixture
11+
def accounts_service(http_client):
12+
return AccountsService(http_client=http_client)
13+
14+
15+
@pytest.fixture
16+
def async_accounts_service(async_http_client):
17+
return AsyncAccountsService(http_client=async_http_client)
18+
19+
20+
def test_accounts_service_get_raises(accounts_service):
21+
with pytest.raises(MethodNotAllowedError):
22+
accounts_service.get("CONTACT-123")
23+
24+
25+
@pytest.mark.asyncio
26+
async def test_async_accounts_service_get_raises(async_accounts_service):
27+
with pytest.raises(MethodNotAllowedError):
28+
await async_accounts_service.get("CONTACT-123")

tests/resources/notifications/test_notifications.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import pytest
22

33
from mpt_api_client.resources import AsyncNotifications, Notifications
4+
from mpt_api_client.resources.notifications.accounts import AccountsService, AsyncAccountsService
45
from mpt_api_client.resources.notifications.batches import AsyncBatchesService, BatchesService
56
from mpt_api_client.resources.notifications.categories import (
67
AsyncCategoriesService,
78
CategoriesService,
89
)
910
from mpt_api_client.resources.notifications.contacts import AsyncContactsService, ContactsService
1011
from mpt_api_client.resources.notifications.messages import AsyncMessagesService, MessagesService
12+
from mpt_api_client.resources.notifications.subscribers import (
13+
AsyncSubscribersService,
14+
SubscribersService,
15+
)
1116

1217

1318
def test_notifications_init(http_client):
@@ -31,6 +36,7 @@ def test_async_notifications_init(async_http_client):
3136
("contacts", ContactsService),
3237
("messages", MessagesService),
3338
("batches", BatchesService),
39+
("subscribers", SubscribersService),
3440
],
3541
)
3642
def test_notifications_properties(http_client, attr_name, expected):
@@ -48,6 +54,7 @@ def test_notifications_properties(http_client, attr_name, expected):
4854
("contacts", AsyncContactsService),
4955
("messages", AsyncMessagesService),
5056
("batches", AsyncBatchesService),
57+
("subscribers", AsyncSubscribersService),
5158
],
5259
)
5360
def test_async_notifications_properties(http_client, attr_name, expected):
@@ -56,3 +63,21 @@ def test_async_notifications_properties(http_client, attr_name, expected):
5663
service = getattr(notifications, attr_name)
5764

5865
assert isinstance(service, expected)
66+
67+
68+
def test_notifications_accounts(http_client):
69+
notifications = Notifications(http_client=http_client)
70+
account_id = "ACC-123"
71+
category_id = "CAT-456"
72+
service = notifications.accounts(account_id, category_id)
73+
assert isinstance(service, AccountsService)
74+
assert service.endpoint_params == {"account_id": account_id, "category_id": category_id}
75+
76+
77+
def test_async_notifications_accounts(async_http_client):
78+
notifications = AsyncNotifications(http_client=async_http_client)
79+
account_id = "ACC-123"
80+
category_id = "CAT-456"
81+
service = notifications.accounts(account_id, category_id)
82+
assert isinstance(service, AsyncAccountsService)
83+
assert service.endpoint_params == {"account_id": account_id, "category_id": category_id}

0 commit comments

Comments
 (0)