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
14 changes: 14 additions & 0 deletions mpt_api_client/resources/accounts/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
UpdateMixin,
)
from mpt_api_client.models import Model
from mpt_api_client.resources.accounts.mixins import (
ActivatableMixin,
AsyncActivatableMixin,
AsyncEnablableMixin,
AsyncValidateMixin,
EnablableMixin,
ValidateMixin,
)


class Account(Model):
Expand All @@ -23,6 +31,9 @@ class AccountsServiceConfig:
class AccountsService(
CreateMixin[Account],
UpdateMixin[Account],
ActivatableMixin[Account],
EnablableMixin[Account],
ValidateMixin[Account],
Service[Account],
AccountsServiceConfig,
):
Expand All @@ -32,6 +43,9 @@ class AccountsService(
class AsyncAccountsService(
AsyncCreateMixin[Account],
AsyncUpdateMixin[Account],
AsyncActivatableMixin[Account],
AsyncEnablableMixin[Account],
AsyncValidateMixin[Account],
AsyncService[Account],
AccountsServiceConfig,
):
Expand Down
140 changes: 140 additions & 0 deletions mpt_api_client/resources/accounts/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
from mpt_api_client.models import ResourceData

# TODO: Consider reorganizing functions in mixins to reduce duplication and differences amongst
# different domains


class ActivatableMixin[Model]:
"""Activatable mixin for activating, enabling, disabling and deactivating resources."""

def activate(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Activate a 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", "activate", json=resource_data
)

def deactivate(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Deactivate a 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", "deactivate", json=resource_data
)


class EnablableMixin[Model]:
"""Enablable mixin for enabling and disabling resources."""

def enable(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Enable a 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", "enable", json=resource_data
)

def disable(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Disable a 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", "disable", json=resource_data
)


class ValidateMixin[Model]:
"""Validate mixin adds the ability to validate a resource."""

def validate(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Validate a resource.

Args:
resource_id: Resource ID
resource_data: Resource data will be validated
"""
return self._resource_action( # type: ignore[attr-defined, no-any-return]
resource_id, "POST", "validate", json=resource_data
)


class AsyncActivatableMixin[Model]:
"""Async activatable mixin for activating, enabling, disabling and deactivating resources."""

async def activate(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Activate a 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", "activate", json=resource_data
)

async def deactivate(
self, resource_id: str, resource_data: ResourceData | None = None
) -> Model:
"""Deactivate a 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", "deactivate", json=resource_data
)


class AsyncEnablableMixin[Model]:
"""Asynchronous Enablable mixin for enabling and disabling resources."""

async def enable(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Enable a 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", "enable", json=resource_data
)

async def disable(self, resource_id: str, resource_data: ResourceData | None = None) -> Model:
"""Disable a 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", "disable", json=resource_data
)


class AsyncValidateMixin[Model]:
"""Asynchronous Validate mixin adds the ability to validate a resource."""

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

Args:
resource_id: Resource ID
resource_data: Resource data will be validated
"""
return await self._resource_action( # type: ignore[attr-defined, no-any-return]
resource_id, "POST", "validate", json=resource_data
)
8 changes: 6 additions & 2 deletions tests/resources/accounts/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ def async_account_service(async_http_client):
return AsyncAccountsService(http_client=async_http_client)


@pytest.mark.parametrize("method", ["get", "create", "update"])
@pytest.mark.parametrize(
"method", ["get", "create", "update", "enable", "disable", "activate", "deactivate", "validate"]
)
def test_mixins_present(account_service, method):
assert hasattr(account_service, method)


@pytest.mark.parametrize("method", ["get", "create", "update"])
@pytest.mark.parametrize(
"method", ["get", "create", "update", "enable", "disable", "activate", "deactivate", "validate"]
)
def test_async_mixins_present(async_account_service, method):
assert hasattr(async_account_service, method)
Loading