-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature db2 transport #6
base: master
Are you sure you want to change the base?
Changes from all commits
71e5309
e845c15
b9c44b6
7ba21c1
268fd71
1d7ae73
7bde14b
0c242c7
04707db
d21cb53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import unittest | ||
import os | ||
from pykit.transport.db2 import connect | ||
|
||
|
||
class TestDb2Connection(unittest.TestCase): | ||
def setUp(self): | ||
self.connection = connect() | ||
|
||
def test_execute_payload(self): | ||
payload = { | ||
'pgm': [ | ||
{'name': 'HELLO', 'lib': 'DB2JSON'}, | ||
{'s': {'name': 'char', 'type': '128a', 'value': 'Hi there'}} | ||
] | ||
} | ||
toolkit = self.connection.toolkit() | ||
toolkit.add(payload) | ||
response = toolkit.execute() | ||
|
||
|
||
self.assertTrue(response) | ||
if __name__ == '__main__': | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import requests | ||
import json | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused imports |
||
from ...Toolkit import Toolkit | ||
import ibm_db_dbi as dbi | ||
|
||
|
||
|
||
|
||
class Db2Connection: | ||
""" | ||
Represents a REST HTTP connection that can be used to get the Python | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. REST HTTP -> Database |
||
Toolkit object. | ||
""" | ||
|
||
def __init__(self, database, username, password): | ||
|
||
self.connection = dbi.connect(database=database, \ | ||
user=username, password=password) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible to just wrap def __init__(self, *args, **kwargs):
self.connection = dbi.connect(args, kwargs) https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3 This would allow users to pass any connection options they want, though is that something we actually want them to be able to do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, probably. Thoughts on having db, username, and password explicit (for comprehension), and adding **kwargs on the end? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you can have as many named parameters as you like followed by Does that make sense? |
||
|
||
def toolkit(self): | ||
""" | ||
Return an instance of the toolkit with a connection defined. | ||
|
||
:return: Toolkit | ||
""" | ||
return Toolkit(self) | ||
|
||
def execute(self, payload): | ||
""" | ||
Execute the payload and then clear the payload. | ||
|
||
:return: dict | ||
""" | ||
payload_string = str(payload) | ||
|
||
cur = self.connection.cursor() | ||
cur.callproc('DB2JSON.DB2PROCJR', (payload_string,)) | ||
|
||
result = cur.fetchone() | ||
|
||
""" | ||
if not response.ok: | ||
raise TransportError("There was an error while executing the payload.") | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Residual REST code |
||
return json.loads(result[0]) | ||
|
||
def __test_connection(self): | ||
""" | ||
Test that the DB2Sock REST transport exists and works properly | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/REST/DB2/g |
||
with the given configuration. Raise an error if not. | ||
|
||
:return: void | ||
""" | ||
toolkit = self.toolkit() | ||
toolkit.add({ | ||
'pgm': [ | ||
{'name': 'HELLO', 'lib': 'DB2JSON'}, | ||
{'s': {'name': 'char', 'type': '128a', 'value': 'Hi there'}} | ||
] | ||
}) | ||
response = toolkit.execute() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No assertion? Looks like the test would never fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had a _test_connection funciton in the HTTP transport, because the HTTP transport didn |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .Db2Connection import Db2Connection | ||
|
||
|
||
def connect(database='*LOCAL', username=None, password=None): | ||
return Db2Connection(database, username, password) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import