diff --git a/mpt_api_client/resources/billing/__init__.py b/mpt_api_client/resources/billing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mpt_api_client/resources/billing/billing.py b/mpt_api_client/resources/billing/billing.py new file mode 100644 index 0000000..0b9d60f --- /dev/null +++ b/mpt_api_client/resources/billing/billing.py @@ -0,0 +1,26 @@ +from mpt_api_client.http import AsyncHTTPClient, HTTPClient +from mpt_api_client.resources.billing.journals import AsyncJournalsService, JournalsService + + +class Billing: + """Billing MPT API Module.""" + + def __init__(self, *, http_client: HTTPClient): + self.http_client = http_client + + @property + def journals(self) -> JournalsService: + """Journals service.""" + return JournalsService(http_client=self.http_client) + + +class AsyncBilling: + """Billing MPT API Module.""" + + def __init__(self, *, http_client: AsyncHTTPClient): + self.http_client = http_client + + @property + def journals(self) -> AsyncJournalsService: + """Journals service.""" + return AsyncJournalsService(http_client=self.http_client) diff --git a/mpt_api_client/resources/billing/journals.py b/mpt_api_client/resources/billing/journals.py new file mode 100644 index 0000000..e8080bf --- /dev/null +++ b/mpt_api_client/resources/billing/journals.py @@ -0,0 +1,44 @@ +from mpt_api_client.http import AsyncService, CreateMixin, Service +from mpt_api_client.http.mixins import ( + AsyncCreateMixin, + AsyncDeleteMixin, + AsyncUpdateMixin, + DeleteMixin, + UpdateMixin, +) +from mpt_api_client.models import Model +from mpt_api_client.resources.billing.mixins import AsyncRegeneratableMixin, RegeneratableMixin + + +class Journal(Model): + """Journal resource.""" + + +class JournalsServiceConfig: + """Journals service configuration.""" + + _endpoint = "/public/v1/billing/journals" + _model_class = Journal + _collection_key = "data" + + +class JournalsService( + CreateMixin[Journal], + DeleteMixin, + UpdateMixin[Journal], + RegeneratableMixin[Journal], + Service[Journal], + JournalsServiceConfig, +): + """Journals service.""" + + +class AsyncJournalsService( + AsyncCreateMixin[Journal], + AsyncDeleteMixin, + AsyncUpdateMixin[Journal], + AsyncRegeneratableMixin[Journal], + AsyncService[Journal], + JournalsServiceConfig, +): + """Async Journals service.""" diff --git a/mpt_api_client/resources/billing/mixins.py b/mpt_api_client/resources/billing/mixins.py new file mode 100644 index 0000000..d564e86 --- /dev/null +++ b/mpt_api_client/resources/billing/mixins.py @@ -0,0 +1,100 @@ +from mpt_api_client.models import ResourceData + + +# TODO: Consider moving Regeneratable mixins to http/mixins if publishable and activatable are moved +class RegeneratableMixin[Model]: + """Regeneratable mixin adds the ability to regenerate resources.""" + + def regenerate(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: + """Regenerate resource. + + Args: + resource_id: Resource ID + resource_data: Resource data will be updated + """ + return self._resource_action( # type: ignore[attr-defined, no-any-return] + resource_id, "POST", "regenerate", json=resource_data + ) + + def submit(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: + """Submit resource. + + Args: + resource_id: Resource ID + resource_data: Resource data will be updated + """ + return self._resource_action( # type: ignore[attr-defined, no-any-return] + resource_id, "POST", "submit", json=resource_data + ) + + def enquiry(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: + """Enquiry resource. + + Args: + resource_id: Resource ID + resource_data: Resource data will be updated + """ + return self._resource_action( # type: ignore[attr-defined, no-any-return] + resource_id, "POST", "enquiry", json=resource_data + ) + + def accept(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: + """Accept resource. + + Args: + resource_id: Resource ID + resource_data: Resource data will be updated + """ + return self._resource_action( # type: ignore[attr-defined, no-any-return] + resource_id, "POST", "accept", json=resource_data + ) + + +class AsyncRegeneratableMixin[Model]: + """Regeneratable mixin adds the ability to regenerate resources.""" + + async def regenerate( + self, resource_id: str, resource_data: ResourceData | None = None + ) -> Model: + """Regenerate resource. + + Args: + resource_id: Resource ID + resource_data: Resource data will be updated + """ + return await self._resource_action( # type: ignore[attr-defined, no-any-return] + resource_id, "POST", "regenerate", json=resource_data + ) + + async def submit(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: + """Submit resource. + + Args: + resource_id: Resource ID + resource_data: Resource data will be updated + """ + return await self._resource_action( # type: ignore[attr-defined, no-any-return] + resource_id, "POST", "submit", json=resource_data + ) + + async def enquiry(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: + """Enquiry resource. + + Args: + resource_id: Resource ID + resource_data: Resource data will be updated + """ + return await self._resource_action( # type: ignore[attr-defined, no-any-return] + resource_id, "POST", "enquiry", json=resource_data + ) + + async def accept(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: + """Accept resource. + + Args: + resource_id: Resource ID + resource_data: Resource data will be updated + """ + return await self._resource_action( # type: ignore[attr-defined, no-any-return] + resource_id, "POST", "accept", json=resource_data + ) diff --git a/setup.cfg b/setup.cfg index 885d6de..9111a4d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,6 +32,7 @@ extend-ignore = per-file-ignores = + mpt_api_client/resources/billing/*.py: WPS215 mpt_api_client/resources/catalog/*.py: WPS110 WPS215 WPS214 mpt_api_client/resources/commerce/*.py: WPS215 mpt_api_client/rql/query_builder.py: WPS110 WPS115 WPS210 WPS214 diff --git a/tests/resources/billing/__init__.py b/tests/resources/billing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/resources/billing/test_billing.py b/tests/resources/billing/test_billing.py new file mode 100644 index 0000000..045f420 --- /dev/null +++ b/tests/resources/billing/test_billing.py @@ -0,0 +1,56 @@ +import pytest + +from mpt_api_client.resources.billing.billing import AsyncBilling, Billing +from mpt_api_client.resources.billing.journals import AsyncJournalsService, JournalsService + + +@pytest.fixture +def billing(http_client): + return Billing(http_client=http_client) + + +@pytest.fixture +def async_billing(async_http_client): + return AsyncBilling(http_client=async_http_client) + + +@pytest.mark.parametrize( + ("property_name", "expected_service_class"), + [ + ("journals", JournalsService), + ], +) +def test_billing_properties(billing, property_name, expected_service_class): + """Test that Billing properties return correct instances.""" + service = getattr(billing, property_name) + + assert isinstance(service, expected_service_class) + assert service.http_client is billing.http_client + + +@pytest.mark.parametrize( + ("property_name", "expected_service_class"), + [ + ("journals", AsyncJournalsService), + ], +) +def test_async_billing_properties(async_billing, property_name, expected_service_class): + """Test that AsyncBilling properties return correct instances.""" + service = getattr(async_billing, property_name) + + assert isinstance(service, expected_service_class) + assert service.http_client is async_billing.http_client + + +def test_billing_initialization(http_client): + billing = Billing(http_client=http_client) + + assert billing.http_client is http_client + assert isinstance(billing, Billing) + + +def test_async_billing_initialization(async_http_client): + async_billing = AsyncBilling(http_client=async_http_client) + + assert async_billing.http_client is async_http_client + assert isinstance(async_billing, AsyncBilling) diff --git a/tests/resources/billing/test_journals.py b/tests/resources/billing/test_journals.py new file mode 100644 index 0000000..cf2b4c7 --- /dev/null +++ b/tests/resources/billing/test_journals.py @@ -0,0 +1,29 @@ +import pytest + +from mpt_api_client.resources.billing.journals import AsyncJournalsService, JournalsService + + +@pytest.fixture +def journals_service(http_client): + return JournalsService(http_client=http_client) + + +@pytest.fixture +def async_journals_service(async_http_client): + return AsyncJournalsService(http_client=async_http_client) + + +@pytest.mark.parametrize( + "method", + ["get", "create", "update", "delete"], +) +def test_mixins_present(journals_service, method): + assert hasattr(journals_service, method) + + +@pytest.mark.parametrize( + "method", + ["get", "create", "update", "delete"], +) +def test_async_mixins_present(async_journals_service, method): + assert hasattr(async_journals_service, method) diff --git a/tests/resources/billing/test_mixins.py b/tests/resources/billing/test_mixins.py new file mode 100644 index 0000000..9dcb35a --- /dev/null +++ b/tests/resources/billing/test_mixins.py @@ -0,0 +1,166 @@ +import httpx +import pytest +import respx + +from mpt_api_client.http.async_service import AsyncService +from mpt_api_client.http.service import Service +from mpt_api_client.resources.billing.mixins import AsyncRegeneratableMixin, RegeneratableMixin +from tests.conftest import DummyModel + + +class DummyRegeneratableService( + RegeneratableMixin[DummyModel], + Service[DummyModel], +): + _endpoint = "/public/v1/dummy/regeneratable/" + _model_class = DummyModel + _collection_key = "data" + + +class DummyAsyncRegeneratableService( + AsyncRegeneratableMixin[DummyModel], + AsyncService[DummyModel], +): + _endpoint = "/public/v1/dummy/regeneratable/" + _model_class = DummyModel + _collection_key = "data" + + +@pytest.fixture +def regeneratable_service(http_client): + return DummyRegeneratableService(http_client=http_client) + + +@pytest.fixture +def async_regeneratable_service(async_http_client): + return DummyAsyncRegeneratableService(http_client=async_http_client) + + +@pytest.mark.parametrize( + ("action", "input_status"), + [ + ("regenerate", {"id": "OBJ-0000-0001", "status": "update"}), + ("submit", {"id": "OBJ-0000-0001", "status": "update"}), + ("enquiry", {"id": "OBJ-0000-0001", "status": "update"}), + ("accept", {"id": "OBJ-0000-0001", "status": "update"}), + ], +) +def test_custom_resource_actions(regeneratable_service, action, input_status): + request_expected_content = b'{"id":"OBJ-0000-0001","status":"update"}' + response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"} + with respx.mock: + mock_route = respx.post( + f"https://api.example.com/public/v1/dummy/regeneratable/OBJ-0000-0001/{action}" + ).mock( + return_value=httpx.Response( + status_code=httpx.codes.OK, + headers={"content-type": "application/json"}, + json=response_expected_data, + ) + ) + journal = getattr(regeneratable_service, action)("OBJ-0000-0001", input_status) + + assert mock_route.call_count == 1 + request = mock_route.calls[0].request + + assert request.content == request_expected_content + assert journal.to_dict() == response_expected_data + assert isinstance(journal, DummyModel) + + +@pytest.mark.parametrize( + ("action", "input_status"), + [ + ("regenerate", None), + ("submit", None), + ("enquiry", None), + ("accept", None), + ], +) +def test_custom_resource_actions_no_data(regeneratable_service, action, input_status): + request_expected_content = b"" + response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"} + with respx.mock: + mock_route = respx.post( + f"https://api.example.com/public/v1/dummy/regeneratable/OBJ-0000-0001/{action}" + ).mock( + return_value=httpx.Response( + status_code=httpx.codes.OK, + headers={"content-type": "application/json"}, + json=response_expected_data, + ) + ) + journal = getattr(regeneratable_service, action)("OBJ-0000-0001", input_status) + + assert mock_route.call_count == 1 + request = mock_route.calls[0].request + + assert request.content == request_expected_content + assert journal.to_dict() == response_expected_data + assert isinstance(journal, DummyModel) + + +@pytest.mark.parametrize( + ("action", "input_status"), + [ + ("regenerate", {"id": "OBJ-0000-0001", "status": "update"}), + ("submit", {"id": "OBJ-0000-0001", "status": "update"}), + ("enquiry", {"id": "OBJ-0000-0001", "status": "update"}), + ("accept", {"id": "OBJ-0000-0001", "status": "update"}), + ], +) +async def test_async_custom_resource_actions(async_regeneratable_service, action, input_status): + request_expected_content = b'{"id":"OBJ-0000-0001","status":"update"}' + response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"} + with respx.mock: + mock_route = respx.post( + f"https://api.example.com/public/v1/dummy/regeneratable/OBJ-0000-0001/{action}" + ).mock( + return_value=httpx.Response( + status_code=httpx.codes.OK, + headers={"content-type": "application/json"}, + json=response_expected_data, + ) + ) + journal = await getattr(async_regeneratable_service, action)("OBJ-0000-0001", input_status) + + assert mock_route.call_count == 1 + request = mock_route.calls[0].request + + assert request.content == request_expected_content + assert journal.to_dict() == response_expected_data + assert isinstance(journal, DummyModel) + + +@pytest.mark.parametrize( + ("action", "input_status"), + [ + ("regenerate", None), + ("submit", None), + ("enquiry", None), + ("accept", None), + ], +) +async def test_async_custom_resource_actions_no_data( + async_regeneratable_service, action, input_status +): + request_expected_content = b"" + response_expected_data = {"id": "OBJ-0000-0001", "status": "new_status"} + with respx.mock: + mock_route = respx.post( + f"https://api.example.com/public/v1/dummy/regeneratable/OBJ-0000-0001/{action}" + ).mock( + return_value=httpx.Response( + status_code=httpx.codes.OK, + headers={"content-type": "application/json"}, + json=response_expected_data, + ) + ) + journal = await getattr(async_regeneratable_service, action)("OBJ-0000-0001", input_status) + + assert mock_route.call_count == 1 + request = mock_route.calls[0].request + + assert request.content == request_expected_content + assert journal.to_dict() == response_expected_data + assert isinstance(journal, DummyModel) diff --git a/tests/resources/catalog/__init__.py b/tests/resources/catalog/__init__.py new file mode 100644 index 0000000..e69de29