-
Notifications
You must be signed in to change notification settings - Fork 14
Support Executing SQL #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
meierj-wayscript
merged 6 commits into
wayscript:master
from
meierj-wayscript:gen-294-execute-sql-query-via-integration
Aug 17, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12f5587
Add fixtures for pg connection
meierj-wayscript 0adf4ea
Test that we return a postgres client
meierj-wayscript 9527cc3
Support instantiating postgres connection
meierj-wayscript ea44a27
Install psycopg2
meierj-wayscript 3820413
Ignore local configs
meierj-wayscript ffdb3ae
Properly configure bearer token
meierj-wayscript File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ name = "pypi" | |
|
|
||
| [packages] | ||
| slack-sdk = "*" | ||
| psycopg2 = "*" | ||
|
|
||
| [dev-packages] | ||
| pytest = "*" | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
meierj-wayscript marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.