-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Collin Dutter <[email protected]>
- Loading branch information
1 parent
ff008c0
commit 1b1accc
Showing
8 changed files
with
196 additions
and
3 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
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
39 changes: 39 additions & 0 deletions
39
griptape/drivers/event_listener/pusher_event_listener_driver.py
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,39 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from attrs import define, field, Factory | ||
from griptape.drivers import BaseEventListenerDriver | ||
from griptape.utils import import_optional_dependency | ||
|
||
if TYPE_CHECKING: | ||
from pusher import Pusher | ||
|
||
|
||
@define | ||
class PusherEventListenerDriver(BaseEventListenerDriver): | ||
app_id: str = field(kw_only=True) | ||
key: str = field(kw_only=True) | ||
secret: str = field(kw_only=True) | ||
cluster: str = field(kw_only=True) | ||
channel: str = field(kw_only=True) | ||
event_name: str = field(kw_only=True) | ||
pusher_client: Pusher = field( | ||
default=Factory( | ||
lambda self: import_optional_dependency("pusher").Pusher( | ||
app_id=self.app_id, key=self.key, secret=self.secret, cluster=self.cluster, ssl=True | ||
), | ||
takes_self=True, | ||
), | ||
kw_only=True, | ||
) | ||
|
||
def try_publish_event_payload_batch(self, event_payload_batch: list[dict]) -> None: | ||
data = [ | ||
{"channel": self.channel, "name": self.event_name, "data": event_payload} | ||
for event_payload in event_payload_batch | ||
] | ||
|
||
self.pusher_client.trigger_batch(data) | ||
|
||
def try_publish_event_payload(self, event_payload: dict) -> None: | ||
self.pusher_client.trigger(channels=self.channel, event_name=self.event_name, data=event_payload) |
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
42 changes: 42 additions & 0 deletions
42
tests/unit/drivers/event_listener/test_pusher_event_listener_driver.py
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,42 @@ | ||
from pytest import fixture | ||
from tests.mocks.mock_event import MockEvent | ||
from griptape.drivers import PusherEventListenerDriver | ||
from unittest.mock import Mock | ||
|
||
|
||
class TestPusherEventListenerDriver: | ||
@fixture(autouse=True) | ||
def mock_post(self, mocker): | ||
mock_pusher_client = mocker.patch("pusher.Pusher") | ||
mock_pusher_client.return_value.trigger.return_value = Mock() | ||
mock_pusher_client.return_value.trigger_batch.return_value = Mock() | ||
|
||
return mock_pusher_client | ||
|
||
@fixture() | ||
def driver(self): | ||
return PusherEventListenerDriver( | ||
app_id="test-app-id", | ||
key="test-key", | ||
secret="test-secret", | ||
cluster="test-cluster", | ||
channel="test-channel", | ||
event_name="test-event", | ||
) | ||
|
||
def test_init(self, driver): | ||
assert driver | ||
|
||
def test_try_publish_event_payload(self, driver): | ||
data = MockEvent().to_dict() | ||
driver.try_publish_event_payload(data) | ||
|
||
driver.pusher_client.trigger.assert_called_with(channels="test-channel", event_name="test-event", data=data) | ||
|
||
def test_try_publish_event_payload_batch(self, driver): | ||
data = [MockEvent().to_dict() for _ in range(3)] | ||
driver.try_publish_event_payload_batch(data) | ||
|
||
driver.pusher_client.trigger_batch.assert_called_with( | ||
[{"channel": "test-channel", "name": "test-event", "data": data[i]} for i in range(3)] | ||
) |