Share via


Sample application for accessing the APIs for Apps and Games

Sample applications can be created in C#, JAVA, PYTHON languages based on the Swagger code generation.

This article describes how to:

  • Generate the Python client code from Swagger.
  • Use the client-side code to get available datasets.

The sample application doesn't cover all the method of calling APIs for other functionalities. However, the process of calling other APIs remains the same as outlined in this article.

How to run the application

  1. Generate the client. Python is used in this example. Other clients can be generated from the Swagger.

  2. Copy the contents of the OpenAPI spec from the endpoint. Paste the contents into Swagger Editor.

    A screenshot of the Swagger Editor.

  3. Select Generate Client > Python.

  4. A Python client is generated/downloaded in the local system. On extracting, the following autogenerated files are available.

    A screenshot of the auto-generated files.

  5. Create a file sample.py within the extracted folder. In this file, we call the generated Python code to invoke the APIs using the following code.

    Provide the token value within the token variable

    from __future__ import print_function
    import time
    import swagger_client
    from swagger_client.rest import ApiException
    from pprint import pprint
    
    # Configure API authorization
    token='<provide bearer token>'
    # create an instance of the API class
    api_instance = swagger_client.ScheduledDatasetApi(swagger_client.ApiClient())
    api_instance.api_client.set_default_header("Authorization", "Bearer "+token)
    dataset_name = 'Wishlist' # str |  (optional)
    try:
        # Get available datasets and it's information such as columns, aggregations, pre defined dateranges etc
        api_response = api_instance.insights_v11_scheduled_dataset_get(dataset_name=dataset_name)
        pprint(api_response)
    except ApiException as e:
        print("Exception when calling ScheduledDatasetApi->insights_v11_scheduled_dataset_get: %s\n" % e)
    
  6. Install the dependencies.

    Use the following commands to install dependencies and setup:

    pip install -U pip setuptools
    python setup.py install
    
  7. Run the code.

    python sample.py
    

    The following response is generated from the code.

    A screenshot of the response generated from the sample code.

    Similarly, other APIs can be invoked from the code as shown in this example.