diff --git a/pykit/tests/test_db2.py b/pykit/tests/test_db2.py new file mode 100644 index 0000000..fd61cd0 --- /dev/null +++ b/pykit/tests/test_db2.py @@ -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() diff --git a/pykit/transport/db2/Db2Connection.py b/pykit/transport/db2/Db2Connection.py new file mode 100644 index 0000000..37c592f --- /dev/null +++ b/pykit/transport/db2/Db2Connection.py @@ -0,0 +1,61 @@ +import requests +import json +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 + Toolkit object. + """ + + def __init__(self, database, username, password): + + self.connection = dbi.connect(database=database, \ + user=username, password=password) + + 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.") + """ + return json.loads(result[0]) + + def __test_connection(self): + """ + Test that the DB2Sock REST transport exists and works properly + 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() diff --git a/pykit/transport/db2/__init__.py b/pykit/transport/db2/__init__.py new file mode 100644 index 0000000..d9dab84 --- /dev/null +++ b/pykit/transport/db2/__init__.py @@ -0,0 +1,5 @@ +from .Db2Connection import Db2Connection + + +def connect(database='*LOCAL', username=None, password=None): + return Db2Connection(database, username, password)