4 Replies Latest reply: Aug 8, 2017 10:16 AM by Cristian Hainic RSS

    Qlik Engine API - how to make requests? [Python / Twisted]

    Cristian Hainic

      Hi everyone,

       

      I've managed perfectly fine to interact with Qlik Engine via the Engine API Explorer. However, I cannot figure out how to make Engine API calls programmatically. I am using Python with the Twisted library for interacting with websocket servers.

       

      Say I want to do a simple request, e.g. CreateApp. Upon establishing a connection via a Twisted protocol, I am trying to send the following request by that protocol's transport.write() method:

       

      TEST_LOAD = {

        "handle": -1,
        "method": "CreateApp",
        "params": {

        "qAppName": "Test-App",
        "qLocalizedScriptMainSection": ""
        },
        "jsonrpc": "2.0",
        "id": 5
      }

       

      class QlikClient(Protocol):

       

        def connectionMade(self):

        self.transport.write(bytes(json.dumps(TEST_LOAD), encoding='utf-8'))

        print('Sent ', json.dumps(TEST_LOAD), 'to the server.')

       

        def dataReceived(self, data):

        print("Server said:", data)

        self.transport.loseConnection()

       

      The problem is that the Qlik server does not respond. So I am not sure if this is the correct way of sending Qlik Engine requests. The Twisted protocol is built and the connection seems to be established, however I have no reply from the server.

       

      Could anyone please suggest how I could make a proper request to the Qlik Engine API?

       

      Please note that I am using client certificates copies stored locally for authentication.

       

      Thank you,

       

      Chris

        • Re: Qlik Engine API - how to make requests? [Python / Twisted]
          Ariel Eidelstein

          Hello Cristian,

           

          Have you managed to solve this issue?

           

          Thanks,

          Ariel

            • Re: Qlik Engine API - how to make requests? [Python / Twisted]
              Cristian Hainic

              Hi Ariel. I quit using Twisted for this. I found that creating apps works fine with Python's aiohttp module and its websocket capabilities.

                • Re: Qlik Engine API - how to make requests? [Python / Twisted]
                  Ariel Eidelstein

                  Hi Cristian,

                   

                  Does aiohttp accept https connections?

                  I also kindly ask you if you can share any script for authenticate to Qlik Sense Server or a page where I can look at.

                   

                  Thanks!

                  Ariel

                    • Re: Qlik Engine API - how to make requests? [Python / Twisted]
                      Cristian Hainic

                      Hi Ariel.

                       

                      Aiohttp also handles HTTPS connections, yes, but here we are addressing the Qlik Engine API, which is a websocket server. So I needed to build a websocket client. There is no code out there on the internet that can walk you through this, unfortunately, especially with Python. But you can read about connecting to the Engine API here: Connecting to the Qlik Engine API ‒ Qlik Sense . Personally I am using Qlik Sense Enterprise and connecting directly using certificates. Below is the Python process I am using.

                       

                      So what I did for authentication was to use a certificate file (e.g., "client.pem") and a certificate key file (e.g., "key_client.pem"), which my company had received from Qlik. I loaded these two files into the ssl context certification chain (using Python's ssl module) and then created a connection using aiohttp's TCPConnector, passing the ssl context as an argument. Then I created an aiohttp.ClientSession using the aforementioned connection as a connector. Finally, I called the ws_connect() method of the ClientSession, passing as an argument a websocket address formatted as follows: wss://<host>:<port>/app

                       

                      And the connection was finally made. I could then call the various Engine API methods. I cannot release any code from the project I am working on, but this is the general process for establishing a connection. I found Qlik APIs to be particularly user-UNfriendly, so the best you can do is arm yourself with patience and work it out piece by piece. As you can see, this question was asked in March and I'm still having trouble figuring out how to use the Engine API to its full capabilities.