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
11 changes: 11 additions & 0 deletions docs/hub/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,14 @@ Every method above has an async twin on `client.hub.notifications` when the
client is an `AsyncNorbix`. See [push.md](./push.md) for how to pick a push
campaign audience and a push provider, and [sms.md](./sms.md) for the same two
choices on an SMS campaign.

## Cloning a template

`clone_email_template`, `clone_sms_template` and `clone_push_template` copy a
template and answer the new copy's id, so you can open or edit the copy
without listing the templates again:

```python
copy = client.hub.notifications.clone_email_template(id="tpl_welcome")
client.hub.notifications.get_email_template(id=copy["id"])
```
30 changes: 24 additions & 6 deletions src/norbix_python/hub/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,10 @@ def archive_email_template(self, id: str, *, timeout: float | None = None, beare
)

def clone_email_template(self, id: str, *, timeout: float | None = None, bearer_token: str | None = None, **request: Any) -> Any:
"""POST /{version}/notifications/email/templates/{Id}/clone"""
"""POST /{version}/notifications/email/templates/{Id}/clone

Answers the new copy's id: ``{"id": "..."}``.
"""
return self._transport.send(
target="hub",
path="/{version}/notifications/email/templates/{Id}/clone",
Expand Down Expand Up @@ -700,7 +703,10 @@ def archive_push_template(self, id: str, *, timeout: float | None = None, bearer
)

def clone_push_template(self, id: str, *, timeout: float | None = None, bearer_token: str | None = None, **request: Any) -> Any:
"""POST /{version}/notifications/push/templates/{Id}/clone"""
"""POST /{version}/notifications/push/templates/{Id}/clone

Answers the new copy's id: ``{"id": "..."}``.
"""
return self._transport.send(
target="hub",
path="/{version}/notifications/push/templates/{Id}/clone",
Expand Down Expand Up @@ -1700,7 +1706,10 @@ def archive_sms_template(self, id: str, *, timeout: float | None = None, bearer_
)

def clone_sms_template(self, id: str, *, timeout: float | None = None, bearer_token: str | None = None, **request: Any) -> Any:
"""POST /{version}/notifications/sms/templates/{Id}/clone"""
"""POST /{version}/notifications/sms/templates/{Id}/clone

Answers the new copy's id: ``{"id": "..."}``.
"""
return self._transport.send(
target="hub",
path="/{version}/notifications/sms/templates/{Id}/clone",
Expand Down Expand Up @@ -2167,7 +2176,10 @@ async def archive_email_template(self, id: str, *, timeout: float | None = None,
)

async def clone_email_template(self, id: str, *, timeout: float | None = None, bearer_token: str | None = None, **request: Any) -> Any:
"""POST /{version}/notifications/email/templates/{Id}/clone"""
"""POST /{version}/notifications/email/templates/{Id}/clone

Answers the new copy's id: ``{"id": "..."}``.
"""
return await self._transport.send(
target="hub",
path="/{version}/notifications/email/templates/{Id}/clone",
Expand Down Expand Up @@ -2447,7 +2459,10 @@ async def archive_push_template(self, id: str, *, timeout: float | None = None,
)

async def clone_push_template(self, id: str, *, timeout: float | None = None, bearer_token: str | None = None, **request: Any) -> Any:
"""POST /{version}/notifications/push/templates/{Id}/clone"""
"""POST /{version}/notifications/push/templates/{Id}/clone

Answers the new copy's id: ``{"id": "..."}``.
"""
return await self._transport.send(
target="hub",
path="/{version}/notifications/push/templates/{Id}/clone",
Expand Down Expand Up @@ -3447,7 +3462,10 @@ async def archive_sms_template(self, id: str, *, timeout: float | None = None, b
)

async def clone_sms_template(self, id: str, *, timeout: float | None = None, bearer_token: str | None = None, **request: Any) -> Any:
"""POST /{version}/notifications/sms/templates/{Id}/clone"""
"""POST /{version}/notifications/sms/templates/{Id}/clone

Answers the new copy's id: ``{"id": "..."}``.
"""
return await self._transport.send(
target="hub",
path="/{version}/notifications/sms/templates/{Id}/clone",
Expand Down
74 changes: 74 additions & 0 deletions tests/hub/test_template_clone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""Template clone answers the new copy's id.

The gateway's clone requests (email, SMS and push templates) answer an
``IdResponse``: ``{"id": "<new template id>"}``. The SDK hands that answer to
the caller unchanged. Every call goes to a fake transport; no server is
contacted.
"""

from __future__ import annotations

import asyncio
from typing import Any

import httpx

from norbix_python import AsyncNorbix, Norbix

SOURCE_ID = "tpl_source"
COPY_ID = "tpl_copy"


def _answering(seen: list[httpx.Request]) -> Norbix:
def handler(request: httpx.Request) -> httpx.Response:
seen.append(request)
return httpx.Response(200, json={"id": COPY_ID})

return Norbix(
project_id="test-project",
bearer_token="test-token",
http_client=httpx.Client(transport=httpx.MockTransport(handler)),
)


def _assert_clone(seen: list[httpx.Request], channel: str, answer: Any) -> None:
assert answer == {"id": COPY_ID}
assert len(seen) == 1
assert seen[0].method == "POST"
assert seen[0].url.path == f"/v3/notifications/{channel}/templates/{SOURCE_ID}/clone"


def test_clone_email_template_returns_the_copy_id() -> None:
seen: list[httpx.Request] = []
answer = _answering(seen).hub.notifications.clone_email_template(id=SOURCE_ID)
_assert_clone(seen, "email", answer)


def test_clone_sms_template_returns_the_copy_id() -> None:
seen: list[httpx.Request] = []
answer = _answering(seen).hub.notifications.clone_sms_template(id=SOURCE_ID)
_assert_clone(seen, "sms", answer)


def test_clone_push_template_returns_the_copy_id() -> None:
seen: list[httpx.Request] = []
answer = _answering(seen).hub.notifications.clone_push_template(id=SOURCE_ID)
_assert_clone(seen, "push", answer)


def test_async_clone_returns_the_copy_id() -> None:
seen: list[httpx.Request] = []

async def handler(request: httpx.Request) -> httpx.Response:
seen.append(request)
return httpx.Response(200, json={"id": COPY_ID})

async def run() -> Any:
client = AsyncNorbix(
project_id="test-project",
bearer_token="test-token",
http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
)
return await client.hub.notifications.clone_email_template(id=SOURCE_ID)

_assert_clone(seen, "email", asyncio.run(run()))
Loading