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/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},
)
Empty file.
92 changes: 92 additions & 0 deletions tests/e2e/notifications/templates/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import pytest

from tests.e2e.helper import (
async_create_fixture_resource_and_delete,
create_fixture_resource_and_delete,
)

# A template can only be activated once it has a default variant, and the first variant
# activated on a template becomes its default. Deleting a template soft-deletes its variants,
# so the variant fixtures only create and leave the cleanup to the template teardown.


@pytest.fixture
def templates_service(mpt_ops):
return mpt_ops.notifications.templates


@pytest.fixture
def async_templates_service(async_mpt_ops):
return async_mpt_ops.notifications.templates


@pytest.fixture
def template_data(short_uuid, category_id):
return {
"name": f"e2e template {short_uuid}",
"description": f"e2e template - please delete {short_uuid}",
"type": "Manual",
"category": {"id": category_id},
}


@pytest.fixture
def template_variant_data(short_uuid):
return {
"languageCode": "en-US",
"subject": f"e2e template variant {short_uuid}",
"body": f"e2e template variant body {short_uuid}",
}


@pytest.fixture
def created_template(templates_service, template_data):
with create_fixture_resource_and_delete(templates_service, template_data) as template:
yield template


@pytest.fixture
async def async_created_template(async_templates_service, template_data):
async with async_create_fixture_resource_and_delete(
async_templates_service, template_data
) as template:
yield template


@pytest.fixture
def template_variants_service(templates_service, created_template):
return templates_service.variants(created_template.id)


@pytest.fixture
def async_template_variants_service(async_templates_service, async_created_template):
return async_templates_service.variants(async_created_template.id)


@pytest.fixture
def created_template_variant(template_variants_service, template_variant_data):
return template_variants_service.create(template_variant_data)


@pytest.fixture
async def async_created_template_variant(async_template_variants_service, template_variant_data):
return await async_template_variants_service.create(template_variant_data)


@pytest.fixture
def active_template(
templates_service, template_variants_service, created_template, created_template_variant
):
template_variants_service.activate(created_template_variant.id)
return templates_service.activate(created_template.id)


@pytest.fixture
async def async_active_template(
async_templates_service,
async_template_variants_service,
async_created_template,
async_created_template_variant,
):
await async_template_variants_service.activate(async_created_template_variant.id)
return await async_templates_service.activate(async_created_template.id)
Loading
Loading