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
97 changes: 0 additions & 97 deletions mpt_api_client/resources/commerce/mixins.py

This file was deleted.

18 changes: 18 additions & 0 deletions mpt_api_client/resources/commerce/mixins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from mpt_api_client.resources.commerce.mixins.render_mixin import AsyncRenderMixin, RenderMixin
from mpt_api_client.resources.commerce.mixins.template_mixin import (
AsyncTemplateMixin,
TemplateMixin,
)
from mpt_api_client.resources.commerce.mixins.terminate_mixin import (
AsyncTerminateMixin,
TerminateMixin,
)

__all__ = [ # noqa: WPS410
"AsyncRenderMixin",
"AsyncTemplateMixin",
"AsyncTerminateMixin",
"RenderMixin",
"TemplateMixin",
"TerminateMixin",
]
30 changes: 30 additions & 0 deletions mpt_api_client/resources/commerce/mixins/render_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class RenderMixin[Model]:
"""Render resource mixin."""

def render(self, resource_id: str) -> str:
"""Render resource.

Args:
resource_id: Resource ID

Returns:
Rendered resource.
"""
response = self._resource_do_request(resource_id, action="render") # type: ignore[attr-defined]
return response.text # type: ignore[no-any-return]


class AsyncRenderMixin[Model]:
"""Asynchronous render resource mixin."""

async def render(self, resource_id: str) -> str:
"""Render resource.

Args:
resource_id: Resource ID

Returns:
Rendered resource.
"""
response = await self._resource_do_request(resource_id, action="render") # type: ignore[attr-defined]
return response.text # type: ignore[no-any-return]
31 changes: 31 additions & 0 deletions mpt_api_client/resources/commerce/mixins/template_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class TemplateMixin[Model]:
"""Template resource mixin."""

def template(self, resource_id: str) -> str:
"""Get resource template.

Args:
resource_id: Resource ID

Returns:
Resource template.
"""
response = self._resource_do_request(resource_id, action="template") # type: ignore[attr-defined]
return response.text # type: ignore[no-any-return]


class AsyncTemplateMixin[Model]:
"""Asynchronous template resource mixin."""

async def template(self, resource_id: str) -> str:
"""Get resource template.

Args:
resource_id: Resource ID

Returns:
Resource template.
"""
# pylint: disable=duplicate-code
response = await self._resource_do_request(resource_id, action="template") # type: ignore[attr-defined]
return response.text # type: ignore[no-any-return]
33 changes: 33 additions & 0 deletions mpt_api_client/resources/commerce/mixins/terminate_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from mpt_api_client.models.model import ResourceData


class TerminateMixin[Model]:
"""Terminate resource mixin."""

def terminate(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Terminate resource.

Args:
resource_id: Resource ID
resource_data: Resource data

Returns:
Terminated resource.
"""
return self._resource_action(resource_id, "POST", "terminate", json=resource_data) # type: ignore[attr-defined, no-any-return]


class AsyncTerminateMixin[Model]:
"""Asynchronous terminate resource mixin."""

async def terminate(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Terminate resource.

Args:
resource_id: Resource ID
resource_data: Resource data

Returns:
Terminated resource.
"""
return await self._resource_action(resource_id, "POST", "terminate", json=resource_data) # type: ignore[attr-defined, no-any-return]
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ per-file-ignores = [
"tests/unit/resources/accounts/*.py: WPS204 WPS202 WPS210",
"tests/unit/resources/catalog/test_products.py: WPS202 WPS210",
"tests/unit/resources/commerce/*.py: WPS202 WPS204",
"tests/unit/resources/*/test_mixins.py: WPS118 WPS202 WPS204 WPS235",
"tests/unit/test_mpt_client.py: WPS235",
"tests/*: WPS432 WPS202",
]
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/resources/commerce/mixins/test_render_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import httpx
import respx

from mpt_api_client.http import AsyncService, Service
from mpt_api_client.resources.commerce.mixins.render_mixin import AsyncRenderMixin, RenderMixin
from tests.unit.conftest import DummyModel


class DummyRenderService(
RenderMixin[DummyModel],
Service[DummyModel],
):
_endpoint = "public/v1/dummy/render"
_model_class = DummyModel


class AsyncDummyRenderService(
AsyncRenderMixin[DummyModel],
AsyncService[DummyModel],
):
_endpoint = "public/v1/dummy/render"
_model_class = DummyModel


def test_render(http_client):
service = DummyRenderService(http_client=http_client)
rendered_content = "<h1>Dummy Rendered Content</h1>"
with respx.mock:
respx.get("https://api.example.com/public/v1/dummy/render/DUMMY-123/render").mock(
return_value=httpx.Response(
status_code=httpx.codes.OK,
headers={"content-type": "text/html"},
content=rendered_content,
)
)

result = service.render("DUMMY-123")

assert result == rendered_content


async def test_async_render(async_http_client):
service = AsyncDummyRenderService(http_client=async_http_client)
rendered_content = "<h1>Dummy Rendered Content</h1>"
with respx.mock:
respx.get("https://api.example.com/public/v1/dummy/render/DUMMY-123/render").mock(
return_value=httpx.Response(
status_code=httpx.codes.OK,
headers={"content-type": "text/html"},
content=rendered_content,
)
)

result = await service.render("DUMMY-123")

assert result == rendered_content
57 changes: 57 additions & 0 deletions tests/unit/resources/commerce/mixins/test_template_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import httpx
import respx

from mpt_api_client.http import AsyncService, Service
from mpt_api_client.resources.commerce.mixins.template_mixin import (
AsyncTemplateMixin,
TemplateMixin,
)
from tests.unit.conftest import DummyModel


class DummyTemplateService(
TemplateMixin[DummyModel],
Service[DummyModel],
):
_endpoint = "public/v1/dummy/template"
_model_class = DummyModel


class AsyncDummyTemplateService(
AsyncTemplateMixin[DummyModel],
AsyncService[DummyModel],
):
_endpoint = "public/v1/dummy/template"
_model_class = DummyModel


def test_template(http_client):
service = DummyTemplateService(http_client=http_client)
template_content = "<h1>Dummy Template Content</h1>"
with respx.mock:
respx.get("https://api.example.com/public/v1/dummy/template/DUMMY-123/template").mock(
return_value=httpx.Response(
status_code=httpx.codes.OK,
content=template_content,
)
)

result = service.template("DUMMY-123")

assert result == template_content


async def test_async_template(async_http_client):
service = AsyncDummyTemplateService(http_client=async_http_client)
template_content = "<h1>Dummy Template Content</h1>"
with respx.mock:
respx.get("https://api.example.com/public/v1/dummy/template/DUMMY-123/template").mock(
return_value=httpx.Response(
status_code=httpx.codes.OK,
content=template_content,
)
)

result = await service.template("DUMMY-123")

assert result == template_content
Loading