Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions engine_communicator.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
from websocket import create_connection
import jwt
from jwt import JWT, jwk_from_pem
import ssl

import os
from datetime import datetime, timedelta

class EngineCommunicator:

def __init__(self, url):
def __init__(self, url, debug=False):
self.url = url
self.ws = create_connection(self.url)
self.session = self.ws.recv() # Holds session object. Required for Qlik Sense Sept. 2017 and later
if debug:
print ("> " + self.session)

@staticmethod
def send_call(self, call_msg):
if debug:
print ("> " + call_msg)
self.ws.send(call_msg)
return self.ws.recv()

# Qlik sometimes returns more than one json response for a single request -- perhaps should use an async socket.
# Might be needed to append all responses to returned data in a future review
while True:
data = self.ws.recv()
if debug:
print("< " + data)
if ('result' in data or 'error' in data):
break
return data

@staticmethod
def close_qvengine_connection(self):
self.ws.close()

class SecureEngineCommunicator(EngineCommunicator):

def __init__(self, senseHost, proxyPrefix, userDirectory, userId, privateKeyPath, ignoreCertErrors=False):
def __init__(self, senseHost, proxyPrefix, userDirectory, userId, privateKeyPath, userGroup=None, ignoreCertErrors=False, rootCA=None, ):
self.url = "wss://" + senseHost + "/" + proxyPrefix + "/app/engineData"
sslOpts = {}
if ignoreCertErrors:
sslOpts = {"cert_reqs": ssl.CERT_NONE}
else:
if rootCA is not None:
sslOpts = {'ca_certs': rootCA}
else:
sslOpts = None

privateKey = open(privateKeyPath).read()
token = jwt.encode({'user': userId, 'directory': userDirectory}, privateKey, algorithm='RS256')
payload = {'user': userId, 'directory': userDirectory}
if userGroup is not None:
payload['group'] = userGroup

self.ws = create_connection(self.url, sslopt=sslOpts, header=['Authorization: BEARER ' + str(token)])
self.session = self.ws.recv()
privateKey = jwk_from_pem(open(privateKeyPath,"rb").read())
token=JWT().encode(key=privateKey, alg='RS256',
payload=payload,
optional_headers={'exp': (datetime.utcnow() + timedelta(minutes=10)).isoformat()} )

self.ws = create_connection(self.url, sslopt=sslOpts, header=['authorization: bearer ' + str(token)])
self.session = self.ws.recv()
11 changes: 11 additions & 0 deletions engine_field_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ class EngineFieldApi:
def __init__(self, socket):
self.engine_socket = socket

def select_match(self, fld_handle, value=None):
if value is None:
value = []
msg = json.dumps({"jsonrpc": "2.0", "id": 0, "handle": fld_handle, "method": "Select",
"params": [value, False]})
response = json.loads(self.engine_socket.send_call(self.engine_socket, msg))
try:
return response
except KeyError:
return response["error"]

def select_values(self, fld_handle, values=None):
if values is None:
values = []
Expand Down
26 changes: 26 additions & 0 deletions engine_generic_object_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ def get_layout(self, handle):
except KeyError:
return response["error"]

def get_properties(self, handle):
msg = json.dumps({"jsonrpc": "2.0", "id": 0, "handle": handle, "method": "GetProperties", "params": []})
response = json.loads(self.engine_socket.send_call(self.engine_socket, msg))
try:
return response["result"]
except KeyError:
return response["error"]

def get_hypercube_data(self, handle, path="/qHyperCubeDef", pages=[]):
msg = json.dumps({"jsonrpc": "2.0", "id": 0, "handle": handle, "method": "GetHyperCubeData",
"params": [path,pages]})
Expand All @@ -23,6 +31,15 @@ def get_hypercube_data(self, handle, path="/qHyperCubeDef", pages=[]):
except KeyError:
return response["error"]

def get_hypercube_pivot_data(self, handle, path="/qHyperCubeDef", pages=[]):
msg = json.dumps({"jsonrpc": "2.0", "id": 0, "handle": handle, "method": "GetHyperCubePivotData",
"params": [path,pages]})
response = json.loads(self.engine_socket.send_call(self.engine_socket, msg))
try:
return response["result"]
except KeyError:
return response["error"]

def get_list_object_data(self, handle, path="/qListObjectDef", pages=[]):
msg = json.dumps({"jsonrpc": "2.0", "id": 0, "handle": handle, "method": "GetListObjectData",
"params": [path, pages]})
Expand All @@ -31,3 +48,12 @@ def get_list_object_data(self, handle, path="/qListObjectDef", pages=[]):
return response["result"]
except KeyError:
return response["error"]

def expand_left(self, handle, path="/qHyperCubeDef", row=0, col=0, all=True):
msg = json.dumps({"jsonrpc": "2.0", "id": 0, "handle": handle, "method": "ExpandLeft",
"params": [path, row, col, all]})
response = json.loads(self.engine_socket.send_call(self.engine_socket, msg))
try:
return response["result"]
except KeyError:
return response["error"]
9 changes: 5 additions & 4 deletions pyqlikengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
class QixEngine:

def __init__(self, url, is_secure=False, proxy_prefix='', user_directory='', user_id='', private_key_path='',
ignore_cert_errors=False):
ignore_cert_errors=False, root_ca=None, debug=None):
self.url = url
if is_secure:
self.conn = engine_communicator.SecureEngineCommunicator(url, proxy_prefix, user_directory,user_id,
private_key_path, ignore_cert_errors)
self.conn = engine_communicator.SecureEngineCommunicator(senseHost=url, proxyPrefix=proxy_prefix, userDirectory=user_directory,
userId=user_id, privateKeyPath=private_key_path, ignoreCertErrors=ignore_cert_errors,
rootCA=root_ca, debug=debug)
else:
self.conn = engine_communicator.EngineCommunicator(url)
self.conn = engine_communicator.EngineCommunicator(url, debug=debug)
self.ega = engine_global_api.EngineGlobalApi(self.conn)
self.eaa = engine_app_api.EngineAppApi(self.conn)
self.egoa = engine_generic_object_api.EngineGenericObjectApi(self.conn)
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
wsaccel
websocket-client
jwt
pandas
pprint