Inex/inexConnect.py

53 lines
2.0 KiB
Python
Raw Normal View History

2024-07-11 16:13:45 -05:00
def connectDatabase(self, lib, driver, server, database, user, password):
"""Connects to the database. Requires a windows driver to do so.
Typically there is one installed by default"""
connectionString = f'DRIVER={{{driver}}};SERVER={server};DATABASE={database};UID={user};PWD={password};TrustServerCertificate=yes'
2024-07-11 16:13:45 -05:00
if self.useLog:
self.il.debug(f"Connection String: connectionString")
self.il.info(f"Connecting to {database}@{server} with driver[{driver}].")
try:
connection = lib.connect(connectionString)
except lib.Error as ex:
sqlstate = ex.args[1]
if self.useLog:
self.il.error(sqlstate)
if self.useLog:
self.il.debug(f"Connected.")
cursor = connection.cursor()
2024-07-11 16:13:45 -05:00
return cursor
2024-07-15 22:32:42 -05:00
def databaseQuery(self, cursor, query, args=()):
2024-07-11 16:13:45 -05:00
if self.useLog:
self.il.debug(f"Query:")
self.il.debug(query)
self.il.info(f"Sending query:{query[0:20]}...")
try:
cur = cursor.execute(query, args)
except cur.Error as ex:
sqlstate = ex.args[1]
if self.useLog:
self.il.error(sqlstate)
if self.useLog:
self.il.debug(f"Processing database response...")
2024-06-24 17:46:00 -05:00
r = [dict((cur.description[i][0], value) \
for i, value in enumerate(row)) for row in cur.fetchall()]
2024-07-11 16:13:45 -05:00
2024-06-24 17:46:00 -05:00
cur.connection.close()
2024-07-11 16:13:45 -05:00
if self.useLog:
2024-07-15 22:32:42 -05:00
self.il.debug(f"Database connection closed")
# return (r[0] if r else None) if one else r
2024-07-25 22:22:08 -05:00
return r
2024-07-27 18:24:15 -05:00
def getToken(reqObj,idpUrl, id, secret):
getTokenResponse = reqObj.post(idpUrl, headers={"client_id": id,"client_secret": secret})
return getTokenResponse["access_token"]
2024-07-25 22:22:08 -05:00
2024-07-27 18:24:15 -05:00
def pushPayload(reqObj, host, token, tenant_id, payload):
url = f'{host}/api/v1/unity/data/{tenant_id}/machine_event'
pushPayloadResponse = reqObj.post(url, headers={'Authorization': f'bearer {token}'},\
payload=payload)
2024-07-25 22:22:08 -05:00
return pushPayloadResponse.status_code