diff --git a/mpt_api_client/resources/catalog/catalog.py b/mpt_api_client/resources/catalog/catalog.py index 203fd30b..01ae068e 100644 --- a/mpt_api_client/resources/catalog/catalog.py +++ b/mpt_api_client/resources/catalog/catalog.py @@ -9,6 +9,10 @@ AsyncPriceListsService, PriceListsService, ) +from mpt_api_client.resources.catalog.pricing_policies import ( + AsyncPricingPoliciesService, + PricingPoliciesService, +) from mpt_api_client.resources.catalog.products import AsyncProductsService, ProductsService from mpt_api_client.resources.catalog.units_of_measure import ( AsyncUnitsOfMeasureService, @@ -37,6 +41,11 @@ def price_lists(self) -> PriceListsService: """Price Lists service.""" return PriceListsService(http_client=self.http_client) + @property + def pricing_policies(self) -> PricingPoliciesService: + """Pricing policies service.""" + return PricingPoliciesService(http_client=self.http_client) + @property def products(self) -> ProductsService: """Products service.""" @@ -74,6 +83,11 @@ def price_lists(self) -> AsyncPriceListsService: """Price Lists service.""" return AsyncPriceListsService(http_client=self.http_client) + @property + def pricing_policies(self) -> AsyncPricingPoliciesService: + """Pricing policies service.""" + return AsyncPricingPoliciesService(http_client=self.http_client) + @property def products(self) -> AsyncProductsService: """Products service.""" diff --git a/mpt_api_client/resources/catalog/pricing_policies.py b/mpt_api_client/resources/catalog/pricing_policies.py new file mode 100644 index 00000000..e541846a --- /dev/null +++ b/mpt_api_client/resources/catalog/pricing_policies.py @@ -0,0 +1,87 @@ +from mpt_api_client.http import ( + AsyncCreateMixin, + AsyncDeleteMixin, + AsyncService, + CreateMixin, + DeleteMixin, + Service, +) +from mpt_api_client.models import Model, ResourceData + + +class PricingPolicy(Model): + """Pricing policy resource.""" + + +class PricingPoliciesServiceConfig: + """Pricing policy service config.""" + + _endpoint = "/public/v1/catalog/pricing-policies" + _model_class = PricingPolicy + _collection_key = "data" + + +class PricingPoliciesService( # noqa: WPS215 + CreateMixin[PricingPolicy], + DeleteMixin, + Service[PricingPolicy], + PricingPoliciesServiceConfig, +): + """Pricing policies service.""" + + def activate(self, resource_id: str, resource_data: ResourceData) -> PricingPolicy: + """Activate pricing policy. + + Args: + resource_id: Pricing policy resource ID + resource_data: Pricing policy resource data + + Returns: + Activated pricing policy. + """ + return self._resource_action(resource_id, "POST", "activate", json=resource_data) + + def disable(self, resource_id: str, resource_data: ResourceData) -> PricingPolicy: + """Disable pricing policy. + + Args: + resource_id: Pricing policy resource ID + resource_data: Pricing policy resource data + + Returns: + Disabled pricing policy. + """ + return self._resource_action(resource_id, "POST", "disable", json=resource_data) + + +class AsyncPricingPoliciesService( + AsyncCreateMixin[PricingPolicy], + AsyncDeleteMixin, + AsyncService[PricingPolicy], + PricingPoliciesServiceConfig, +): + """Async pricing policies service.""" + + async def activate(self, resource_id: str, resource_data: ResourceData) -> PricingPolicy: + """Activate pricing policy. + + Args: + resource_id: Pricing policy resource ID + resource_data: Pricing policy resource data + + Returns: + Activated pricing policy. + """ + return await self._resource_action(resource_id, "POST", "activate", json=resource_data) + + async def disable(self, resource_id: str, resource_data: ResourceData) -> PricingPolicy: + """Disable pricing policy. + + Args: + resource_id: Pricing policy resource ID + resource_data: Pricing policy resource data + + Returns: + Disabled pricing policy. + """ + return await self._resource_action(resource_id, "POST", "disable", json=resource_data) diff --git a/setup.cfg b/setup.cfg index a3f34980..c63bdc27 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,7 +32,7 @@ extend-ignore = per-file-ignores = - mpt_api_client/resources/catalog/*.py: WPS110 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 mpt_api_client/resources/catalog/products.py: WPS204 WPS214 WPS215 diff --git a/tests/resources/catalog/test_catalog.py b/tests/resources/catalog/test_catalog.py index 3fefed1e..2ba2163e 100644 --- a/tests/resources/catalog/test_catalog.py +++ b/tests/resources/catalog/test_catalog.py @@ -11,6 +11,10 @@ AsyncPriceListsService, PriceListsService, ) +from mpt_api_client.resources.catalog.pricing_policies import ( + AsyncPricingPoliciesService, + PricingPoliciesService, +) from mpt_api_client.resources.catalog.products import AsyncProductsService, ProductsService from mpt_api_client.resources.catalog.units_of_measure import ( AsyncUnitsOfMeasureService, @@ -34,6 +38,7 @@ def async_catalog(async_http_client): ("authorizations", AuthorizationsService), ("listings", ListingsService), ("price_lists", PriceListsService), + ("pricing_policies", PricingPoliciesService), ("products", ProductsService), ("units_of_measure", UnitsOfMeasureService), ("items", ItemsService), @@ -53,6 +58,7 @@ def test_catalog_properties(catalog, property_name, expected_service_class): ("authorizations", AsyncAuthorizationsService), ("listings", AsyncListingsService), ("price_lists", AsyncPriceListsService), + ("pricing_policies", AsyncPricingPoliciesService), ("products", AsyncProductsService), ("units_of_measure", AsyncUnitsOfMeasureService), ("items", AsyncItemsService), diff --git a/tests/resources/catalog/test_pricing_policies.py b/tests/resources/catalog/test_pricing_policies.py new file mode 100644 index 00000000..e69779dc --- /dev/null +++ b/tests/resources/catalog/test_pricing_policies.py @@ -0,0 +1,110 @@ +import httpx +import pytest +import respx + +from mpt_api_client.resources.catalog.pricing_policies import ( + AsyncPricingPoliciesService, + PricingPoliciesService, +) + + +@pytest.fixture +def pricing_policies_service(http_client): + return PricingPoliciesService(http_client=http_client) + + +@pytest.fixture +def async_pricing_policies_service(async_http_client): + return AsyncPricingPoliciesService(http_client=async_http_client) + + +def test_activate(pricing_policies_service): + pricing_policy_expected = { + "id": "PRP-0000-0001", + "status": "Active", + "name": "Active Pricing Policy", + } + with respx.mock: + respx.post( + "https://api.example.com/public/v1/catalog/pricing-policies/PRP-0000-0001/activate" + ).mock( + return_value=httpx.Response( + status_code=httpx.codes.OK, + json=pricing_policy_expected, + ) + ) + + pricing_policy_activated = pricing_policies_service.activate( + "PRP-0000-0001", {"name": "Active Pricing Policy"} + ) + + assert pricing_policy_activated.to_dict() == pricing_policy_expected + + +async def test_async_activate(async_pricing_policies_service): + pricing_policy_expected = { + "id": "PRP-0000-0001", + "status": "Active", + "name": "Active Pricing Policy", + } + with respx.mock: + respx.post( + "https://api.example.com/public/v1/catalog/pricing-policies/PRP-0000-0001/activate" + ).mock( + return_value=httpx.Response( + status_code=httpx.codes.OK, + json=pricing_policy_expected, + ) + ) + + pricing_policy_activated = await async_pricing_policies_service.activate( + "PRP-0000-0001", {"name": "Active Pricing Policy"} + ) + + assert pricing_policy_activated.to_dict() == pricing_policy_expected + + +def test_disable(pricing_policies_service): + pricing_policy_expected = { + "id": "PRP-0000-0001", + "status": "Inactive", + "name": "Inactive Pricing Policy", + } + with respx.mock: + respx.post( + "https://api.example.com/public/v1/catalog/pricing-policies/PRP-0000-0001/disable" + ).mock( + return_value=httpx.Response( + status_code=httpx.codes.OK, + json=pricing_policy_expected, + ) + ) + + pricing_policy_disabled = pricing_policies_service.disable( + "PRP-0000-0001", {"name": "Inactive Pricing Policy"} + ) + + assert pricing_policy_disabled.to_dict() == pricing_policy_expected + + +async def test_async_disable(async_pricing_policies_service): + pricing_policy_expected = { + "id": "PRP-0000-0001", + "status": "Inactive", + "name": "Inactive Pricing Policy", + } + with respx.mock: + respx.post( + "https://api.example.com/public/v1/catalog/pricing-policies/PRP-0000-0001/disable" + ).mock( + return_value=httpx.Response( + status_code=httpx.codes.OK, + json=pricing_policy_expected, + ) + ) + + pricing_policy_disabled = await async_pricing_policies_service.disable( + "PRP-0000-0001", {"name": "Inactive Pricing Policy"} + ) + + assert pricing_policy_disabled.to_dict() == pricing_policy_expected