-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(changes): add Change DTO definitions and wrappers
- Loading branch information
Showing
9 changed files
with
948 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
This module is used to define the ``ChangeApi``, a python wrapper to interact with ``Changes`` in Novu. | ||
""" | ||
from typing import Dict, Generator, List, Optional, Union | ||
|
||
from novu.api.base import Api | ||
from novu.constants import CHANGES_ENDPOINT | ||
from novu.dto.change import ChangeDto, PaginatedChangeDto | ||
|
||
|
||
class ChangeApi(Api): | ||
"""This class aims to handle all API methods around changes in API""" | ||
|
||
def __init__(self, url: Optional[str] = None, api_key: Optional[str] = None) -> None: | ||
super().__init__(url, api_key) | ||
|
||
self._change_url = f"{self._url}{CHANGES_ENDPOINT}" | ||
|
||
def list(self, page: Optional[int] = None, limit: Optional[int] = None) -> PaginatedChangeDto: | ||
"""List existing changes | ||
Args: | ||
page: Page to retrieve. Defaults to None. | ||
limit: Size of the page to retrieve. Defaults to None. | ||
Returns: | ||
Paginated list of change | ||
""" | ||
payload: Dict[str, Union[int, str]] = {} | ||
if page: | ||
payload["page"] = page | ||
if limit: | ||
payload["limit"] = limit | ||
|
||
return PaginatedChangeDto.from_camel_case(self.handle_request("GET", f"{self._change_url}", payload=payload)) | ||
|
||
def count(self) -> int: | ||
"""Get the number of changes | ||
Returns: | ||
Number of changes | ||
""" | ||
return self.handle_request("GET", f"{self._change_url}/count")["data"] | ||
|
||
def apply(self, change_id: str) -> PaginatedChangeDto: | ||
"""Apply one change using its ID | ||
Args: | ||
change_ids: A change ID to apply. | ||
Returns: | ||
Paginated changes | ||
""" | ||
return PaginatedChangeDto.from_camel_case(self.handle_request("POST", f"{self._change_url}/{change_id}/apply")) | ||
|
||
def bulk_apply(self, change_ids: List[str]) -> Generator[ChangeDto, None, None]: | ||
"""Apply a list of changes using their IDs | ||
Args: | ||
change_ids: The list of IDs to apply | ||
Yields: | ||
Parsed Change from results list | ||
""" | ||
results = self.handle_request("POST", f"{self._change_url}/bulk/apply", {"changeIds": change_ids})["data"] | ||
for result in results: | ||
for sub_result in result: | ||
yield ChangeDto.from_camel_case(sub_result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""This module is used to gather all DTO definitions related to the Change resource in Novu""" | ||
import dataclasses | ||
from typing import Any, List, Optional | ||
|
||
from novu.dto.base import CamelCaseDto, DtoIterableDescriptor | ||
from novu.enums import ChangeKind | ||
|
||
|
||
@dataclasses.dataclass | ||
class ChangeDetailDto(CamelCaseDto["ChangeDetailDto"]): | ||
"""Definition of a change detail""" | ||
|
||
op: Optional[str] = None # pylint: disable=C0103 | ||
"""The operation code of the change""" | ||
|
||
path: Optional[List[str]] = None | ||
"""The path of the modification""" | ||
|
||
val: Optional[Any] = None | ||
"""Value of the change, struct depend of the context""" | ||
|
||
|
||
@dataclasses.dataclass | ||
class ChangeDto(CamelCaseDto["ChangeDto"]): # pylint: disable=R0902 | ||
"""Definition of a change""" | ||
|
||
_id: Optional[str] = None | ||
"""Change ID in Novu internal storage system""" | ||
|
||
_environment_id: Optional[str] = None | ||
"""Environment ID in Novu internal storage system""" | ||
|
||
_organization_id: Optional[str] = None | ||
"""Organization ID in Novu internal storage system""" | ||
|
||
_entity_id: Optional[str] = None | ||
"""Entity ID in Novu internal storage system""" | ||
|
||
_parent_id: Optional[str] = None | ||
"""Parent ID in Novu internal storage system""" | ||
|
||
enabled: Optional[bool] = None | ||
"""If the change was enabled.""" | ||
|
||
type: Optional[ChangeKind] = None | ||
"""Kind of change applied.""" | ||
|
||
change: DtoIterableDescriptor[ChangeDetailDto] = DtoIterableDescriptor[ChangeDetailDto]( | ||
default_factory=list, item_cls=ChangeDetailDto | ||
) | ||
"""Details about a change""" | ||
|
||
created_at: Optional[str] = None | ||
"""Date-time of the integration initial configuration""" | ||
|
||
updated_at: Optional[str] = None | ||
"""Date-time of the last update of the integration""" | ||
|
||
|
||
@dataclasses.dataclass | ||
class PaginatedChangeDto(CamelCaseDto["PaginatedChangeDto"]): | ||
"""Definition of paginated changes""" | ||
|
||
page: int = 0 | ||
total_count: int = 0 | ||
page_size: int = 0 | ||
data: DtoIterableDescriptor[ChangeDto] = DtoIterableDescriptor[ChangeDto](default_factory=list, item_cls=ChangeDto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""This module is used to gather enumerations related to the Change resource in Novu""" | ||
from enum import Enum | ||
|
||
|
||
class ChangeKind(Enum): | ||
"""This enumeration define all kinds of change in Novu""" | ||
|
||
FEED = "Feed" | ||
MESSAGE_TEMPLATE = "MessageTemplate" | ||
LAYOUT = "Layout" | ||
NOTIFICATION_TEMPLATE = "NotificationTemplate" | ||
NOTIFICATION_GROUP = "NotificationGroup" |
Oops, something went wrong.