4
4
import requests
5
5
from ibm_watson import AssistantV2
6
6
from ibm_cloud_sdk_core .authenticators import IAMAuthenticator
7
+ import base64
7
8
try :
8
9
import ibm_db
9
10
except :
22
23
with open ('ibm-db2-credentials.json' , 'r' ) as credentialsFile :
23
24
credentials1 = json .loads (credentialsFile .read ())
24
25
25
- dsn_driver = "IBM DB2 ODBC DRIVER"
26
- dsn_database = credentials1 ['db' ]
27
- dsn_hostname = credentials1 ['host' ]
28
- dsn_port = "50000"
29
- dsn_uid = credentials1 ['username' ]
30
- dsn_pwd = credentials1 ['password' ]
31
-
32
- dsn = (
33
- "DRIVER={{IBM DB2 ODBC DRIVER}};"
34
- "DATABASE={0};"
35
- "HOSTNAME={1};"
36
- "PORT={2};"
37
- "PROTOCOL=TCPIP;"
38
- "UID={3};"
39
- "PWD={4};" ).format (dsn_database , dsn_hostname , dsn_port , dsn_uid , dsn_pwd )
40
26
try :
41
- conn = ibm_db .connect (dsn , "" , "" )
27
+ dsn_database = credentials1 ['connection' ]['db2' ]['database' ]
28
+ dsn_hostname = credentials1 ['connection' ]['db2' ]['hosts' ][0 ]['hostname' ]
29
+ dsn_port = credentials1 ['connection' ]['db2' ]['hosts' ][0 ]['port' ]
30
+ dsn_uid = credentials1 ['connection' ]['db2' ]['authentication' ]['username' ]
31
+ dsn_pwd = credentials1 ['connection' ]['db2' ]['authentication' ]['password' ]
32
+
33
+ certificate = credentials1 ['connection' ]['db2' ]['certificate' ]['certificate_base64' ]
34
+ ssl_certificate_bytes = base64 .b64decode (certificate )
35
+ ssl_certificate = ssl_certificate_bytes .decode ('ascii' )
42
36
except :
43
- pass
37
+ print ('No DB2 credentials found' )
38
+
39
+ db2_conn_cert = os .path .join (os .path .expanduser ('~' ),'ibm-db2-ssl.cert' )
40
+ with open (db2_conn_cert , "w" ) as f :
41
+ f .write (ssl_certificate )
42
+
43
+ dsn = 'DATABASE={db};HOSTNAME={host};PORT={port};PROTOCOL=TCPIP;UID={uid};PWD={pwd};SECURITY=SSL;SSLServerCertificate={cert}' .format (
44
+ db = dsn_database ,
45
+ host = dsn_hostname ,
46
+ port = dsn_port ,
47
+ uid = dsn_uid ,
48
+ pwd = dsn_pwd ,
49
+ cert = db2_conn_cert
50
+ )
51
+
52
+ try :
53
+ conn = ibm_db .connect (dsn , "" , "" )
54
+ except Exception as e :
55
+ print (e )
44
56
45
57
#########################
46
58
# Create Orders Table
52
64
ADDRESS varchar(255) ); '
53
65
try :
54
66
ibm_db .exec_immediate (conn , table )
55
- except :
56
- pass
67
+ except Exception as e :
68
+ print ( "Create Table error-> " , e )
57
69
58
70
59
71
with open ('watson-assistant-credentials.json' , 'r' ) as credentialsFile :
@@ -93,7 +105,7 @@ def destroySession():
93
105
assistant_id = assistantid , session_id = sessionid ).get_result ()
94
106
print (response )
95
107
except Exception as e :
96
- pass
108
+ print ( e )
97
109
98
110
# assistant.delete_session(skillid, "<YOUR SESSION ID>").get_result()
99
111
@@ -234,3 +246,5 @@ def index():
234
246
if __name__ == "__main__" :
235
247
app .secret_key = os .urandom (12 )
236
248
app .run (debug = True , host = '0.0.0.0' , port = port )
249
+
250
+
0 commit comments