Skip to content

Commit

Permalink
Eat notification object in .notify
Browse files Browse the repository at this point in the history
  • Loading branch information
TheByronHimes committed Mar 8, 2024
1 parent aad4766 commit 2794cd1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/nos/adapters/outbound/event_pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pydantic import Field
from pydantic_settings import BaseSettings

from nos.core.notifications import Notification
from nos.ports.outbound.notification_emitter import NotificationEmitterPort

__all__ = ["NotificationEmitterConfig", "NotificationEmitter"]
Expand Down Expand Up @@ -56,14 +57,14 @@ def __init__(
self._event_publisher = event_publisher

async def notify(
self, *, email: str, full_name: str, subject: str, text: str
self, *, email: str, full_name: str, notification: Notification
) -> None:
"""Send notification to the specified email address."""
payload: JsonObject = event_schemas.Notification(
recipient_email=email,
recipient_name=full_name,
subject=subject,
plaintext_body=text,
subject=notification.subject,
plaintext_body=notification.text,
).model_dump()

await self._event_publisher.publish(
Expand Down
7 changes: 5 additions & 2 deletions src/nos/core/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ class Notification(NamedTuple):
subject: str
text: str

def format_text(self, **kwargs):
def formatted(self, **kwargs) -> "Notification":
"""Perform string interpolation on the `text` attribute.
Returns a new Notification object with the subject and interpolated
text of the original.
Raises a KeyError if the required template keys are not provided.
"""
try:
return self.text.format(**kwargs)
return Notification(self.subject, self.text.format(**kwargs))
except KeyError:
log.error(
"Unable to format notification text with kwargs %s",
Expand Down
18 changes: 6 additions & 12 deletions src/nos/core/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ async def process_access_request_created(self, *, user_id: str, dataset_id: str)
await self._notification_emitter.notify(
email=user.email,
full_name=user.name,
subject=notifications.ACCESS_REQUEST_CREATED_TO_USER.subject,
text=notifications.ACCESS_REQUEST_CREATED_TO_USER.format_text(
notification=notifications.ACCESS_REQUEST_CREATED_TO_USER.formatted(
dataset_id=dataset_id
),
)
Expand All @@ -85,8 +84,7 @@ async def process_access_request_created(self, *, user_id: str, dataset_id: str)
await self._notification_emitter.notify(
email=self._config.central_data_steward_email,
full_name=DATA_STEWARD_NAME,
subject=notifications.ACCESS_REQUEST_CREATED_TO_DS.subject,
text=notifications.ACCESS_REQUEST_CREATED_TO_DS.format_text(
notification=notifications.ACCESS_REQUEST_CREATED_TO_DS.formatted(
full_user_name=user.name, email=user.email, dataset_id=dataset_id
),
)
Expand Down Expand Up @@ -125,8 +123,7 @@ async def process_access_request_allowed(self, *, user_id: str, dataset_id: str)
await self._notification_emitter.notify(
email=user.email,
full_name=user.name,
subject=notifications.ACCESS_REQUEST_ALLOWED_TO_USER.subject,
text=notifications.ACCESS_REQUEST_ALLOWED_TO_USER.format_text(
notification=notifications.ACCESS_REQUEST_ALLOWED_TO_USER.formatted(
dataset_id=dataset_id
),
)
Expand All @@ -136,8 +133,7 @@ async def process_access_request_allowed(self, *, user_id: str, dataset_id: str)
await self._notification_emitter.notify(
email=self._config.central_data_steward_email,
full_name=DATA_STEWARD_NAME,
subject=notifications.ACCESS_REQUEST_ALLOWED_TO_DS.subject,
text=notifications.ACCESS_REQUEST_ALLOWED_TO_DS.format_text(
notification=notifications.ACCESS_REQUEST_ALLOWED_TO_DS.formatted(
full_user_name=user.name, dataset_id=dataset_id
),
)
Expand Down Expand Up @@ -175,8 +171,7 @@ async def process_access_request_denied(self, *, user_id: str, dataset_id: str):
await self._notification_emitter.notify(
email=user.email,
full_name=user.name,
subject=notifications.ACCESS_REQUEST_DENIED_TO_USER.subject,
text=notifications.ACCESS_REQUEST_DENIED_TO_USER.format_text(
notification=notifications.ACCESS_REQUEST_DENIED_TO_USER.formatted(
dataset_id=dataset_id
),
)
Expand All @@ -186,8 +181,7 @@ async def process_access_request_denied(self, *, user_id: str, dataset_id: str):
await self._notification_emitter.notify(
email=self._config.central_data_steward_email,
full_name=DATA_STEWARD_NAME,
subject=notifications.ACCESS_REQUEST_DENIED_TO_DS.subject,
text=notifications.ACCESS_REQUEST_DENIED_TO_DS.format_text(
notification=notifications.ACCESS_REQUEST_DENIED_TO_DS.formatted(
full_user_name=user.name, dataset_id=dataset_id
),
)
Expand Down
4 changes: 3 additions & 1 deletion src/nos/ports/outbound/notification_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from abc import ABC, abstractmethod

from nos.core.notifications import Notification

__all__ = ["NotificationEmitterPort"]


Expand All @@ -26,7 +28,7 @@ class NotificationEmitterPort(ABC):

@abstractmethod
async def notify(
self, *, email: str, full_name: str, subject: str, text: str
self, *, email: str, full_name: str, notification: Notification
) -> None:
"""Send notification to the specified email address."""
...

0 comments on commit 2794cd1

Please sign in to comment.