Skip to content
Draft
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/notifications/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
AsyncSubscribersService,
SubscribersService,
)
from mpt_api_client.resources.notifications.templates import (
AsyncTemplatesService,
TemplatesService,
)
from mpt_api_client.resources.notifications.webhooks import (
AsyncWebhooksService,
WebhooksService,
Expand Down Expand Up @@ -80,6 +84,11 @@ def subscribers(self) -> SubscribersService:
"""Subscriptions service."""
return SubscribersService(http_client=self.http_client)

@property
def templates(self) -> TemplatesService:
"""Templates service."""
return TemplatesService(http_client=self.http_client)

@property
def webhooks(self) -> WebhooksService:
"""Webhooks service."""
Expand Down Expand Up @@ -144,6 +153,11 @@ def subscribers(self) -> AsyncSubscribersService:
"""Subscriptions service."""
return AsyncSubscribersService(http_client=self.http_client)

@property
def templates(self) -> AsyncTemplatesService:
"""Async Templates service."""
return AsyncTemplatesService(http_client=self.http_client)

@property
def webhooks(self) -> AsyncWebhooksService:
"""Async Webhooks service."""
Expand Down
73 changes: 73 additions & 0 deletions mpt_api_client/resources/notifications/template_variants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncDisableMixin,
AsyncManagedResourceMixin,
CollectionMixin,
DisableMixin,
ManagedResourceMixin,
)
from mpt_api_client.models import Model, ResourceData
from mpt_api_client.models.model import BaseModel


class TemplateVariant(Model):
"""Notifications Template Variant resource.

Attributes:
body: Body content of the variant.
default: Whether this variant is the default one of its template.
language_code: Language code of the variant.
template: Reference to the parent template.
status: Variant status.
subject: Subject of the messages created from this variant.
audit: Audit information (created, updated events).
"""

body: str | None
default: bool | None
language_code: str | None
template: BaseModel | None
status: str | None
subject: str | None
audit: BaseModel | None


class TemplateVariantsServiceConfig:
"""Notifications Template Variants service configuration."""

_endpoint = "/public/v1/notifications/templates/{template_id}/variants"
_model_class = TemplateVariant
_collection_key = "data"


class TemplateVariantsService(
DisableMixin[TemplateVariant],
ManagedResourceMixin[TemplateVariant],
CollectionMixin[TemplateVariant],
Service[TemplateVariant],
TemplateVariantsServiceConfig,
):
"""Notifications Template Variants service."""

def activate(
self, resource_id: str, resource_data: ResourceData | None = None
) -> TemplateVariant:
"""Switch template variant to active state."""
return self._resource(resource_id).post("activate", json=resource_data)


class AsyncTemplateVariantsService(
AsyncDisableMixin[TemplateVariant],
AsyncManagedResourceMixin[TemplateVariant],
AsyncCollectionMixin[TemplateVariant],
AsyncService[TemplateVariant],
TemplateVariantsServiceConfig,
):
"""Async Notifications Template Variants service."""

async def activate(
self, resource_id: str, resource_data: ResourceData | None = None
) -> TemplateVariant:
"""Switch template variant to active state."""
return await self._resource(resource_id).post("activate", json=resource_data)
117 changes: 117 additions & 0 deletions mpt_api_client/resources/notifications/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncDisableMixin,
AsyncManagedResourceMixin,
CollectionMixin,
DisableMixin,
ManagedResourceMixin,
)
from mpt_api_client.models import Model, ResourceData
from mpt_api_client.models.model import BaseModel
from mpt_api_client.resources.notifications.template_variants import (
AsyncTemplateVariantsService,
TemplateVariantsService,
)


class Template(Model):
"""Notifications Template resource.

Attributes:
name: Template name.
category: Reference to the notification category of the template.
criteria: Criteria triggering the template automatically.
default_variant: Reference to the default variant of the template.
description: Template description.
last_used: Timestamp of the last time the template was used.
owner: Reference to the account owning the template.
schedule: Reference to the schedule triggering the template.
statistics: Usage statistics of the template.
status: Template status.
type: How the template is triggered (Event, Scheduled or Manual).
variants: References to the language-specific variants of the template.
external_id: External identifier of the template.
audit: Audit information (created, updated events).
"""

name: str | None
category: BaseModel | None
criteria: BaseModel | None
default_variant: BaseModel | None
description: str | None
last_used: str | None
owner: BaseModel | None
schedule: BaseModel | None
statistics: BaseModel | None
status: str | None
type: str | None
variants: list[BaseModel] | None
external_id: str | None
audit: BaseModel | None


class TemplatesServiceConfig:
"""Notifications Templates service configuration."""

_endpoint = "/public/v1/notifications/templates"
_model_class = Template
_collection_key = "data"


class TemplatesService(
DisableMixin[Template],
ManagedResourceMixin[Template],
CollectionMixin[Template],
Service[Template],
TemplatesServiceConfig,
):
"""Notifications Templates service."""

def activate(self, resource_id: str, resource_data: ResourceData | None = None) -> Template:
"""Switch template to active state."""
return self._resource(resource_id).post("activate", json=resource_data)

def variants(self, template_id: str) -> TemplateVariantsService:
"""Access template variants service.

Args:
template_id: Template ID.

Returns:
TemplateVariantsService
"""
return TemplateVariantsService(
http_client=self.http_client,
endpoint_params={"template_id": template_id},
)


class AsyncTemplatesService(
AsyncDisableMixin[Template],
AsyncManagedResourceMixin[Template],
AsyncCollectionMixin[Template],
AsyncService[Template],
TemplatesServiceConfig,
):
"""Async Notifications Templates service."""

async def activate(
self, resource_id: str, resource_data: ResourceData | None = None
) -> Template:
"""Switch template to active state."""
return await self._resource(resource_id).post("activate", json=resource_data)

def variants(self, template_id: str) -> AsyncTemplateVariantsService:
"""Access async template variants service.

Args:
template_id: Template ID.

Returns:
AsyncTemplateVariantsService
"""
return AsyncTemplateVariantsService(
http_client=self.http_client,
endpoint_params={"template_id": template_id},
)
6 changes: 6 additions & 0 deletions tests/unit/resources/notifications/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
AsyncSubscribersService,
SubscribersService,
)
from mpt_api_client.resources.notifications.templates import (
AsyncTemplatesService,
TemplatesService,
)
from mpt_api_client.resources.notifications.webhooks import (
AsyncWebhooksService,
WebhooksService,
Expand Down Expand Up @@ -48,6 +52,7 @@ def test_async_notifications_init(async_http_client):
("subscribers", SubscribersService),
("directories", DirectoriesService),
("footers", FootersService),
("templates", TemplatesService),
("webhooks", WebhooksService),
],
)
Expand All @@ -69,6 +74,7 @@ def test_notifications_properties(http_client, attr_name, expected):
("subscribers", AsyncSubscribersService),
("directories", AsyncDirectoriesService),
("footers", AsyncFootersService),
("templates", AsyncTemplatesService),
("webhooks", AsyncWebhooksService),
],
)
Expand Down
125 changes: 125 additions & 0 deletions tests/unit/resources/notifications/test_template_variants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import httpx
import pytest
import respx

from mpt_api_client.resources.notifications.template_variants import (
AsyncTemplateVariantsService,
TemplateVariantsService,
)

TEMPLATE_ID = "NTL-1234"
VARIANTS_PATH = f"/public/v1/notifications/templates/{TEMPLATE_ID}/variants"


@pytest.fixture
def template_variants_service(http_client):
return TemplateVariantsService(
http_client=http_client, endpoint_params={"template_id": TEMPLATE_ID}
)


@pytest.fixture
def async_template_variants_service(async_http_client):
return AsyncTemplateVariantsService(
http_client=async_http_client, endpoint_params={"template_id": TEMPLATE_ID}
)


@pytest.fixture
def template_variant_data():
return {
"id": "NTV-1234",
"body": "Hello",
"default": True,
"languageCode": "en-US",
"subject": "Order confirmed",
"status": "Active",
"template": {"id": TEMPLATE_ID, "name": "Order confirmation"},
"audit": {"created": {"at": "2024-01-01T00:00:00Z"}},
}


def test_endpoint_contains_template_id(template_variants_service):
result = template_variants_service.build_path()

assert result == VARIANTS_PATH


def test_async_endpoint_contains_template_id(async_template_variants_service):
result = async_template_variants_service.build_path()

assert result == VARIANTS_PATH


@pytest.mark.parametrize(
"method",
["get", "create", "update", "delete", "activate", "disable", "iterate", "fetch_page"],
)
def test_mixins_present(template_variants_service, method):
result = hasattr(template_variants_service, method)

assert result is True


@pytest.mark.parametrize(
"method",
["get", "create", "update", "delete", "activate", "disable", "iterate", "fetch_page"],
)
def test_async_mixins_present(async_template_variants_service, method):
result = hasattr(async_template_variants_service, method)

assert result is True


def test_get_template_variant(template_variants_service, template_variant_data):
with respx.mock:
mock_route = respx.get(f"https://api.example.com{VARIANTS_PATH}/NTV-1234").mock(
return_value=httpx.Response(
status_code=200,
headers={"content-type": "application/json"},
json=template_variant_data,
)
)

result = template_variants_service.get("NTV-1234")

assert mock_route.call_count == 1
assert result.to_dict() == template_variant_data
assert result.language_code == "en-US"
assert result.template.id == TEMPLATE_ID


@pytest.mark.parametrize("action", ["activate", "disable"])
def test_template_variant_state_actions(template_variants_service, action):
response_expected_data = {"id": "NTV-1234", "status": "Active"}
with respx.mock:
mock_route = respx.post(f"https://api.example.com{VARIANTS_PATH}/NTV-1234/{action}").mock(
return_value=httpx.Response(
status_code=200,
headers={"content-type": "application/json"},
json=response_expected_data,
)
)

result = getattr(template_variants_service, action)("NTV-1234")

assert mock_route.call_count == 1
assert result.to_dict() == response_expected_data


@pytest.mark.parametrize("action", ["activate", "disable"])
async def test_async_template_variant_state_actions(async_template_variants_service, action):
response_expected_data = {"id": "NTV-1234", "status": "Active"}
with respx.mock:
mock_route = respx.post(f"https://api.example.com{VARIANTS_PATH}/NTV-1234/{action}").mock(
return_value=httpx.Response(
status_code=200,
headers={"content-type": "application/json"},
json=response_expected_data,
)
)

result = await getattr(async_template_variants_service, action)("NTV-1234")

assert mock_route.call_count == 1
assert result.to_dict() == response_expected_data
Loading