I am trying to list all object in a qlik sense application using Qlik engine API and Python. I am facing the issues. please help me out. code and issues as follows
import websocket
import json
import ssl
import pandas as pd
# WebSocket connection URL, replace with your Qlik server's address
qlik_server = "ws://localhost:4848/app/"
header = {
"X-Qlik-User": "UserDirectory=Internal;UserId=sa_repository" # Replace with correct Qlik user details
}
variable_list = [] # List to store variable details
app_handle = None # Handle for the app after opening it
session_object_handle = None # Handle for the session object
# WebSocket connection settings
def on_message(ws, message😞
global app_handle, session_object_handle, variable_list
response = json.loads(message)
# Print the full response to inspect its structure
print("Full response from Qlik API:\n", json.dumps(response, indent=4))
if 'result' in response:
# Step 1: Handle OpenDoc response to get the app handle
if 'qHandle' in response['result']:
app_handle = response['result']['qHandle'] # Get the app handle
print(f"App handle obtained: {app_handle}")
# Step 2: Now create the session object to list variables using the app handle
create_session_object_payload = {
"jsonrpc": "2.0",
"id": 3,
"method": "CreateSessionObject",
"handle": app_handle, # Use the app handle here
"params": [{
"qInfo": {
"qType": "VariableList"
},
"qVariableListDef": {
"qType": "variable",
"qShowReserved": True,
"qShowConfig": True,
"qData": {
"tags": "/tags"
}
}
}]
}
ws.send(json.dumps(create_session_object_payload))
# Step 3: Handle CreateSessionObject response to get the session object handle
elif 'qHandle' in response['result'] and 'qLayout' not in response['result']:
session_object_handle = response['result']['qHandle'] # Get the session object handle
print(f"Session object handle obtained: {session_object_handle}")
# Step 4: Now call GetLayout to fetch the variable list using the session object handle
get_layout_payload = {
"jsonrpc": "2.0",
"id": 4,
"method": "GetLayout",
"handle": session_object_handle, # Use the session object handle here
"params": []
}
ws.send(json.dumps(get_layout_payload))
# Step 5: Handle the GetLayout response to extract the variable list
elif 'qLayout' in response['result']:
variables = response['result']['qLayout']['qVariableList']['qItems']
for var in variables:
variable_list.append({
"Variable Name": var['qName'],
"Definition": var.get('qDefinition', 'N/A'),
"Tags": var.get('qTags', 'N/A')
})
# Display the variable list in a table using pandas
df = pd.DataFrame(variable_list)
print("\nList of Variables:")
print(df.to_string(index=False)) # Display without index
ws.close() # Close WebSocket after listing variables
def on_error(ws, error😞
print(f"Error: {error}")
def on_close(ws, close_status_code, close_msg😞
print("### Closed connection ###")
def on_open(ws😞
# Step 1: Open the app to get a valid app handle
open_doc_payload = {
"jsonrpc": "2.0",
"id": 2,
"method": "OpenDoc",
"handle": -1, # No handle needed for OpenDoc
"params": {
"qDocName": r"C:\Users\soms\Documents\Qlik\Sense\Apps\01_Extract_Sales.qvf" # Replace with your app's full path
}
}
ws.send(json.dumps(open_doc_payload))
# Establish WebSocket connection
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp(
qlik_server,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
header=header
)
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) # Handle self-signed certificates
Error--
-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
upgrade: websocket
connection: Upgrade
sec-websocket-accept: 5l4eYHrBR7WaqBwrWe2WXCT9B7w=
-----------------------
Websocket connected
++Sent raw: b'\x81\xfe\x00\xee~\xdd\xe5\xff\x05\xff\x8f\x8c\x11\xb3\x97\x8f\x1d\xff\xdf\xdf\\\xef\xcb\xcf\\\xf1\xc5\xdd\x17\xb9\xc7\xc5^\xee\xc9\xdf\\\xb0\x80\x8b\x16\xb2\x81\xddD\xfd\xc7\xbc\x0c\xb8\x84\x8b\x1b\x8e\x80\x8c\r\xb4\x8a\x911\xbf\x8f\x9a\x1d\xa9\xc7\xd3^\xff\x8d\x9e\x10\xb9\x89\x9a\\\xe7\xc5\xceR\xfd\xc7\x8f\x1f\xaf\x84\x92\r\xff\xdf\xdf%\xa6\xc7\x8e7\xb3\x83\x90\\\xe7\xc5\x84\\\xac\xb1\x86\x0e\xb8\xc7\xc5^\xff\xb3\x9e\x0c\xb4\x84\x9d\x12\xb8\xa9\x96\r\xa9\xc7\x82R\xfd\xc7\x8e(\xbc\x97\x96\x1f\xbf\x89\x9a2\xb4\x96\x8b:\xb8\x83\xddD\xfd\x9e\xdd\x0f\x89\x9c\x8f\x1b\xff\xdf\xdf\\\xab\x84\x8d\x17\xbc\x87\x93\x1b\xff\xc9\xdf\\\xac\xb6\x97\x11\xaa\xb7\x9a\r\xb8\x97\x89\x1b\xb9\xc7\xc5^\xa9\x97\x8a\x1b\xf1\xc5\xdd\x0f\x8e\x8d\x90\t\x9e\x8a\x91\x18\xb4\x82\xddD\xfd\x91\x8d\x0b\xb8\xc9\xdf\\\xac\xa1\x9e\n\xbc\xc7\xc5^\xa6\xc7\x8b\x1f\xba\x96\xddD\xfd\xc7\xd0\n\xbc\x82\x8c\\\xa0\x98\x82#\xa0'
++Sent decoded: fin=1 opcode=1 data=b'{"jsonrpc": "2.0", "id": 3, "method": "CreateSessionObject", "handle": 1, "params": [{"qInfo": {"qType": "VariableList"}, "qVariableListDef": {"qType": "variable", "qShowReserved": true, "qShowConfig": true, "qData": {"tags": "/tags"}}}]}'
++Rcv raw: b'\x81U{"jsonrpc":"2.0","method":"OnConnected","params":{"qSessionState":"SESSION_CREATED"}}'
++Rcv decoded: fin=1 opcode=1 data=b'{"jsonrpc":"2.0","method":"OnConnected","params":{"qSessionState":"SESSION_CREATED"}}'
Full response from Qlik API:
{
"jsonrpc": "2.0",
"method": "OnConnected",
"params": {
"qSessionState": "SESSION_CREATED"
}
}
++Rcv raw: b'\x81h{"jsonrpc":"2.0","id":3,"error":{"code":-32602,"parameter":"Invalid handle","message":"Invalid Params"}}'
++Rcv decoded: fin=1 opcode=1 data=b'{"jsonrpc":"2.0","id":3,"error":{"code":-32602,"parameter":"Invalid handle","message":"Invalid Params"}}'
Full response from Qlik API:
{
"jsonrpc": "2.0",
"id": 3,
"error": {
"code": -32602,
"parameter": "Invalid handle",
"message": "Invalid Params"
}
}