Advertisement
Guest User

Python script for Qlik Engine JSON API

a guest
Dec 23rd, 2020
588
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import requests
  2. import websocket, ssl
  3. import json,  csv
  4. import os, time
  5. from pprint import pprint
  6.  
  7. #Connecting to the server. The lines below will be replaced by the certificates and headers for enterprise usage later
  8. ws = websocket.WebSocket()
  9. ws.connect("ws://localhost:4848/app/")
  10.  
  11. #For getting the doc (app) list
  12. doclist_req = {
  13.     "handle": -1,
  14.     "method": "GetDocList",
  15.     "params": [],
  16.     "outKey": -1,
  17.     "id": 1
  18. }
  19.  
  20. ws.send(json.dumps(doclist_req))
  21. result = ws.recv()
  22. ws.send(json.dumps(doclist_req))
  23. result = ws.recv()
  24. result_json = json.loads(result)
  25. print(result_json)
  26. print()
  27.  
  28. #For opening the doc (app)
  29. app_req = {
  30.     "jsonrpc": "2.0",
  31.     "method": "OpenDoc",
  32.     "handle": -1,
  33.     "params": [
  34.         #Can iterate if multiple apps are there
  35.         #Since only one app was present we used the index 0
  36.         result_json['result']['qDocList'][0]['qDocId']
  37.     ],
  38.     "outKey": -1,
  39.     "id": 2
  40. }
  41.  
  42. #The first call seems to be for establishing the connection and second to actually send the request body
  43. ws.send(json.dumps(app_req))
  44. result = ws.recv()
  45. ws.send(json.dumps(app_req))
  46. result = ws.recv()
  47. result_json = json.loads(result)
  48. print(result_json)
  49. print()
  50. app_req_handle = result_json['result']['qReturn']['qHandle']
  51.  
  52. #For creating the session object necessary for fetching dimensions, fields, etc.
  53. session_req = {
  54.     "jsonrpc": "2.0",
  55.     "method": "CreateSessionObject",
  56.     "handle": app_req_handle,
  57.     "params": [
  58.         {
  59.             "qInfo": {
  60.                 "qType": "SheetList"
  61.             },
  62.             "qAppObjectListDef": {
  63.                 "qType": "sheet",
  64.                 "qData": {
  65.                     "title": "/qMetaDef/title",
  66.                     "description": "/qMetaDef/description",
  67.                     "thumbnail": "/thumbnail",
  68.                     "cells": "/cells",
  69.                     "rank": "/rank",
  70.                     "columns": "/columns",
  71.                     "rows": "/rows"
  72.                 }
  73.             }
  74.         }
  75.     ],
  76.     "outKey": -1,
  77.     "id": 3
  78. }
  79.  
  80. ws.send(json.dumps(session_req))
  81. result = ws.recv()
  82. ws.send(json.dumps(session_req))
  83. result = ws.recv()
  84. result_json = json.loads(result)
  85. print(result_json)
  86. print()
  87. session_req_handle = result_json['result']['qReturn']['qHandle']
  88.  
  89. #For fetching the layout of the sheets
  90. layout_req = {
  91.     "jsonrpc": "2.0",
  92.     "method": "GetLayout",
  93.     "handle": session_req_handle,
  94.     "params": [],
  95.     "outKey": -1,
  96.     "id": 4
  97. }
  98.  
  99. ws.send(json.dumps(layout_req))
  100. result = ws.recv()
  101. ws.send(json.dumps(layout_req))
  102. result = ws.recv()
  103. result_json = json.loads(result)
  104. print(result_json)
  105. print()
  106.  
  107. #Since only one sheet was present we used the index 0
  108. list_of_charts = result_json['result']['qLayout']['qAppObjectList']['qItems'][0]['qData']['cells']
  109. for chart in list_of_charts:
  110.     print(chart['name'])
  111. print()
  112.  
  113. ws.close()
  114.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement