import requests
import json
Simple Python implementation of V2.0 REST API
Get started🔗
We’ll use the files from the tse-api-tutorial GitHub repository that you downloaded at the beginning of the tutorial.
-
Open your IDE
Visual Studio Code will be used in all images and instructions. The files for this lesson areapi_training_python_1_begin.pyandapi_training_python_1_end.py -
Open up your command line or terminal environment as well.
|
Note
|
There are many ways to install and configure Python on a system. This tutorial shows "plain" versions of all commands as if you are using the main system-wide install of Python for the tutorial. Please adjust all Python commands according to your own environment. |
01 - Imports and variables🔗
At the top of api_training_python_1_begin.py, there is a set of imports and variables that configure the overall script.
Import Requests library🔗
To issue HTTP commands in a given programming language, you must use a library that sends and receives HTTP.
For this lesson, we’ll use Requests, the most commonly used high-level HTTP library for Python.
The file starts with import commands for the json standard Python package and the requests package, which must be installed.
Install requests package🔗
If you followed the prerequisites and installed the thoughtspot_rest_api_v1 package using your command line or terminal:
pip install thoughtspot_rest_api_v1
The requests package should have automatically been installed along with the other requirements.
You can double-check the installation or force a version upgrade using the following command:
pip install requests --upgrade
Set global variables🔗
It is very convenient to declare global variables at the beginning of a script so that they can be reused throughout.
The URL of the ThoughtSpot instance is always necessary to send a REST API command, and if the instance has Orgs enabled, you need to send the org_id when you request a login token. We’ll set those as global variables along with the api_version (which hasn’t changed ever at this point).
import requests
import json
thoughtspot_url = 'https://{}.thoughtspot.cloud'
org_id = 1613534286
api_version = '2.0'
Now, let’s construct the starting portion of any API endpoint URL and define the most basic headers that will be used by every call:
...
base_url = '{thoughtspot_url}/api/rest/{version}/'.format(thoughtspot_url=thoughtspot_url, version=api_version)
api_headers = {
'X-Requested-By': 'ThoughtSpot',
'Accept': 'application/json'
}
02 - Use a Session object🔗
Rather than set the full configuration of each HTTP request you make, you can construct a Session object from the requests library, which an open HTTP connection and maintains settings like headers and cookies between individual HTTP actions.
You send HTTP commands by calling the .get(), .post(), .put() and .delete() methods of the Session object.
All of those methods will return a Response object, which you assign to a variable to do further processing. This will look something the following:
# Create a new Session object
requests_session = requests.Session()
# Set the headers for all uses of the requests_session object
requests_session.headers.update(api_headers)
# Define the JSON message, in Python object syntax (close but not exactly JSON)
json_post_data = { // a request body }
# Set the URL of the endpoint
url = base_url + "{api_endpoint_ending}"
# Issue the HTTP request and store the response to a variable
resp = requests_session.post(url=url, json=json_post_data)