-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_training_python_1_final.py
57 lines (49 loc) · 1.94 KB
/
api_training_python_1_final.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import requests
import json
thoughtspot_url = 'https://yourinstance.thoughtspot.cloud'
org_id = 1613534286
api_version = '2.0'
base_url = '{thoughtspot_url}/api/rest/{version}/'.format(thoughtspot_url=thoughtspot_url, version=api_version)
# Add additional headers for specific request if necessary
api_headers = {
'X-Requested-By': 'ThoughtSpot',
'Accept': 'application/json'
}
requests_session = requests.Session()
# Set the headers for all uses of the requests_session object
requests_session.headers.update(api_headers)
# url is base_url + endpoint
endpoint = "auth/token/full"
url = base_url + endpoint
# JSON request as Python Dict
json_post_data = {
"username": "yourusername",
"password": "y0urP@ssword",
"validity_time_in_sec": 3600,
"org_id": org_id,
"auto_create": False # make sure to uppercase in Python
}
try:
# requests returns back Response object with various properties and methods
resp = requests_session.post(url=url, json=json_post_data)
# This method causes Python Exception to throw if the response status is not 2XX
resp.raise_for_status()
# Retrieve the JSON body of response and convert into Python Dict
# If a call (like a DELETE) returns 204 instead of 200, with no body, this may cause error
resp_json = resp.json()
# You can just print(resp_json) to see the Python Dict
print(json.dumps(resp_json, indent=2))
# 'token' property of the response is the Bearer Token to use
token = resp_json["token"]
# Update the api_headers from before with a new header for the Bearer token
api_headers['Authorization'] = 'Bearer {}'.format(token)
requests_session.headers.update(api_headers)
# Now we can use the requests_session object to issue any other command we'd like
except requests.exceptions.HTTPError as e:
print("Requests")
print(e)
print(e.request)
print(e.request.url)
print(e.request.headers)
print(e.request.body)
print(e.response.content)