Skip to content

Commit

Permalink
Pull cloud url from env var
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Apr 23, 2024
1 parent f16f421 commit 2ce0d9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@

@define
class GriptapeCloudEventListenerDriver(BaseEventListenerDriver):
base_url: str = field(default="https://cloud.griptape.ai", kw_only=True)
"""Driver for publishing events to Griptape Cloud.
Attributes:
base_url: The base URL of Griptape Cloud. Defaults to the GT_CLOUD_BASE_URL environment variable.
api_key: The API key to authenticate with Griptape Cloud.
headers: The headers to use when making requests to Griptape Cloud. Defaults to include the Authorization header.
run_id: The ID of the Structure Run to publish events to. Defaults to the GT_CLOUD_RUN_ID environment variable.
"""

base_url: str = field(
default=Factory(lambda: os.getenv("GT_CLOUD_BASE_URL", "https://cloud.griptape.ai")), kw_only=True
)
api_key: str = field(kw_only=True)
headers: dict = field(
default=Factory(lambda self: {"Authorization": f"Bearer {self.api_key}"}, takes_self=True), kw_only=True
Expand All @@ -28,5 +39,6 @@ def validate_run_id(self, _, run_id: str):

def try_publish_event(self, event: BaseEvent) -> None:
url = urljoin(self.base_url.strip("/"), f"/api/structure-runs/{self.run_id}/events")
print(url)

requests.post(url=url, json=event.to_dict(), headers=self.headers)
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
from unittest.mock import Mock
from pytest import fixture

import pytest
from tests.mocks.mock_event import MockEvent
from pytest import fixture

from griptape.drivers.event_listener.griptape_cloud_event_listener_driver import GriptapeCloudEventListenerDriver
from tests.mocks.mock_event import MockEvent


class TestGriptapeCloudEventListenerDriver:
Expand All @@ -17,17 +20,21 @@ def mock_post(self, mocker):

@fixture()
def driver(self):
return GriptapeCloudEventListenerDriver(api_key="foo bar", run_id="baz")
os.environ["GT_CLOUD_BASE_URL"] = "https://cloud123.griptape.ai"

return GriptapeCloudEventListenerDriver(api_key="foo bar", run_id="bar baz")

def test_init(self, driver):
assert driver
assert driver.api_key == "foo bar"
assert driver.run_id == "bar baz"

def test_try_publish_event(self, mock_post, driver):
event = MockEvent()
driver.try_publish_event(event=event)

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

0 comments on commit 2ce0d9e

Please sign in to comment.