Skip to content

Commit

Permalink
Update GriptapeCloudEventListenerDriver (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
vachillo authored Aug 29, 2024
1 parent c1ee9f6 commit 10c0170
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,21 @@ def publish_event(self, event: BaseEvent | dict, *, flush: bool = False) -> None
super().publish_event(event_payload, flush=flush)

def try_publish_event_payload(self, event_payload: dict) -> None:
url = urljoin(self.base_url.strip("/"), f"/api/structure-runs/{self.structure_run_id}/events")

response = requests.post(url=url, json=event_payload, headers=self.headers)
response.raise_for_status()
self._post_event(self._get_event_request(event_payload))

def try_publish_event_payload_batch(self, event_payload_batch: list[dict]) -> None:
url = urljoin(self.base_url.strip("/"), f"/api/structure-runs/{self.structure_run_id}/events")

response = requests.post(url=url, json=event_payload_batch, headers=self.headers)
response.raise_for_status()
self._post_event([self._get_event_request(event_payload) for event_payload in event_payload_batch])

def _get_event_request(self, event_payload: dict) -> dict:
return {
"payload": event_payload,
"timestamp": event_payload["timestamp"],
"type": event_payload["type"],
}

def _post_event(self, json: list[dict] | dict) -> None:
requests.post(
url=urljoin(self.base_url.strip("/"), f"/api/structure-runs/{self.structure_run_id}/events"),
json=json,
headers=self.headers,
).raise_for_status()
2 changes: 1 addition & 1 deletion tests/mocks/mock_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class MockEvent(BaseEvent):
def to_dict(self) -> dict:
return {"timestamp": self.timestamp, "id": self.id}
return {"timestamp": self.timestamp, "id": self.id, "meta": self.meta, "type": self.__class__.__name__}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_publish_event_without_span_id(self, mock_post, driver):

mock_post.assert_called_with(
url="https://cloud123.griptape.ai/api/structure-runs/bar baz/events",
json=[event.to_dict()],
json=[driver._get_event_request(event.to_dict())],
headers={"Authorization": "Bearer foo bar"},
)

Expand All @@ -63,7 +63,7 @@ def test_publish_event_with_span_id(self, mock_post, driver):

mock_post.assert_called_with(
url="https://cloud123.griptape.ai/api/structure-runs/bar baz/events",
json=[{**event.to_dict(), "span_id": "test"}],
json=[driver._get_event_request({**event.to_dict(), "span_id": "test"})],
headers={"Authorization": "Bearer foo bar"},
)

Expand All @@ -73,7 +73,7 @@ def test_try_publish_event_payload(self, mock_post, driver):

mock_post.assert_called_once_with(
url="https://cloud123.griptape.ai/api/structure-runs/bar baz/events",
json=event.to_dict(),
json=driver._get_event_request(event.to_dict()),
headers={"Authorization": "Bearer foo bar"},
)

Expand All @@ -84,6 +84,6 @@ def try_publish_event_payload_batch(self, mock_post, driver):

mock_post.assert_called_with(
url="https://cloud123.griptape.ai/api/structure-runs/bar baz/events",
json=event.to_dict(),
json=driver._get_event_request(event.to_dict()),
headers={"Authorization": "Bearer foo bar"},
)

0 comments on commit 10c0170

Please sign in to comment.