Inex/inexConnect.py

62 lines
2.4 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")
2024-07-25 22:22:08 -05:00
return r
2024-07-29 14:04:35 -05:00
class fortraEFC:
def __init__(self):
if self.os.path.exists(self.tokenFilepath):
with open(self.tokenFilepath, 'rb') as t:
self.token = self.j.load(t)
print(self.token["access_token"])
2024-07-25 22:22:08 -05:00
2024-07-29 14:04:35 -05:00
def saveToken(self):
with open(self.tokenFilepath, "w") as f:
self.j.dump(self.tokenData, f, indent = 2)
def getToken(self):
self.tokenData = self.r.post(self.platformConfig["idp"], headers={"client_id": self.platformConfig["client_id"],"client_secret": self.platformConfig["secret"]})
def pushPayload(self):
url = f'{self.host}/api/v1/unity/data/{self.tenant_id}/machine_event'
pushPayloadResponse = self.r.post(self.platformConfig["efc_url"], headers={'Authorization': f'bearer {self.token["access_token"]}'},\
payload=self.modifiedData)
return pushPayloadResponse.status_code