Skip to content

Commit d1e1d24

Browse files
author
Robert Segal
committed
Added accounts buyers endpoints
1 parent 7938167 commit d1e1d24

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed

mpt_api_client/resources/accounts/accounts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mpt_api_client.http import AsyncHTTPClient, HTTPClient
22
from mpt_api_client.resources.accounts.account import AccountsService, AsyncAccountsService
3+
from mpt_api_client.resources.accounts.buyers import AsyncBuyersService, BuyersService
34
from mpt_api_client.resources.accounts.licensees import AsyncLicenseesService, LicenseesService
45
from mpt_api_client.resources.accounts.sellers import AsyncSellersService, SellersService
56
from mpt_api_client.resources.accounts.users import AsyncUsersService, UsersService
@@ -31,6 +32,11 @@ def licensees(self) -> LicenseesService:
3132
"""Licensees service."""
3233
return LicenseesService(http_client=self.http_client)
3334

35+
@property
36+
def buyers(self) -> BuyersService:
37+
"""Buyers service."""
38+
return BuyersService(http_client=self.http_client)
39+
3440

3541
class AsyncAccounts:
3642
"""Async Accounts MPT API Module."""
@@ -57,3 +63,8 @@ def sellers(self) -> AsyncSellersService:
5763
def licensees(self) -> AsyncLicenseesService:
5864
"""Licensees service."""
5965
return AsyncLicenseesService(http_client=self.http_client)
66+
67+
@property
68+
def buyers(self) -> AsyncBuyersService:
69+
"""Buyers service."""
70+
return AsyncBuyersService(http_client=self.http_client)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from mpt_api_client.http import AsyncService, Service
2+
from mpt_api_client.http.mixins import (
3+
AsyncCreateMixin,
4+
AsyncDeleteMixin,
5+
AsyncUpdateMixin,
6+
CreateMixin,
7+
DeleteMixin,
8+
UpdateMixin,
9+
)
10+
from mpt_api_client.models import Model
11+
from mpt_api_client.models.model import ResourceData
12+
from mpt_api_client.resources.accounts.mixins import (
13+
ActivatableMixin,
14+
AsyncActivatableMixin,
15+
AsyncEnablableMixin,
16+
AsyncValidateMixin,
17+
EnablableMixin,
18+
ValidateMixin,
19+
)
20+
21+
22+
class Buyer(Model):
23+
"""Buyer Model."""
24+
25+
26+
class BuyersServiceConfig:
27+
"""Buyers Service Configuration."""
28+
29+
_endpoint = "/public/v1/accounts/buyers"
30+
_model_class = Buyer
31+
_collection_key = "data"
32+
33+
34+
class BuyersService(
35+
CreateMixin[Buyer],
36+
DeleteMixin,
37+
UpdateMixin[Buyer],
38+
ActivatableMixin[Buyer],
39+
EnablableMixin[Buyer],
40+
ValidateMixin[Buyer],
41+
Service[Buyer],
42+
BuyersServiceConfig,
43+
):
44+
"""Buyers Service."""
45+
46+
def synchronize(self, resource_id: str, resource_data: ResourceData | None = None) -> Buyer:
47+
"""Synchronize a buyer.
48+
49+
Args:
50+
resource_id: Resource ID
51+
resource_data: Resource data will be updated
52+
"""
53+
return self._resource_action(resource_id, "POST", "synchronize", json=resource_data)
54+
55+
def transfer(self, resource_id: str, resource_data: ResourceData | None = None) -> Buyer:
56+
"""Transfer a buyer.
57+
58+
Args:
59+
resource_id: Resource ID
60+
resource_data: Resource data will be updated
61+
"""
62+
return self._resource_action(resource_id, "POST", "transfer", json=resource_data)
63+
64+
65+
class AsyncBuyersService(
66+
AsyncCreateMixin[Buyer],
67+
AsyncDeleteMixin,
68+
AsyncUpdateMixin[Buyer],
69+
AsyncActivatableMixin[Buyer],
70+
AsyncEnablableMixin[Buyer],
71+
AsyncValidateMixin[Buyer],
72+
AsyncService[Buyer],
73+
BuyersServiceConfig,
74+
):
75+
"""Async Buyers Service."""
76+
77+
async def synchronize(
78+
self, resource_id: str, resource_data: ResourceData | None = None
79+
) -> Buyer:
80+
"""Synchronize a buyer.
81+
82+
Args:
83+
resource_id: Resource ID
84+
resource_data: Resource data will be updated
85+
"""
86+
return await self._resource_action(resource_id, "POST", "synchronize", json=resource_data)
87+
88+
async def transfer(self, resource_id: str, resource_data: ResourceData | None = None) -> Buyer:
89+
"""Transfer a buyer.
90+
91+
Args:
92+
resource_id: Resource ID
93+
resource_data: Resource data will be updated
94+
"""
95+
return await self._resource_action(resource_id, "POST", "transfer", json=resource_data)

tests/resources/accounts/test_accounts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from mpt_api_client.resources.accounts.account import AccountsService, AsyncAccountsService
44
from mpt_api_client.resources.accounts.accounts import Accounts, AsyncAccounts
5+
from mpt_api_client.resources.accounts.buyers import AsyncBuyersService, BuyersService
56
from mpt_api_client.resources.accounts.licensees import AsyncLicenseesService, LicenseesService
67
from mpt_api_client.resources.accounts.sellers import AsyncSellersService, SellersService
78
from mpt_api_client.resources.accounts.users import AsyncUsersService, UsersService
@@ -24,6 +25,7 @@ def async_accounts(async_http_client):
2425
("users", UsersService),
2526
("sellers", SellersService),
2627
("licensees", LicenseesService),
28+
("buyers", BuyersService),
2729
],
2830
)
2931
def test_accounts_properties(accounts, property_name, expected_service_class):
@@ -41,6 +43,7 @@ def test_accounts_properties(accounts, property_name, expected_service_class):
4143
("users", AsyncUsersService),
4244
("sellers", AsyncSellersService),
4345
("licensees", AsyncLicenseesService),
46+
("buyers", AsyncBuyersService),
4447
],
4548
)
4649
def test_async_accounts_properties(async_accounts, property_name, expected_service_class):
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import httpx
2+
import pytest
3+
import respx
4+
5+
from mpt_api_client.resources.accounts.buyers import AsyncBuyersService, Buyer, BuyersService
6+
7+
8+
@pytest.fixture
9+
def buyers_service(http_client):
10+
return BuyersService(http_client=http_client)
11+
12+
13+
@pytest.fixture
14+
def async_buyers_service(async_http_client):
15+
return AsyncBuyersService(http_client=async_http_client)
16+
17+
18+
@pytest.mark.parametrize(
19+
"method",
20+
[
21+
"get",
22+
"create",
23+
"update",
24+
"delete",
25+
"activate",
26+
"deactivate",
27+
"enable",
28+
"disable",
29+
"validate",
30+
"synchronize",
31+
"transfer",
32+
],
33+
)
34+
def test_mixins_present(buyers_service, method):
35+
assert hasattr(buyers_service, method)
36+
37+
38+
@pytest.mark.parametrize(
39+
"method",
40+
[
41+
"get",
42+
"create",
43+
"update",
44+
"delete",
45+
"activate",
46+
"deactivate",
47+
"enable",
48+
"disable",
49+
"validate",
50+
"synchronize",
51+
"transfer",
52+
],
53+
)
54+
def test_async_mixins_present(async_buyers_service, method):
55+
assert hasattr(async_buyers_service, method)
56+
57+
58+
@pytest.mark.parametrize(
59+
("action", "input_status"),
60+
[
61+
("synchronize", {"id": "OBJ-0000-0001", "status": "update"}),
62+
("transfer", {"id": "OBJ-0000-0001", "status": "update"}),
63+
],
64+
)
65+
def test_buyers_resource_action(buyers_service, action, input_status):
66+
request_expected_content = b'{"id":"OBJ-0000-0001","status":"update"}'
67+
response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"}
68+
with respx.mock:
69+
mock_route = respx.post(
70+
f"https://api.example.com/public/v1/accounts/buyers/OBJ-0000-0001/{action}"
71+
).mock(
72+
return_value=httpx.Response(
73+
status_code=httpx.codes.OK,
74+
json=response_expected_data,
75+
)
76+
)
77+
buyers_obj = getattr(buyers_service, action)("OBJ-0000-0001", input_status)
78+
79+
request = mock_route.calls[0].request
80+
81+
assert request.content == request_expected_content
82+
assert buyers_obj.to_dict() == response_expected_data
83+
assert isinstance(buyers_obj, Buyer)
84+
85+
86+
@pytest.mark.parametrize(
87+
("action", "input_status"),
88+
[("synchronize", None), ("transfer", None)],
89+
)
90+
def test_buyers_resouce_action_no_data(buyers_service, action, input_status):
91+
request_expected_content = b""
92+
response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"}
93+
with respx.mock:
94+
mock_route = respx.post(
95+
f"https://api.example.com/public/v1/accounts/buyers/OBJ-0000-0001/{action}"
96+
).mock(
97+
return_value=httpx.Response(
98+
status_code=httpx.codes.OK,
99+
json=response_expected_data,
100+
)
101+
)
102+
buyers_obj = getattr(buyers_service, action)("OBJ-0000-0001", None)
103+
104+
request = mock_route.calls[0].request
105+
106+
assert request.content == request_expected_content
107+
assert buyers_obj.to_dict() == response_expected_data
108+
assert isinstance(buyers_obj, Buyer)
109+
110+
111+
@pytest.mark.parametrize(
112+
("action", "input_status"),
113+
[
114+
("synchronize", {"id": "OBJ-0000-0001", "status": "update"}),
115+
("transfer", {"id": "OBJ-0000-0001", "status": "update"}),
116+
],
117+
)
118+
async def test_async_buyers_resource_action(async_buyers_service, action, input_status):
119+
request_expected_content = b'{"id":"OBJ-0000-0001","status":"update"}'
120+
response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"}
121+
with respx.mock:
122+
mock_route = respx.post(
123+
f"https://api.example.com/public/v1/accounts/buyers/OBJ-0000-0001/{action}"
124+
).mock(
125+
return_value=httpx.Response(
126+
status_code=httpx.codes.OK,
127+
json=response_expected_data,
128+
)
129+
)
130+
buyers_obj = await getattr(async_buyers_service, action)("OBJ-0000-0001", input_status)
131+
132+
request = mock_route.calls[0].request
133+
134+
assert request.content == request_expected_content
135+
assert buyers_obj.to_dict() == response_expected_data
136+
assert isinstance(buyers_obj, Buyer)
137+
138+
139+
@pytest.mark.parametrize(
140+
("action", "input_status"),
141+
[("synchronize", None), ("transfer", None)],
142+
)
143+
async def test_async_buyers_resource_action_no_data(async_buyers_service, action, input_status):
144+
request_expected_content = b""
145+
response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"}
146+
with respx.mock:
147+
mock_route = respx.post(
148+
f"https://api.example.com/public/v1/accounts/buyers/OBJ-0000-0001/{action}"
149+
).mock(
150+
return_value=httpx.Response(
151+
status_code=httpx.codes.OK,
152+
json=response_expected_data,
153+
)
154+
)
155+
buyers_obj = await getattr(async_buyers_service, action)("OBJ-0000-0001", None)
156+
157+
request = mock_route.calls[0].request
158+
159+
assert request.content == request_expected_content
160+
assert buyers_obj.to_dict() == response_expected_data
161+
assert isinstance(buyers_obj, Buyer)

0 commit comments

Comments
 (0)