Skip to content

Commit

Permalink
working workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
EDsCODE committed Nov 2, 2023
1 parent 81a233e commit c83afee
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion posthog/warehouse/external_data_source/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions posthog/warehouse/external_data_source/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions posthog/warehouse/external_data_source/destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ 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"],
)


def delete_destination(destination_id: str) -> None:
send_request(AIRBYTE_DESTINATION_URL + "/" + destination_id)
send_request(AIRBYTE_DESTINATION_URL + "/" + destination_id, method="DELETE")
4 changes: 2 additions & 2 deletions posthog/warehouse/external_data_source/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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")
13 changes: 7 additions & 6 deletions posthog/warehouse/external_data_source/workspace.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c83afee

Please sign in to comment.