forked from Theamazingdp/AlteryxGalleryAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlteryxGalleryAPI.py
138 lines (113 loc) · 5.03 KB
/
AlteryxGalleryAPI.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import time
import collections
import random
import math
import requests
import string
import json
'''
SEE README FOR INSTRUCTIONS ON FUNCTIONALITY
GALLERY MODULE CREATED BY: DAVID PRYOR, NICK SIMMONS, AND RITU GOWLIKAR
'''
class Gallery(object):
def __init__(self, apiLocation, apiKey, apiSecret):
self.apiLocation = apiLocation
self.apiKey = apiKey
self.apiSecret = apiSecret
def buildOauthParams(self):
return {'oauth_consumer_key': self.apiKey,
'oauth_nonce': self.generate_nonce(5),
'oauth_signature_method': 'HMAC-SHA1',
'oauth_timestamp': str(int(math.floor(time.time()))),
'oauth_version': '1.0'}
'''Finds workflows in a subscription'''
def subscription(self):
method = 'GET'
url = self.apiLocation + '/workflows/subscription/'
params = self.buildOauthParams()
signature = self.generateSignature(method, url, params)
params.update({'oauth_signature': signature})
output = requests.get(url, params=params)
return output.text
'''Returns the questions for the given Alteryx Analytics App'''
def questions(self, appId):
method = 'GET'
url = self.apiLocation + '/workflows/' + appId + '/questions/'
params = self.buildOauthParams()
signature = self.generateSignature(method, url, params)
params.update({'oauth_signature': signature})
output = requests.get(url, params=params)
return output, output.content
'''Queue an app execution job. Returns ID of the job'''
def executeWorkflow(self, appId, *kwpos, **kwargs):
if('payload' in kwargs):
print('Payload included: %s' % kwargs['payload'])
data = kwargs['payload']
method = 'POST'
url = self.apiLocation + '/workflows/' + appId + '/jobs/'
params = self.buildOauthParams()
signature = self.generateSignature(method, url, params)
params.update({'oauth_signature': signature})
output = requests.post(url, json=data, headers={'Content-Type':'application/json'}, params=params)
else:
print('No Payload included')
method = 'POST'
url = self.apiLocation + '/workflows/' + appId + '/jobs/'
params = self.buildOauthParams()
signature = self.generateSignature(method, url, params)
params.update({'oauth_signature': signature})
output = requests.post(url, params=params)
return output,output.content
'''Returns the jobs for the given Alteryx Analytics App'''
def getJobs(self, appId):
method = 'GET'
url = self.apiLocation + '/workflows/' + appId + '/jobs/'
params = self.buildOauthParams()
signature = self.generateSignature(method, url, params)
params.update({'oauth_signature': signature})
output = requests.get(url, params=params)
return output, output.content
'''Retrieves the job and its current state'''
def getJobStatus(self, jobId):
method = 'GET'
url = self.apiLocation + '/jobs/' + jobId + '/'
params = self.buildOauthParams()
signature = self.generateSignature(method, url, params)
params.update({'oauth_signature': signature})
output = requests.get(url, params=params)
return output, output.content
'''Returns the output for a given job (FileURL)'''
def getJobOutput(self, jobID, outputID):
method = 'GET'
url = '/jobs/' + jobID + '/output/' + '/outputID/'
params = self.buildOauthParams()
signature = self.generateSignature(method, url, params)
params.update({'oauth_signature': signature})
output = requests.get(url, params=params)
return output, output.content
'''Returns the App that was requested'''
def getApp(self, appId):
method = 'GET'
url = self.apiLocation + '/' + appId + '/package/'
params = self.buildOauthParams()
signature = self.generateSignature(method, url, params)
params.update({'oauth_signature': signature})
output = requests.get(url, params=params)
return output, output.content
'''Generate pseudorandom number'''
def generate_nonce(self, length=5):
return ''.join([str(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase)) for i in
range(length)])
def generateSignature(self, httpMethod, url, params):
import urllib
import hmac
import binascii
import hashlib
from requests.utils import quote
"""returns HMAC-SHA1 sign"""
q = lambda x: quote(x, safe="~")
sorted_params = collections.OrderedDict(sorted(params.items()))
normalized_params = urllib.urlencode(sorted_params)
base_string = "&".join((httpMethod.upper(), q(url), q(normalized_params)))
sig = hmac.new("&".join([self.apiSecret, '']), base_string, hashlib.sha1)
return sig.digest().encode("base64").rstrip('\n')