Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for subscriber creation in trigger function #61

Open
wants to merge 22 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ef56cc6
FEATURE: Add notifications ressource in wrapper #9(update the api and…
VeldaKiara Jul 11, 2023
f503a65
format changes
VeldaKiara Jul 11, 2023
299aca8
Added the tests for the notification wrapper
VeldaKiara Jul 13, 2023
eae9809
wip: updates requested with issues
VeldaKiara Aug 23, 2023
407211d
activity notification changes to the DTO
VeldaKiara Sep 8, 2023
f4687bd
requested changes on review of activity notification updates
VeldaKiara Sep 9, 2023
c4378f8
missed changes
VeldaKiara Sep 10, 2023
65a2c94
modifications based on the review
VeldaKiara Sep 12, 2023
468d7f9
Merge branch 'novuhq:main' into main
VeldaKiara Sep 12, 2023
070c25e
remove commented code
ryshu Sep 12, 2023
072c66a
try to fix mypy error
ryshu Sep 13, 2023
28db8d2
try to fix activity graph serialization
ryshu Sep 13, 2023
1d46799
missing channel import
ryshu Sep 13, 2023
4ed8f4f
fix: fixes on lint, pre-commit and notification stats
ryshu Sep 13, 2023
4f9d629
fix: add missing py.typed file
ryshu Sep 13, 2023
4da1dbd
support for subscriber creation in trigger function
VeldaKiara Sep 19, 2023
e7b3dcc
Merge branch 'novuhq:main' into main
VeldaKiara Sep 19, 2023
4af8f21
Merge branch 'novuhq:main' into main
VeldaKiara Nov 14, 2023
f814151
wip: Changes to the tests coverage and feature update
VeldaKiara Jan 12, 2024
e730b4d
Merge branch 'novuhq:main' into main
VeldaKiara Jan 12, 2024
2037062
feature update with test coverage
VeldaKiara Jan 12, 2024
6aa2515
wip: additional rectifying of tests
VeldaKiara Jan 12, 2024
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
34 changes: 29 additions & 5 deletions novu/api/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from novu.api.base import Api
from novu.constants import EVENTS_ENDPOINT
from novu.dto.event import EventDto, InputEventDto
from novu.dto.event import EventDto, InputEventDto, RecipientDto
from novu.dto.topic import TriggerTopicDto


Expand All @@ -29,7 +29,7 @@ def __init__(
def trigger(
self,
name: str,
recipients: Union[str, List[str]],
recipients: Union[str, Iterable[str], RecipientDto, Iterable[RecipientDto]],
payload: dict,
overrides: Optional[dict] = None,
transaction_id: Optional[str] = None,
Expand All @@ -51,7 +51,14 @@ def trigger(

Args:
name: The name of the template trigger to activate.
recipients: A subscriber ID (or a list of subscriber ID) to reach with this trigger
recipients:
One of following format (to define the recipient of the event):

* A subscriber ID
* A list of subscriber ID
* A SubscriberDto
* A list of SubscriberDto

payload:
A JSON serializable python dict to pass additional custom information that could be used to render the
template, or perform routing rules based on it. This data will also be available when fetching the
Expand All @@ -72,7 +79,20 @@ def trigger(
Returns:
Create Event definition in Novu
"""
payload = {"name": name, "to": recipients, "payload": payload}
tmp = [recipients] if not isinstance(recipients, Iterable) else recipients

to: List[Union[str, dict]] = []
for item in tmp:
if isinstance(item, str):
to.append(item)
elif isinstance(item, RecipientDto):
to.append(item.to_camel_case())

else:
raise ValueError(
f"Provided format for the 'recipients' arguments ({type(item)}) isn't supported by the SDK."
)
payload = {"name": name, "to": to, "payload": payload}
if overrides:
payload["overrides"] = overrides
if actor:
Expand Down Expand Up @@ -161,7 +181,11 @@ def trigger_topic(
"""
_recipients = topics if isinstance(topics, Iterable) else [topics]

payload = {"name": name, "to": [r.to_camel_case() for r in _recipients], "payload": payload}
payload = {
"name": name,
"to": [r.to_camel_case() for r in _recipients],
"payload": payload,
}
if overrides:
payload["overrides"] = overrides
if actor:
Expand Down
4 changes: 3 additions & 1 deletion novu/dto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
EnvironmentDto,
EnvironmentWidgetDto,
)
from novu.dto.event import EventDto
from novu.dto.event import EventDto, InputEventDto, RecipientDto
from novu.dto.execution_detail import ExecutionDetailDto
from novu.dto.feed import FeedDto
from novu.dto.field import FieldFilterPartDto
Expand Down Expand Up @@ -66,6 +66,8 @@
"EnvironmentDto",
"EnvironmentWidgetDto",
"EventDto",
"RecipientDto",
"InputEventDto",
"ExecutionDetailDto",
"FeedDto",
"FieldFilterPartDto",
Expand Down
39 changes: 35 additions & 4 deletions novu/dto/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import dataclasses
from typing import List, Optional, Union
VeldaKiara marked this conversation as resolved.
Show resolved Hide resolved

from novu.dto.base import CamelCaseDto
from novu.dto.base import CamelCaseDto, DtoDescriptor, DtoIterableDescriptor
from novu.enums import EventStatus


Expand All @@ -20,19 +20,50 @@ class EventDto(CamelCaseDto["EventDto"]):
"""Transaction id for trigger."""


@dataclasses.dataclass
class RecipientDto(CamelCaseDto["RecipientDto"]): # pylint: disable=R0902
"""The recipients list of people who will receive the notification."""

subscriber_id: str
"""Subscriber ID of the recipient."""

email: Optional[str] = None
"""Email of the recipient."""

first_name: Optional[str] = None
"""First name of the recipient."""

last_name: Optional[str] = None
"""Last name of the recipient."""

phone: Optional[str] = None
"""Phone number of the recipient."""

avatar: Optional[str] = None
"""Avatar URL of the recipient."""

locale: Optional[str] = None
"""Locale(language and region) of the recipient, ."""

data: Optional[dict] = None
"""Additional data for the recipient."""


@dataclasses.dataclass
class InputEventDto(CamelCaseDto["InputEventDto"]):
"""Definition of an event used as an input"""

name: str
"""The name of the template trigger to activate."""

recipients: Union[str, List[str]]
"""A subscriber ID (or a list of subscriber ID) to reach with this trigger."""

payload: dict
"""A JSON serializable python dict to pass additional custom information."""

recipients: DtoIterableDescriptor[RecipientDto] = DtoIterableDescriptor[RecipientDto](
default_factory=list, item_cls=RecipientDto
)
"""A subscriber ID (or a list of subscriber ID) to reach with this trigger."""

overrides: Optional[dict] = None
"""A JSON serializable python dict used to override provider specific configurations."""

Expand Down
Loading
Loading