From c83afeef61dca771bb1565dfb7d3b9542511bce8 Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 2 Nov 2023 15:21:37 -0400 Subject: [PATCH] working workspaces --- posthog/warehouse/external_data_source/client.py | 2 +- .../warehouse/external_data_source/connection.py | 10 +++++----- .../warehouse/external_data_source/destination.py | 4 ++-- posthog/warehouse/external_data_source/source.py | 4 ++-- posthog/warehouse/external_data_source/workspace.py | 13 +++++++------ 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/posthog/warehouse/external_data_source/client.py b/posthog/warehouse/external_data_source/client.py index 947ba368bf802..ef37c92a7c34a 100644 --- a/posthog/warehouse/external_data_source/client.py +++ b/posthog/warehouse/external_data_source/client.py @@ -23,6 +23,6 @@ def send_request(path, method, params=None, payload=None): response_payload = response.json() if not response.ok: - raise ValueError(response_payload["message"]) + raise ValueError(response_payload["detail"]) return response_payload diff --git a/posthog/warehouse/external_data_source/connection.py b/posthog/warehouse/external_data_source/connection.py index e227fe754605a..b941b47335389 100644 --- a/posthog/warehouse/external_data_source/connection.py +++ b/posthog/warehouse/external_data_source/connection.py @@ -21,7 +21,7 @@ def create_connection(source_id: str, destination_id: str) -> ExternalDataConnec "destinationId": destination_id, } - response = send_request(AIRBYTE_CONNECTION_URL, payload=payload) + response = send_request(AIRBYTE_CONNECTION_URL, method="POST", payload=payload) update_connection_stream(response["connectionId"]) @@ -44,22 +44,22 @@ def update_connection_stream(connection_id: str): "namespaceFormat": None, } - send_request(connection_id_url, payload=payload) + send_request(connection_id_url, method="PATCH", payload=payload) def delete_connection(connection_id: str) -> None: - send_request(AIRBYTE_CONNECTION_URL + "/" + connection_id) + send_request(AIRBYTE_CONNECTION_URL + "/" + connection_id, method="DELETE") # Fire and forget def start_sync(connection_id: str): payload = {"jobType": "sync", "connectionId": connection_id} - send_request(AIRBYTE_JOBS_URL, payload=payload) + send_request(AIRBYTE_JOBS_URL, method="POST", payload=payload) def retrieve_sync(connection_id: str): params = {"connectionId": connection_id, "limit": 1} - response = send_request(AIRBYTE_JOBS_URL, params=params) + response = send_request(AIRBYTE_JOBS_URL, method="GET", params=params) data = response.get("data", []) if not data: diff --git a/posthog/warehouse/external_data_source/destination.py b/posthog/warehouse/external_data_source/destination.py index f8ed5c35ae4a6..eb45744df8340 100644 --- a/posthog/warehouse/external_data_source/destination.py +++ b/posthog/warehouse/external_data_source/destination.py @@ -24,7 +24,7 @@ def create_destination(team_id: int, workspace_id: str) -> ExternalDataDestinati "workspaceId": workspace_id, } - response = send_request(AIRBYTE_DESTINATION_URL, payload=payload) + response = send_request(AIRBYTE_DESTINATION_URL, method="POST", payload=payload) return ExternalDataDestination( destination_id=response["destinationId"], @@ -32,4 +32,4 @@ def create_destination(team_id: int, workspace_id: str) -> ExternalDataDestinati def delete_destination(destination_id: str) -> None: - send_request(AIRBYTE_DESTINATION_URL + "/" + destination_id) + send_request(AIRBYTE_DESTINATION_URL + "/" + destination_id, method="DELETE") diff --git a/posthog/warehouse/external_data_source/source.py b/posthog/warehouse/external_data_source/source.py index 5a2ab5cfba4c8..bacd99e812197 100644 --- a/posthog/warehouse/external_data_source/source.py +++ b/posthog/warehouse/external_data_source/source.py @@ -70,7 +70,7 @@ def create_stripe_source(payload: StripeSourcePayload, workspace_id: str) -> Ext def _create_source(payload: Dict) -> ExternalDataSource: - response = send_request(AIRBYTE_SOURCE_URL, payload=payload) + response = send_request(AIRBYTE_SOURCE_URL, method="POST", payload=payload) return ExternalDataSource( source_id=response["sourceId"], name=response["name"], @@ -80,4 +80,4 @@ def _create_source(payload: Dict) -> ExternalDataSource: def delete_source(source_id): - send_request(AIRBYTE_SOURCE_URL + "/" + source_id) + send_request(AIRBYTE_SOURCE_URL + "/" + source_id, method="DELETE") diff --git a/posthog/warehouse/external_data_source/workspace.py b/posthog/warehouse/external_data_source/workspace.py index 5d0d7e84b8bb5..d2eaeea2c00a3 100644 --- a/posthog/warehouse/external_data_source/workspace.py +++ b/posthog/warehouse/external_data_source/workspace.py @@ -1,22 +1,23 @@ from posthog.warehouse.models.external_data_workspace import ExternalDataWorkspace from posthog.warehouse.external_data_source.client import send_request -AIRBYTE_WORKSPACE_URL = "https://api.airbyte.io/api/v1/workspaces" +AIRBYTE_WORKSPACE_URL = "https://api.airbyte.com/v1/workspaces" def create_workspace(team_id: int): - payload = {"name": "Team " + team_id} - response = send_request(AIRBYTE_WORKSPACE_URL, payload=payload) + payload = {"name": "Team " + str(team_id)} + response = send_request(AIRBYTE_WORKSPACE_URL, method="POST", payload=payload) return response["workspaceId"] def get_or_create_workspace(team_id: int): - workspace = ExternalDataWorkspace.objects.get(team_id=team_id) + workspace_exists = ExternalDataWorkspace.objects.filter(team_id=team_id).exists() - if not workspace: + if not workspace_exists: workspace_id = create_workspace(team_id) workspace = ExternalDataWorkspace.objects.create(team_id=team_id, workspace_id=workspace_id) - + else: + workspace = ExternalDataWorkspace.objects.get(team_id=team_id) return workspace