-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor ProtocolBaseHandler and add target_type to Events (#236)
- Loading branch information
1 parent
c2ec60c
commit d90dcd8
Showing
12 changed files
with
177 additions
and
37 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from canvas_generated.messages.events_pb2 import Event, EventResponse, EventType | ||
from canvas_generated.messages.events_pb2 import Event as EventRequest | ||
from canvas_generated.messages.events_pb2 import EventResponse, EventType | ||
|
||
__all__ = ("Event", "EventResponse", "EventType") | ||
from .base import Event | ||
|
||
__all__ = ("EventRequest", "EventResponse", "EventType", "Event") |
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,50 @@ | ||
import dataclasses | ||
import json | ||
from functools import cached_property | ||
from typing import Any | ||
|
||
from django.apps import apps | ||
from django.db import models | ||
|
||
from canvas_generated.messages.events_pb2 import Event as EventRequest | ||
from canvas_generated.messages.events_pb2 import EventType | ||
|
||
|
||
@dataclasses.dataclass | ||
class TargetType: | ||
"""The target of the event.""" | ||
|
||
id: str | ||
type: type[models.Model] | None | ||
|
||
@cached_property | ||
def instance(self) -> models.Model | None: | ||
"""Return the instance of the target.""" | ||
return self.type._default_manager.filter(id=self.id).first() if self.type else None | ||
|
||
|
||
class Event: | ||
"""An event that occurs in the Canvas environment.""" | ||
|
||
name: str | ||
type: EventType | ||
context: dict[str, Any] | ||
target: TargetType | ||
|
||
def __init__(self, event_request: EventRequest) -> None: | ||
try: | ||
target_model = apps.get_model( | ||
app_label="canvas_sdk", model_name=event_request.target_type | ||
) | ||
except LookupError: | ||
target_model = None | ||
|
||
try: | ||
context = json.loads(event_request.context) | ||
except ValueError: | ||
context = {} | ||
|
||
self.type = event_request.type | ||
self.name = EventType.Name(self.type) | ||
self.context = context | ||
self.target = TargetType(id=event_request.target, type=target_model) |
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 |
---|---|---|
@@ -1,29 +1,45 @@ | ||
import json | ||
from typing import TYPE_CHECKING, Any | ||
import importlib.metadata | ||
from typing import Any | ||
|
||
if TYPE_CHECKING: | ||
from canvas_generated.messages.events_pb2 import Event | ||
import deprecation | ||
|
||
from canvas_sdk.events import Event | ||
|
||
version = importlib.metadata.version("canvas") | ||
|
||
|
||
class BaseHandler: | ||
""" | ||
The class that all handlers inherit from. | ||
""" | ||
"""The class that all handlers inherit from.""" | ||
|
||
secrets: dict[str, Any] | ||
target: str | ||
event: Event | ||
|
||
def __init__( | ||
self, | ||
event: "Event", | ||
event: Event, | ||
secrets: dict[str, Any] | None = None, | ||
) -> None: | ||
self.event = event | ||
self.secrets = secrets or {} | ||
|
||
try: | ||
self.context = json.loads(event.context) | ||
except ValueError: | ||
self.context = {} | ||
@property | ||
@deprecation.deprecated( | ||
deprecated_in="0.11.0", | ||
removed_in="1.0.0", | ||
current_version=version, | ||
details="Use 'event.context' directly instead", | ||
) | ||
def context(self) -> dict[str, Any]: | ||
"""The context of the event.""" | ||
return self.event.context | ||
|
||
self.target = event.target | ||
self.secrets = secrets or {} | ||
@property | ||
@deprecation.deprecated( | ||
deprecated_in="0.11.0", | ||
removed_in="1.0.0", | ||
current_version=version, | ||
details="Use 'event.target['id']' directly instead", | ||
) | ||
def target(self) -> str: | ||
"""The target id of the event.""" | ||
return self.event.target.id |
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,7 @@ | ||
# ruff: noqa | ||
# register all models in the app so they can be used with apps.get_model() | ||
from canvas_sdk.v1.data.command import Command | ||
from canvas_sdk.v1.data.condition import Condition | ||
from canvas_sdk.v1.data.note import Note | ||
from canvas_sdk.v1.data.patient import Patient | ||
from canvas_sdk.v1.data.user import CanvasUser |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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