Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# local configs
env.*.local

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = "pypi"

[packages]
slack-sdk = "*"
psycopg2 = "*"

[dev-packages]
pytest = "*"
Expand Down
47 changes: 31 additions & 16 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
44 changes: 44 additions & 0 deletions src/wayscript/integrations/sql/postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import json

import psycopg2

from wayscript import utils
from wayscript.errors import MissingCredentialsError


def get_connection_kwargs(_id: str) -> dict:
"""
Return postgres connection kwargs

If you want to instantiate your own client, use this method.
"""
wayscript_client = utils.WayScriptClient()
response = wayscript_client.get_workspace_integration_detail(_id)
response.raise_for_status()
workspace_integration_data = response.json()
credentials_str = workspace_integration_data.get("credentials")
credentials = {}

try:
credentials = json.loads(credentials_str)
except json.decoder.JSONDecodeError:
credentials = {}

kwargs = {
"dbname": credentials.get("database_name"),
"user": credentials.get("database_user"),
"password": credentials.get("database_password"),
"host": credentials.get("database_host"),
"port": credentials.get("database_port", 5432),
}

if not credentials or not all(v for v in kwargs.values()):
raise MissingCredentialsError(f"Missing credentials for workspace_integration={_id}")

return kwargs


def get_client_for_workspace_integration(_id: str):
"""Instantiate connection from workspace integration kwargs"""
kwargs = get_connection_kwargs(_id)
return psycopg2.connect(**kwargs)
4 changes: 3 additions & 1 deletion src/wayscript/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class WayScriptClient:
def __init__(self, *args, **kwargs):
"""Init a wayscript client"""
self.session = requests.Session()
self.session.headers["authorization"] = get_process_execution_user_token()
access_token = get_process_execution_user_token()
self.session.headers["authorization"] = f"Bearer {access_token}"
self.session.headers["content-type"] = "application/json"

def _get_url(self, subpath: str, route: str, template_args: dict=None):
"""Generate an url"""
Expand Down
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,30 @@ def workspace_integrations_detail_response():
})
}
return data

@pytest.fixture
def workspace_integration_sql_credentials():
"""Data for sql credentials"""
data = {
"database_name": "my_db",
"database_user": "zach",
"database_port": 15432,
"database_password": "very-secret-password",
"database_host": "host.docker.internal"
}
return data


@pytest.fixture
def workspace_integrations_detail_response_sql(workspace_integration_sql_credentials):
"""
Data from GET /workspaces-integrations/<id>

For type=sql
"""
data = {
"id": WORKSPACE_INTEGRATION_ID,
"type": "sql",
"credentials": json.dumps(workspace_integration_sql_credentials),
}
return data
31 changes: 31 additions & 0 deletions tests/integrations/sql/test_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest import mock

import psycopg2
import responses

from wayscript.integrations.sql import postgres


@responses.activate
def test_get_postgres_client_for_workspace_integration(
monkeypatch,
patch_client_get_url,
workspace_integration_sql_credentials,
workspace_integrations_detail_response_sql,
):
"""Test getting postgres client kwargs"""
connection = "my-connection"
monkeypatch.setattr(psycopg2, "connect", mock.Mock(return_value=connection))

responses.add(
responses.GET,
patch_client_get_url,
json=workspace_integrations_detail_response_sql,
status=200,
)

_id = workspace_integrations_detail_response_sql["id"]

assert connection == postgres.get_client_for_workspace_integration(_id)

psycopg2.connect.assert_called_with(dbname='my_db', user='zach', password='very-secret-password', host='host.docker.internal', port=15432)