-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
346 lines (302 loc) · 13.6 KB
/
api.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# -*- coding: iso-8859-15 -*-
"""
Copyright 2011 © Ooyala, Inc. All rights reserved.
Ooyala, Inc. ("Ooyala") hereby grants permission, free of charge, to any person or entity obtaining a copy of the software code provided in source code format via this webpage and direct links contained within this webpage and any associated documentation (collectively, the "Software"), to use, copy, modify, merge, and/or publish the Software and, subject to pass-through of all terms and conditions hereof, permission to transfer, distribute and sublicense the Software; all of the foregoing subject to the following terms and conditions:
1. The above copyright notice and this permission notice shall be included in all copies or portions of the Software.
2. For purposes of clarity, the Software does not include any APIs, but instead consists of code that may be used in conjunction with APIs that may be provided by Ooyala pursuant to a separate written agreement subject to fees.
3. Ooyala may in its sole discretion maintain and/or update the Software. However, the Software is provided without any promise or obligation of support, maintenance or update.
4. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, RELATING TO, ARISING FROM, IN CONNECTION WITH, OR INCIDENTAL TO THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, (i) IN NO EVENT SHALL OOYALA BE LIABLE FOR ANY CONSEQUENTIAL, INCIDENTAL, INDIRECT, SPECIAL, PUNITIVE, OR OTHER DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) RELATING TO, ARISING FROM, IN CONNECTION WITH, OR INCIDENTAL TO THE SOFTWARE OR THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF OOYALA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, AND (ii) OOYALA'S TOTAL AGGREGATE LIABILITY RELATING TO, ARISING FROM, IN CONNECTION WITH, OR INCIDENTAL TO THE SOFTWARE SHALL BE LIMITED TO THE ACTUAL DIRECT DAMAGES INCURRED UP TO MAXIMUM AMOUNT OF FIFTY DOLLARS ($50).
"""
import hashlib, base64, urllib.request, urllib.parse, urllib.error, http.client, time, logging, json
HTTP_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']
DEFAULT_EXPIRATION_WINDOW = 1500
DEFAULT_ROUND_UP_TIME = 300
API_VERSION = 'v2'
DEFAULT_BASE_URL = 'api.ooyala.com'
DEFAULT_CACHE_BASE_URL = 'cdn-api.ooyala.com'
logging.basicConfig(format='',level=logging.INFO)
class OoyalaAPI(object):
def __init__(self,
api_key,
secret_key,
base_url=DEFAULT_BASE_URL,
cache_base_url=DEFAULT_CACHE_BASE_URL,
expiration=DEFAULT_EXPIRATION_WINDOW):
"""OoyalaAPI Constructor
Type signature:
(str, str, str:DEFAULT_BASE_URL, int:DEFAULT_EXPIRATION_WINDOW) -> OoyalaAPI
Parameters:
api_key - The API key
secret_key - The secret key
base_url - the url's base
expiration - the expiration window, in seconds
Example:
api = OoyalaAPI("..", "..")
"""
self._secret_key = secret_key
self._api_key = api_key
self._base_url = base_url
self._cache_base_url = cache_base_url
self._expiration_window = expiration
self._response_headers = [()]
def send_request(self, http_method, relative_path, body=None, params={}):
"""Send a request.
Type signature:
(str, str, str:None, dict:{}) -> json str | None
Parameters:
http_method - the http method
relative_path - the url's relative path
body - the request's body
params - the query parameters
Example:
api = OoyalaAPI(secret_key, api_key)
response = api.send_request('GET', 'players/2kj390')
response = api.send_request('PATCH', 'players/2kj390', "{'name': 'my new player name'}")
"""
# create the url
path = '/%s/%s' % (API_VERSION,relative_path)
# Convert the body to JSON format
json_body = ''
if (body is not None):
json_body = json.dumps(body) if type(body) is not str else body
url = self.build_path_with_authentication_params(http_method, path, params, json_body)
if url is None:
return None
base_url = self.base_url if http_method != 'GET' else self.cache_base_url
connection = http.client.HTTPSConnection(base_url)
#hack needed when a PUT request has an empty body
headers = {}
if (body is None):
headers['Content-length'] = 0
#Make the request
connection.request(http_method, url, json_body, headers)
#get the response
response = connection.getresponse()
data = response.read()
self._response_headers = response.getheaders()
connection.close()
logging.debug('[%s] %s %s %d %s\n' % (http_method, url, json_body, response.status, data))
logging.info('[%s] %s %d' % (http_method, path, response.status))
if (response.status >= http.client.BAD_REQUEST):
return None
else:
if (len(data)==0):
data = b'true'
return json.loads(data.decode())
def get(self, path, query={}):
"""Send a GET request.
Type signature:
(str, srt:{}) -> json str | None
Parameters:
path - the url's path
query - the query parameters
Example:
api = Ooyala(...)
response = api.get('players/2kj390')
"""
return self.send_request('GET', path, None, query)
def put(self, path, body):
"""Send a PUT request.
Type signature:
(str, object) -> json str | None
Parameters:
path - the url's path
body - the request's body
Example:
api = Ooyala(...)
response = api.put('players/2kj390/metadata', {'skin' : 'branded'})
"""
return self.send_request('PUT', path, body)
def post(self, path, body, query={}):
"""Send a POST request.
Type signature:
(str, object) -> json str | None
Parameters:
path - the url's path
body - the request's body
Example:
api = Ooyala(...)
response = api.post('players/', {'name' : 'sample player'})
"""
return self.send_request('POST', path, body, query)
def patch(self, path, body):
"""Send a PATCH request.
Type signature:
(str, object) -> json str | None
Parameters:
path - the url's path
body - the request's body
Example:
api = Ooyala(...)
response = api.patch('players/2kj390', {'name' : 'renamed player'})
"""
return self.send_request('PATCH', path, body)
def delete(self, path):
"""Send a DELETE request.
Type signature:
(str) -> json str | None
Key Parameters:
'path' - the url's path
Example:
api = Ooyala(...)
response = api.delete('players/2kj390')
"""
return self.send_request('DELETE', path)
def generate_signature(self, http_method, path, params, body=''):
"""Generates the signature for a request.
Type signature:
(str, str, dict, str:'') -> str
Parameters:
http_method - the http method
path - the url's path
body - the request's body (a JSON encoded string)
params - query parameters
"""
signature = str(self.secret_key) + http_method.upper() + path
for key, value in sorted(params.items()):
signature += key + '=' + str(value)
# This is neccesary on python 2.7. if missing, signature+=body with raise an exception when body are bytes (image data)
signature = signature.encode('ascii')
signature += body.encode('ascii')
signature = base64.b64encode(hashlib.sha256(signature).digest())[0:43]
signature = urllib.parse.quote_plus(signature)
return signature
def build_path(self, path, params):
"""Build the path for a request.
Type signature:
(str, dict) -> str
Parameters:
path - the url's path
params - the query parameters
"""
# a local function which check if item is a query param
f = lambda k: k == 'where' or k == 'orderby' or k == 'limit' or k == 'page_token' or k == 'version'
url = path + '?'
url += "&".join(["%s=%s" % (key, urllib.parse.quote_plus(str(value)) if f(key) else value) for key, value in list(params.items())])
return url
def build_path_with_authentication_params(self, http_method, path, params, body):
"""Build the path for a Ooyala API request, including authentication parameters.
Type signature:
(str, str, str, dict, str) -> str
Parameters:
http_method - the http method
path - the url's path
params - the query parameters
body - the request's body
"""
if (http_method not in HTTP_METHODS) or (self.api_key is None) or (self.secret_key is None):
return None
authentication_params = dict(params)
#add the ooyala authentication params
authentication_params['api_key'] = str(self.api_key)
authentication_params['expires'] = str(self.expires)
authentication_params['signature'] = self.generate_signature(http_method, path, authentication_params, body)
return self.build_path(path, authentication_params)
@property
def response_headers(self):
"""Get the response's header from last request made.
Type signature:
() -> [(header,value)]
"""
return self._response_headers
def get_secret_key(self):
"""Secret Key getter.
Type signature:
() -> str
Example:
api = OoyalaAPI(...)
print 'the secret key is ', api.secret_key
"""
return self._secret_key
def set_secret_key(self, value):
"""Secret Key setter.
Type signature:
(str) -> None
Parameters:
value - the secret key to be set
Example:
api = OoyalaAPI(...)
api.secret_key = "83jja"
print 'the new secret key is ', api.secret_key
"""
self._secret_key = value
secret_key = property(get_secret_key, set_secret_key)
def get_api_key(self):
"""API Key getter.
Type signature:
() -> str
Example:
api = OoyalaAPI(...)
print 'the API key is ', api.api_key
"""
return self._api_key
def set_api_key(self, value):
"""API Key setter.
Type signature:
(str) -> None
Parameters:
value - the API key to be set
Example:
api = OoyalaAPI(...)
api.api_key = "332kka"
print 'the new API key is ', api.api_key
"""
self._api_key = value
api_key = property(get_api_key, set_api_key)
def get_base_url(self):
"""Base url getter.
Type signature:
() -> str
Example:
api = OoyalaAPI(...)
print 'the base url is ', api.base_url
"""
return self._base_url
def set_base_url(self, value):
"""Base url setter.
Type signature:
(str) -> None
Parameters:
value - the url's base to be set
Example:
api = OoyalaAPI(...)
api.base_url = "cache.api.ooyala.com"
print 'the new url's base is ', api.base_url
"""
self._base_url = value
base_url = property(get_base_url, set_base_url)
def get_cache_base_url(self):
"""Cache base url getter.
Type signature:
() -> str
Example:
api = OoyalaAPI(...)
print 'the cache base url is ', api.cache_base_url
"""
return self._cache_base_url
def set_cache_base_url(self, value):
"""Cache base url setter.
Type signature:
(str) -> None
Parameters:
value - the url's base to be set
Example:
api = OoyalaAPI(...)
api.base_url = "cache.api.ooyala.com"
print 'the new url's base is ', api.cache_base_url
"""
self._cache_base_url = value
cache_base_url = property(get_cache_base_url, set_cache_base_url)
def get_expiration_window(self): return self._expiration_window
def set_expiration_window(self, value): self._expiration_window = value
def del_expiration_window(self): del self._expiration_window
expiration_window = property(get_expiration_window, set_expiration_window, del_expiration_window)
@property
def expires(self):
"""Computes the expiration's time
Type signature:
() -> int
"""
now_plus_window = int(time.time()) + self.expiration_window
return now_plus_window + DEFAULT_ROUND_UP_TIME - (now_plus_window % DEFAULT_ROUND_UP_TIME)