Cognos Analytics

 View Only
Expand all | Collapse all

Python Cognos Analytics with REST API

  • 1.  Python Cognos Analytics with REST API

    Posted Tue September 26, 2023 03:15 PM

    Hey Community and Experts,

    I would like to generate Cognos reports using Python. Is this possible at all? I could not find any documentation regd. running of reports programatically. There is Cognos Documentation regarding REST APIs but I could not find anything related to running of Reports.

    Here is what my request is:

    Does the Cognos API allow us to specify the following:

    1. which report to run
    2. with the appropriate parameters for example - customer, time range, etc...

    I want to use Python to achieve this.

    NOTE: I have used parameterized URLs that will return customized urls. But I want to achieve this via REST APIs and python. Any help appreciated. 

    #Cognos Analytics with Watson



    ------------------------------
    SRIHARI CHANDRAMOULI
    ------------------------------


  • 2.  RE: Python Cognos Analytics with REST API

    IBM Champion
    Posted Wed September 27, 2023 04:43 AM

    Hi Srihari,

    Yes the "new Cognos REST API"  (i.e. https://www.ibm.com/docs/en/cognos-analytics/11.1.0?topic=apis-rest-api ) is mostly focused on automating the administrative operations of CA (e.g. managing users/groups/roles, UI customization like extensions and themes, Content Manager content, datasources/modules, etc.) 

    Probably more suited to what you are trying to accomplish is the Cognos Mashup Service ( https://www.ibm.com/docs/en/cognos-analytics/11.0.0?topic=guide-developing-mashup-service-applications-using-rest-interface ) - and specifically the REST API option within CMS. With it, you can login into Cognos, run reports (including specifying the parameters for prompted reports). As well if you want the resulting report output returned (as opposed to just run and saved in the CA portal) you can specify the format you want the report results returned in (e.g. image, HTML, HTML fragment, XML, JSON. Or you can ask for the report in one of the report viewer formats (e.g. PDF, Excel, etc.). Unlike the  automation of administrative tasks role of the "new REST API", Cognos Mashup service is more a web service to your reports themselves.

    Note: if you are looking to actually programmatically consume the results of the report in Python (e.g. pandas), Cognos has created a python module, CADataConnector,  to allow you to work with (create and read)  CA data resources directly in python, without using reports. CADataConnector is not published by itself, but is included in the IBM Cognos Analytics for Jupyter Notebook Server and in Watson Studio Notebook environment .



    ------------------------------
    Jim Boland

    LinkedIn: https://www.linkedin.com/in/jimboland
    Website: https://coreinsightz.com
    Email: jimboland@coreinsightz.com
    ------------------------------



  • 3.  RE: Python Cognos Analytics with REST API

    Posted Wed September 27, 2023 03:48 PM

    Thanks a lot Jim. I was able use the REST API option within CMS. However, I want PDF as the format of the returned output. And it looks like "fmt" does not give me an option to export it as PDF
    https://www.ibm.com/docs/en/cognos-analytics/11.2.0?topic=options-fmt

    So far,

    • I was able to use the mashup API to authenticate using auth/logon
    • I was able to construct the URL as follows which returned an XML structure when I pasted it in my browser window. Here is the URL:
      https://<my-server>/ibmcognos/bi/v1/disp/rds/reportData/report/iD9493B2B788C40B7BFD20C92888EE745?fmt=PDF&p_client_id=<selectChoices><selectOption useValue="client_id" displayValue="client_name"/></selectChoices>
    • fmt=HTML works but things look out of place
    • I guess I will find out a way to export PDF via REST API?

      Any help appreciated


    ------------------------------
    SRIHARI CHANDRAMOULI
    ------------------------------



  • 4.  RE: Python Cognos Analytics with REST API

    IBM Champion
    Posted Thu September 28, 2023 09:04 AM
    Edited by Jim Boland Thu September 28, 2023 09:57 PM

    Hi Srihari,

    Yes, CMS has two different - somewhat  overlapping - RDS resources:

    • the reportData resource that you tried has the following formats (i.e. "fmt=") supported :LDX (XML), Simple (XML), HTML, HTML fragment, JSON, DataSet XML/ATOM/JSON, and Image. These formats are NOT the report server formats you can choose in the CA portal. (Instead, they are formats built to facilitate embedding/"mashing-up" report content into another application/environment. (e.g. PDF is NOT one of the fmt supported for this call.)
    • the outputFormat resource, on the other hand, runs/returns the report output in one of the formats you can generate in the CA portal (e.g. CSV, MHT, PDF, spreadsheetML, XLWA, XML, xlsxData). This is the REST call, I believe you want to use, for the scenario you described  (more details here).

    You can specify the specific report, and the prompt parameters in the same way for either of these call (outputFormat or reportData).

    E.g. your call probably looks something like:

    https://<my-server>/ibmcognos/bi/v1/disp/rds/outputFormat/report/iD9493B2B788C40B7BFD20C92888EE745/PDF?p_client_id=22



    ------------------------------
    Jim Boland

    LinkedIn: https://www.linkedin.com/in/jimboland
    Website: https://coreinsightz.com
    Email: jimboland@coreinsightz.com
    ------------------------------



  • 5.  RE: Python Cognos Analytics with REST API

    Posted Thu September 28, 2023 11:33 AM

    Thanks a lot Jim. The outputFormat resource worked out well. I am able to get the report in a PDF version. However there seems to be some error regarding the passing of my parameters. The returned PDF Report did not have any values. 


    This is my original URL which does not use mashup and works fine:
    https://my-server/ibmcognos/bi/?pathRef=my-report-path&action=run&format=PDF&prompt=false&promptParameters=%5B%7B%22name%22%3A%22client_id%22%2C%22value%22%3A%5B%7B%22use%22%3A%22<client-id>%22%2C%22display%22%3A%22<client-name>%22%7D%5D%7D%5D

    And here is the mashed up url:

    https://<my-server>/ibmcognos/bi/v1/disp/rds/outputFormat/report/iD9493B2B788C40B7BFD20C92888EE745/PDF&p_client_id=<selectChoices><selectOption useValue="client_id" displayValue="client_name"/></selectChoices>


    I was not able to find anything about formatting parameters. But if you could give me some insights, it would be great. Thank you for helping me out.



    ------------------------------
    SRIHARI CHANDRAMOULI
    ------------------------------



  • 6.  RE: Python Cognos Analytics with REST API

    IBM Champion
    Posted Thu September 28, 2023 10:29 PM
    Edited by Jim Boland Thu September 28, 2023 10:31 PM

    Hi Srihari,

    Sorry,  that was my fault - I had just copy/pasted the prompt stuff incorrectly in the example in  my reply above. I've edited/fixed up the example in that comment, now.

    For CMS, you have two options for how you specify the prompt parameters

    1. long-form - add an xmlData query parameter to your url, containing a <promptAnswers> xml element. Details/example here
    2. shorthand - add a p_<prompt_parameter_name>=<prompt_value> query parameter for each prompt. Details of how you can specify more complicated things in the values (e.g. ranges) are here

    Here's an example (that I tested before posting this time! 😎):

    https://us3.ca.analytics.ibm.com/bi/v1/disp/rds/outputFormat/report/i15E936FAE97A43AE88385CADB5B7807E/PDF?p_product=Earl%20Grey&p_type=Black%20tea

    This example runs the report with storeID i15E936FAE97A43AE88385CADB5B7807E, with the prompt answers:

    • product set to "Earl Grey"
    • type set to "Black tea"

    and returns a PDF format of the resultant report output.



    ------------------------------
    Jim Boland

    LinkedIn: https://www.linkedin.com/in/jimboland
    Website: https://coreinsightz.com
    Email: jimboland@coreinsightz.com
    ------------------------------



  • 7.  RE: Python Cognos Analytics with REST API

    Posted Fri September 29, 2023 10:35 AM

    Thanks a lot Jim, the mashup url worked perfectly. I was able to run the report in a PDF Format. I was able to get the conversation ID in this format:
    https://my-server/ibmcognos/bi/v1/disp/rds/sessionOutput/conversationID/iDF7DE2EB6B7045DF9038D3159F9CE3F6

    Now, i want to retrieve/download the report using REST APIs. I guess we would need to use the conversationID right for this purpose?



    ------------------------------
    SRIHARI CHANDRAMOULI
    ------------------------------



  • 8.  RE: Python Cognos Analytics with REST API

    IBM Champion
    Posted Fri September 29, 2023 11:09 AM

    Yes...ish.

    You have two options when making request to CMS - you can have them run synchronously, or asynchronously.

    Details on the async options are here.

    The easiest to use, is to specify synchronous, by adding an async=off query parameter to your request. Example:

    https://us3.ca.analytics.ibm.com/bi/v1/disp/rds/outputFormat/report/i15E936FAE97A43AE88385CADB5B7807E/PDF?p_product=Earl%20Grey&p_type=Black%20tea&async=off 

    Running synchronously, will cause your request to block waiting for the report to complete execution, then return the result (as a response to this initial request).

    You can also run it asynchronously. If you add async=MANUAL , instead of waiting for (and returning) the report output, it will immediately return an XML response that looks like this:

    <rds:receipt>
    <rds:status>working</rds:status>
    <rds:url>
    ../../../../rds/sessionOutput/conversationID/iE9F2D01E5EE643ABAD08BB13BB13AA2C
    </rds:url>
    <rds:conversationID>iE9F2D01E5EE643ABAD08BB13BB13AA2C</rds:conversationID>
    </rds:receipt>

    You can then poll the url returned (e.g. https://us3.ca.analytics.ibm.com/bi/v1/disp/rds/sessionOutput/conversationID/iE9F2D01E5EE643ABAD08BB13BB13AA2C )  which will return another similar response if the report is still running, or return the report output, if it is done.

    A third option is async=auto which is asynchronous as well, but returns a redirect http response code (and the redirect url). If your browser or http client respects the redirect, then it will automatically do the polling for you.



    ------------------------------
    Jim Boland

    LinkedIn: https://www.linkedin.com/in/jimboland
    Website: https://coreinsightz.com
    Email: jimboland@coreinsightz.com
    ------------------------------



  • 9.  RE: Python Cognos Analytics with REST API

    Posted Fri September 29, 2023 12:27 PM

    Hello Jim, thanks a lot for the above information. I will try these out.




    ------------------------------
    SRIHARI CHANDRAMOULI
    ------------------------------



  • 10.  RE: Python Cognos Analytics with REST API

    Posted Fri September 29, 2023 02:54 PM
    Edited by Aram Basil Sun December 03, 2023 05:57 AM

    Yes, it is possible to generate Cognos reports using Python by leveraging the Cognos REST API. While the official Cognos documentation may not provide explicit details on running reports programmatically, you can achieve this by making HTTP requests to the Cognos REST API endpoints. Here's how you can approach it:

    1. Authentication: Before you can interact with the Cognos REST API, you need to authenticate. You can use basic authentication or a token-based approach depending on your Cognos setup. Basic authentication involves providing a username and password, while token-based authentication often requires an initial login to obtain a token for subsequent API requests.

    2. Listing Reports: You can use the API to list the available reports. The API endpoint for listing reports may vary depending on your Cognos version and setup. This will allow you to retrieve a list of report IDs or paths.

    3. Running a Report: To run a report, you need to send an HTTP POST request to the appropriate API endpoint with the report ID or path and the required parameters as part of the request body. The parameters would typically be passed as JSON data.

    4. Retrieving Report Output: After running the report, you can use the API to retrieve the report output in your desired format (e.g., PDF, CSV, Excel). You will typically need to specify the output format in the request.

    Here's a high-level example of how you can use Python to achieve this using the requests library:

    python
    import requests # Replace with your Cognos API endpoint and credentials cognos_base_url = "https://your-cognos-server/api" username = "your-username" password = "your-password" # Authenticate and get a token (if using token-based authentication) # This step might vary depending on your Cognos setup # You may need to use session management or obtain a token via another means # List available reports reports_url = f"{cognos_base_url}/v1/reports" response = requests.get(reports_url, auth=(username, password)) if response.status_code == 200: reports = response.json() # Choose the report you want to run (find its ID or path) # Define report parameters report_id = "your-report-id" parameters = { "parameter1": "value1", "parameter2": "value2" # Add more parameters as needed } # Run the report run_url = f"{cognos_base_url}/v1/reports/{report_id}/runs" run_data = { "parameters": parameters, "outputFormat": "pdf" # Specify the desired output format } response = requests.post(run_url, json=run_data, auth=(username, password)) if response.status_code == 201: # Report has been successfully run; you can now retrieve the output report_run_info = response.json() report_output_url = report_run_info["output"]["url"] # Retrieve the report output (PDF in this case) report_output = requests.get(report_output_url, auth=(username, password)) # Process the report output as needed else: print("Failed to run the report.") else: print("Failed to list reports.") # Handle errors and exceptions as necessary

    Please note that the exact endpoints and API structure may vary based on your Cognos version and configuration. You should consult your Cognos documentation and possibly the API documentation provided by IBM for your specific version to ensure compatibility and accuracy.

    ILT20 2024



    ------------------------------
    Aram Basil
    ------------------------------



  • 11.  RE: Python Cognos Analytics with REST API

    Posted Sun November 05, 2023 08:26 PM

    Yes, it is possible to generate Cognos reports using Python by leveraging the Cognos REST API. While the official Cognos documentation may not provide explicit details on running reports programmatically, you can achieve this by making HTTP requests to the Cognos REST API endpoints. Here's how you can approach it:

    1. Authentication: Before you can interact with the Cognos REST API, you need to authenticate. You can use basic authentication or a token-based approach depending on your Cognos setup. Basic authentication involves providing a username and password, while token-based authentication often requires an initial login to obtain a token for subsequent API requests.

    2. Listing Reports: You can use the API to list the available reports. The API endpoint for listing reports may vary depending on your Cognos version and setup. This will allow you to retrieve a list of report IDs or paths.

    3. Running a Report: To run a report, you need to send an HTTP POST request to the appropriate API endpoint with the report ID or path and the required parameters as part of the request body. The parameters would typically be passed as JSON data.

    4. Retrieving Report Output: After running the report, you can use the API to retrieve the report output in your desired format (e.g., PDF, CSV, Excel). You will typically need to specify the output format in the request.

    Here's a high-level example of how you can use Python to achieve this using the requests library:

    python
    import requests # Replace with your Cognos API endpoint and credentials cognos_base_url = "https://your-cognos-server/api" username = "your-username" password = "your-password" # Authenticate and get a token (if using token-based authentication) # This step might vary depending on your Cognos setup # You may need to use session management or obtain a token via another means # List available reports reports_url = f"{cognos_base_url}/v1/reports" response = requests.get(reports_url, auth=(username, password)) if response.status_code == 200: reports = response.json() # Choose the report you want to run (find its ID or path) # Define report parameters report_id = "your-report-id" parameters = { "parameter1": "value1", "parameter2": "value2" # Add more parameters as needed } # Run the report run_url = f"{cognos_base_url}/v1/reports/{report_id}/runs" run_data = { "parameters": parameters, "outputFormat": "pdf" # Specify the desired output format } response = requests.post(run_url, json=run_data, auth=(username, password)) if response.status_code == 201: # Report has been successfully run; you can now retrieve the output report_run_info = response.json() report_output_url = report_run_info["output"]["url"] # Retrieve the report output (PDF in this case) report_output = requests.get(report_output_url, auth=(username, password)) # Process the report output as needed else: print("Failed to run the report.") else: print("Failed to list reports.") # Handle errors and exceptions as necessary

    Please note that the exact endpoints and API structure may vary based on your Cognos version and configuration. You should consult your Cognos documentation and possibly the API documentation provided by IBM for your specific version to ensure compatibility and accuracy.

    ------------------------------
    Aram Basil
    ------------------------------



  • 12.  RE: Python Cognos Analytics with REST API

    IBM Champion
    Posted Mon November 06, 2023 02:25 PM
    Edited by Ralf Roeber Mon November 06, 2023 02:28 PM

    Hi Cognos Lovers,

    a little bit off topic - not python :-)  ... but we have implemented the same using JavaScript (Angular) for super fast retrieval into the browsers.

    See this Repo - https://github.com/AMVARA-CONSULTING/dashboard_bap - for details.

    And here the live example: https://amvara-consulting.github.io/dashboard_bap.github.io/#/activity

    Will be presenting the same at IBM TXC EMEA in BCN with example JS code.

    Let me know any questions. Happy to help.

    Best regards,

    Ralf



    ------------------------------
    Ralf Roeber
    https://linkedin.com/in/ralf-roeber/
    ------------------------------