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
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ name = "pypi"
[packages]
slack-sdk = "*"
psycopg2 = "*"
pymssql = "*"
mysql-connector-python = "*"

[dev-packages]
pytest = "*"
Expand Down
113 changes: 112 additions & 1 deletion Pipfile.lock

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

16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# wayscript-python
# wayscript-python


## Integrations

### SQL

To connect to a postgres resource, use the following snippet:
```
import psycopg2
from wayscript.ingegrations import sql

kwargs = sql.get_psycopg2_connection_kwargs(_id)
connection = psycopg2.connect(**kwargs)
```
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ services:
- TWINE_USERNAME=__token__
- TWINE_NON_INTERACTIVE=1
- TWINE_PASSWORD
# env_file:
- WAYSCRIPT_ORIGIN
env_file:
# if you want to test publishing, put your test pypi creds in .env.pypi-test
# - ./.env.pypi-test
- ./env.meierj.local
stdin_open: true
tty: true

8 changes: 8 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from wayscript.integrations.sql import postgres

_id = "c227ed10-1174-4722-a25a-1e5d3f770ad2"
conn = postgres.get_client_for_workspace_integration(_id)

with conn.cursor() as cursor:
cursor.execute("SELECT * FROM public.event;")
print(len([x for x in cursor]))
70 changes: 70 additions & 0 deletions src/wayscript/integrations/sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import json

from wayscript import utils
from wayscript.errors import MissingCredentialsError


PSYCOPG2_CONNECTION_KWARG_MAP = {
"dbname": "database_name",
"user": "database_user",
"password": "database_password",
"host": "database_host",
"port": "database_port",
}


MSSQL_CONNECTION_KWARG_MAP = {
"server": "database_host",
"user": "database_user",
"password": "database_password",
"database": "database_name",
"port": "database_port",

}


MYSQL_CONNECTION_KWARG_MAP = {
"host": "database_host",
"user": "database_user",
"password": "database_password",
"database": "database_name",
"port": "database_port",
}


def get_connection_kwargs(_id: str, credentials_mapping: dict) -> dict:
"""
Return 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 = {k: credentials.get(v) for k, v in credentials_mapping.items()}

if not credentials:
raise MissingCredentialsError(f"Missing credentials for workspace_integration={_id}")

return kwargs

def get_psycopg2_connection_kwargs(_id: str) -> dict:
"""Get connection kwargs for psycopg2"""
return get_connection_kwargs(_id, credentials_mapping=PSYCOPG2_CONNECTION_KWARG_MAP)

def get_mssql_connection_kwargs(_id: str) -> dict:
"""Get connection kwargs for SQL Server connection via mssql driver"""
return get_connection_kwargs(_id, credentials_mapping=MSSQL_CONNECTION_KWARG_MAP)

def get_mysql_connection_kwargs(_id: str) -> dict:
"""Return connection kwargs for MySQL connection via mysql driver"""
return get_connection_kwargs(_id, credentials_mapping=MYSQL_CONNECTION_KWARG_MAP)
Empty file.
44 changes: 0 additions & 44 deletions src/wayscript/integrations/sql/postgres.py

This file was deleted.

31 changes: 0 additions & 31 deletions tests/integrations/sql/test_postgres.py

This file was deleted.

61 changes: 61 additions & 0 deletions tests/integrations/test_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
import responses

from wayscript.integrations import sql


PSYCOPG2_EXPECTED = dict(
dbname="my_db",
user="zach",
password="very-secret-password",
host="host.docker.internal",
port=15432,
)

MSSQL_EXPECTED = {
"database": "my_db",
"password": "very-secret-password",
"port": 15432,
"server": "host.docker.internal",
"user": "zach",
}

MYSQL_EXPECTED = {
"database": "my_db",
"host": "host.docker.internal",
"password": "very-secret-password",
"port": 15432,
"user": "zach",
}


@responses.activate
@pytest.mark.parametrize(
"client_name,expected",
[
("psycopg2", PSYCOPG2_EXPECTED),
("mssql", MSSQL_EXPECTED),
("mysql", MYSQL_EXPECTED),
],
)
def test_get_connection_kwargs(
client_name,
expected,
patch_client_get_url,
workspace_integrations_detail_response_sql,
):
"""Test getting postgres client kwargs"""

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

_id = workspace_integrations_detail_response_sql["id"]

function_name = f"get_{client_name}_connection_kwargs"
function = getattr(sql, function_name)

assert expected == function(_id)