-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample1.py
More file actions
110 lines (85 loc) · 2.89 KB
/
Example1.py
File metadata and controls
110 lines (85 loc) · 2.89 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Example1.py
# Resource Owner Flow
# written and tested in Python 3.6.0
# last updated 07/07/17
import getpass
import requests
import json
# ---------------- #
# Global Variables #
# ---------------- #
# replace tenant with your own
url_root = 'https://tenant.tap.thinksmart.com/prod/'
# fill in (must be strings)
client_id = ''
client_secret = ''
workflow_name = ''
# enter username and password in command line during runtime
username = input("Please enter your TAP username: ")
password = getpass.getpass("Enter your password: ")
# --------- #
# Functions #
# --------- #
def getToken(url_root, username, password, client_id, client_secret):
"""
Given: URL root, user credentials, and client info.
Return: Access token, valid for 1 hour.
"""
# construct URL
url = '{}auth/identity/connect/token'.format(url_root)
# static header, see docs.thinksmart1.apiary.io for documentation
headers = {'Content-Type' : 'application/x-www-form-urlencoded'}
# create dict for body
body = {'grant_type' : 'password',
'scope' : 'api',
'redirect_uri' : 'testuri',
'username' : username,
'password' : password,
'client_id' : client_id,
'client_secret' : client_secret}
# make API call
r = requests.post(url, headers=headers, data=body)
# parse POST call response, return token
return json.loads(r.text).get('access_token')
def getTemplateID(url_root, workflow_name, token):
"""
Given: URL root, name of workflow, and valid token.
Return: Template ID of workflow.
"""
# construct URL
# workflow_name must be quoted, e.g. ?$filter=WorkflowName eq 'Test'
url = ("{}api/v1/templates/dashboard?$filter=WorkflowName eq '{}'"
.format(url_root, workflow_name))
# needs token
headers = {'Authorization' : 'Bearer {}'.format(token)}
# make API call
r = requests.get(url, headers=headers)
# parse GET call response, return template ID
return json.loads(r.text).get('Items')[0].get('ID')
def initiateWorkflow(url_root, template_id, token, body):
"""
Given: URL root, ID of workflow, valid token, and field names and values.
Return: None, makes POST call to initiate workflow.
"""
# construct URL
url = '{}api/v1/workflows/{}/form'.format(url_root, template_id)
# needs token
headers = {'Authorization' : 'Bearer {}'.format(token),
'Content-Type' : 'application/json'}
# encode body into JSON
json_body = json.dumps(body)
# make API call
requests.post(url, headers=headers, data=json_body)
# -------------- #
# Function Calls #
# -------------- #
# get token
token = getToken(url_root, username, password, client_id, client_secret)
# get template ID
template_id = getTemplateID(url_root, workflow_name, token)
# a dictionary is used to fill fields in the workflow
# keys are field names, values are field values
# below is an example of what it may look like
body = {"element1": "Hello", "element2": "World"}
# initiate workflow
initiateWorkflow(url_root, template_id, token, body)