Skip to content

Commit 6dd7fd7

Browse files
tests: update models
1 parent 09d6026 commit 6dd7fd7

File tree

8 files changed

+39
-49
lines changed

8 files changed

+39
-49
lines changed
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
from abc import ABC
2-
from typing import Annotated, Any, TypeAlias
2+
from typing import Any, Literal
33

4-
from pydantic import BaseModel, Field
4+
from pydantic import BaseModel, ConfigDict, EmailStr
55

66

7-
class BaseRecipient(BaseModel, ABC):
7+
class Channel(BaseModel, ABC):
88
type: str
99

10+
model_config = ConfigDict(
11+
frozen=True,
12+
)
1013

11-
class SMSRecipient(BaseRecipient):
12-
type: Annotated[str, Field(frozen=True)] = "sms"
13-
phone_number: str
1414

15+
class EmailChannel(Channel):
16+
type: Literal["email"] = "email"
17+
to: EmailStr
18+
reply_to: EmailStr | None = None
1519

16-
class EmailRecipient(BaseRecipient):
17-
type: Annotated[str, Field(frozen=True)] = "email"
18-
address: str
1920

20-
21-
Recipient: TypeAlias = Annotated[
22-
EmailRecipient | SMSRecipient, Field(discriminator="type")
23-
]
21+
class SMSChannel(Channel):
22+
type: Literal["sms"] = "sms"
23+
phone_number: str # Consider using phone number validation library here
2424

2525

2626
class NotificationMessage(BaseModel):
27-
event: str
28-
context: dict[str, Any] | None = None
27+
event_type: str # e.g. "account.registered"
28+
channel: Channel
29+
context: dict[str, Any] | None = None # Additional context for the notification

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/notifications/messages.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from models_library.api_schemas_notifications import NOTIFICATIONS_RPC_NAMESPACE
44
from models_library.rabbitmq_basic_types import RPCMethodName
5-
from models_library.rpc.notifications.messages import NotificationMessage, Recipient
5+
from models_library.rpc.notifications.messages import NotificationMessage
66
from pydantic import NonNegativeInt, TypeAdapter
77

88
from ... import RabbitMQRPCClient
@@ -14,12 +14,10 @@ async def send_notification_message(
1414
rabbitmq_rpc_client: RabbitMQRPCClient,
1515
*,
1616
message: NotificationMessage,
17-
recipients: list[Recipient],
1817
) -> None:
1918
await rabbitmq_rpc_client.request(
2019
NOTIFICATIONS_RPC_NAMESPACE,
2120
TypeAdapter(RPCMethodName).validate_python("send_notification_message"),
2221
timeout_s=_DEFAULT_TIMEOUT_S,
2322
message=message,
24-
recipients=recipients,
2523
)

services/notifications/src/simcore_service_notifications/api/rpc/_notifications.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from models_library.rpc.notifications.messages import NotificationMessage, Recipient
1+
from models_library.rpc.notifications.messages import NotificationMessage
22
from servicelib.celery.task_manager import TaskManager
33
from servicelib.rabbitmq import RPCRouter
44

@@ -12,10 +12,8 @@ async def send_notification_message(
1212
task_manager: TaskManager,
1313
*,
1414
message: NotificationMessage,
15-
recipients: list[Recipient],
1615
) -> None:
1716
await notifications_service.send_notification_message(
1817
task_manager,
1918
message=message,
20-
recipients=recipients,
2119
)

services/notifications/src/simcore_service_notifications/clients/celery.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from fastapi import FastAPI
88
from fastapi_lifespan_manager import State
99
from models_library.rpc.notifications.messages import (
10-
EmailRecipient,
10+
EmailChannel,
1111
NotificationMessage,
12-
SMSRecipient,
12+
SMSChannel,
1313
)
1414
from settings_library.celery import CelerySettings
1515

@@ -28,7 +28,7 @@ async def celery_lifespan(app: FastAPI) -> AsyncIterator[State]:
2828
)
2929

3030
register_celery_types()
31-
register_pydantic_types(NotificationMessage, EmailRecipient, SMSRecipient)
31+
register_pydantic_types(NotificationMessage, EmailChannel, SMSChannel)
3232
yield {}
3333

3434

services/notifications/src/simcore_service_notifications/modules/celery/_email_tasks.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import logging
44

5-
from celery import Task # type: ignore[import-untyped]
6-
from models_library.rpc.notifications.messages import (
7-
EmailRecipient,
8-
NotificationMessage,
9-
)
5+
from celery import Task
6+
from models_library.rpc.notifications.messages import EmailChannel, NotificationMessage
107
from servicelib.celery.models import TaskID
118

129
_logger = logging.getLogger(__name__)
@@ -18,8 +15,7 @@ async def send_email(
1815
task: Task,
1916
task_id: TaskID,
2017
message: NotificationMessage,
21-
recipient: EmailRecipient,
2218
) -> None:
23-
# TODO: render email template with message and recipient details
24-
# and send the email using an email service
25-
_logger.info("Sending email notification to %s", recipient.address)
19+
assert isinstance(message.channel, EmailChannel) # nosec
20+
21+
_logger.info("Sending email notification to %s", message.channel.to)

services/notifications/src/simcore_service_notifications/modules/celery/tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from celery_library.task import register_task
66
from celery_library.types import register_celery_types, register_pydantic_types
77
from models_library.rpc.notifications.messages import (
8-
EmailRecipient,
8+
EmailChannel,
99
NotificationMessage,
10-
SMSRecipient,
10+
SMSChannel,
1111
)
1212
from servicelib.logging_utils import log_context
1313

@@ -25,7 +25,7 @@ class TaskQueue(StrEnum):
2525

2626
def setup_worker_tasks(app: Celery) -> None:
2727
register_celery_types()
28-
register_pydantic_types(NotificationMessage, EmailRecipient, SMSRecipient)
28+
register_pydantic_types(NotificationMessage, EmailChannel, SMSChannel)
2929

3030
with log_context(_logger, logging.INFO, msg="worker tasks registration"):
3131
register_task(
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from models_library.rpc.notifications.messages import NotificationMessage, Recipient
3+
from models_library.rpc.notifications.messages import NotificationMessage
44
from servicelib.celery.models import TaskContext
55
from servicelib.celery.task_manager import TaskManager
66

@@ -13,13 +13,10 @@ async def send_notification_message(
1313
task_manager: TaskManager,
1414
*,
1515
message: NotificationMessage,
16-
recipients: list[Recipient],
1716
) -> None:
18-
for recipient in recipients:
19-
await task_manager.send_task(
20-
name=f"notifications.{recipient.type}",
21-
context=TaskContext(), # TODO: TaskFilter
22-
queue=TaskQueue.DEFAULT,
23-
message=message,
24-
recipient=recipient,
25-
)
17+
await task_manager.send_task(
18+
name=f"notifications.{message.event_type}",
19+
context=TaskContext(), # TODO: TaskFilter
20+
queue=TaskQueue.DEFAULT,
21+
message=message,
22+
)

services/notifications/tests/unit/test_tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from servicelib.rabbitmq.rpc_interfaces.notifications.messages import (
55
send_notification_message,
66
)
7-
from simcore_service_notifications.clients.celery import EmailRecipient
7+
from simcore_service_notifications.clients.celery import EmailChannel
88

99
pytest_simcore_core_services_selection = [
1010
"rabbit",
@@ -23,8 +23,8 @@ async def test_send_email(
2323
await send_notification_message(
2424
notifications_rabbitmq_rpc_client,
2525
message=NotificationMessage(
26-
event="test_event",
26+
event_type="on_account_requested",
27+
channel=EmailChannel(to="[email protected]"),
2728
context={"key": "value"},
2829
),
29-
recipients=[EmailRecipient(address="[email protected]")],
3030
)

0 commit comments

Comments
 (0)