-
Notifications
You must be signed in to change notification settings - Fork 278
/
base.py
604 lines (480 loc) · 20.7 KB
/
base.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# coding=utf-8
"""Base module for request handlers."""
from __future__ import division
from __future__ import unicode_literals
import base64
import collections
import json
import logging
import operator
import traceback
from builtins import object
from concurrent.futures import ThreadPoolExecutor
from datetime import date, datetime
from babelfish.language import Language
import jwt
from medusa import app
from medusa.logger.adapters.style import BraceAdapter
from six import itervalues, string_types, text_type, viewitems
from tornado.gen import coroutine
from tornado.httpclient import HTTPError
from tornado.httputil import url_concat
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler
log = BraceAdapter(logging.getLogger(__name__))
log.logger.addHandler(logging.NullHandler())
executor = ThreadPoolExecutor(thread_name_prefix='APIv2-Thread')
class BaseRequestHandler(RequestHandler):
"""A base class used for shared RequestHandler methods."""
DEFAULT_ALLOWED_METHODS = ('OPTIONS', )
#: resource name
name = None
#: identifier
identifier = None
#: path param
path_param = None
#: allowed HTTP methods
allowed_methods = None
#: parent resource handler
parent_handler = None
def prepare(self):
"""Check if JWT or API key is provided and valid."""
if self.request.method == 'OPTIONS':
return
api_key = self.get_argument('api_key', default=None) or self.request.headers.get('X-Api-Key')
if api_key and api_key == app.API_KEY:
return
authorization = self.request.headers.get('Authorization')
if not authorization:
return self._unauthorized('No authorization token.')
if authorization.startswith('Bearer'):
try:
token = authorization.replace('Bearer ', '')
jwt.decode(token, app.ENCRYPTION_SECRET, algorithms=['HS256'])
except jwt.ExpiredSignatureError:
return self._unauthorized('Token has expired.')
except jwt.DecodeError:
return self._unauthorized('Invalid token.')
elif authorization.startswith('Basic'):
auth_decoded = base64.decodestring(authorization[6:])
username, password = auth_decoded.split(':', 2)
if username != app.WEB_USERNAME or password != app.WEB_PASSWORD:
return self._unauthorized('Invalid user/pass.')
else:
return self._unauthorized('Invalid token.')
def async_call(self, name, *args, **kwargs):
"""Call the actual HTTP method, if available."""
try:
method = getattr(self, 'http_' + name)
except AttributeError:
raise HTTPError(405, '{name} method is not allowed'.format(name=name.upper()))
def blocking_call():
try:
return method(*args, **kwargs)
except Exception as error:
self._handle_request_exception(error)
return IOLoop.current().run_in_executor(executor, blocking_call)
@coroutine
def head(self, *args, **kwargs):
"""HEAD HTTP method."""
content = self.async_call('head', *args, **kwargs)
if content is not None:
content = yield content
if not self._finished:
self.finish(content)
@coroutine
def get(self, *args, **kwargs):
"""GET HTTP method."""
content = self.async_call('get', *args, **kwargs)
if content is not None:
content = yield content
if not self._finished:
self.finish(content)
@coroutine
def post(self, *args, **kwargs):
"""POST HTTP method."""
content = self.async_call('post', *args, **kwargs)
if content is not None:
content = yield content
if not self._finished:
self.finish(content)
@coroutine
def delete(self, *args, **kwargs):
"""DELETE HTTP method."""
content = self.async_call('delete', *args, **kwargs)
if content is not None:
content = yield content
if not self._finished:
self.finish(content)
@coroutine
def patch(self, *args, **kwargs):
"""PATCH HTTP method."""
content = self.async_call('patch', *args, **kwargs)
if content is not None:
content = yield content
if not self._finished:
self.finish(content)
@coroutine
def put(self, *args, **kwargs):
"""PUT HTTP method."""
content = self.async_call('put', *args, **kwargs)
if content is not None:
content = yield content
if not self._finished:
self.finish(content)
def write_error(self, *args, **kwargs):
"""Only send traceback if app.DEVELOPER is true."""
if app.DEVELOPER and 'exc_info' in kwargs:
self.set_header('content-type', 'text/plain')
self.set_status(500)
for line in traceback.format_exception(*kwargs['exc_info']):
self.write(line)
self.finish()
else:
response = self._internal_server_error()
self.finish(response)
def options(self, *args, **kwargs):
"""OPTIONS HTTP method."""
self._no_content()
def set_default_headers(self):
"""Set default CORS headers."""
if app.APP_VERSION:
self.set_header('X-Medusa-Server', app.APP_VERSION)
self.set_header('Access-Control-Allow-Origin', '*')
allowed_headers = ('Origin', 'Accept', 'Authorization', 'Content-Type',
'X-Requested-With', 'X-CSRF-Token', 'X-Api-Key', 'X-Medusa-Server')
self.set_header('Access-Control-Allow-Headers', ', '.join(allowed_headers))
allowed_methods = self.DEFAULT_ALLOWED_METHODS
if self.allowed_methods:
allowed_methods += self.allowed_methods
self.set_header('Access-Control-Allow-Methods', ', '.join(allowed_methods))
def api_response(self, status=None, error=None, data=None, headers=None, stream=None, content_type=None, **kwargs):
"""End the api request writing error or data to http response."""
content_type = content_type or 'application/json; charset=UTF-8'
if headers is not None:
for header in headers:
self.set_header(header, headers[header])
if error is not None and status is not None:
self.set_status(status)
self.set_header('content-type', content_type)
return {
'error': error
}
else:
self.set_status(status or 200)
if data is not None:
self.set_header('content-type', content_type)
return json.JSONEncoder(default=json_default_encoder).encode(data)
elif stream:
# This is mainly for assets
self.set_header('content-type', content_type)
return stream
elif kwargs and 'chunk' in kwargs:
self.set_header('content-type', content_type)
return kwargs
return None
@classmethod
def _create_base_url(cls, prefix_url, resource_name, *args):
elements = [prefix_url, resource_name] + \
[r'(?P<{key}>{value})'.format(key=key, value=value) for (key, value) in args]
return '/'.join(elements)
@classmethod
def create_url(cls, prefix_url, resource_name, *args):
"""Create url base on resource name and path params."""
resource_url = prefix_url + '/' + resource_name
path_params = ''
for arg in args:
if not arg:
continue
key, value = arg
q = r'(?:/(?P<{key}>{value}))'.format(key=key, value=value)
if path_params:
path_params = r'(?:{previous}(?:{current}|/?))'.format(previous=path_params, current=q)
else:
path_params = q
path_params = r'(?:{path}|/?)'.format(path=path_params)
return resource_url + path_params + '/?$'
@classmethod
def create_app_handler(cls, base):
"""Create app handler tuple: regex, class."""
if cls.parent_handler:
base = cls._create_base_url(base, cls.parent_handler.name, cls.parent_handler.identifier)
return cls.create_url(base, cls.name, *(cls.identifier, cls.path_param)), cls
def _handle_request_exception(self, error):
if isinstance(error, HTTPError):
response = self.api_response(error.code, error.message)
self.finish(response)
else:
super(BaseRequestHandler, self)._handle_request_exception(error)
def _ok(self, data=None, headers=None, stream=None, content_type=None):
return self.api_response(200, data=data, headers=headers, stream=stream, content_type=content_type)
def _created(self, data=None, identifier=None):
if identifier is not None:
location = self.request.path
if not location.endswith('/'):
location += '/'
self.set_header('Location', '{0}{1}'.format(location, identifier))
return self.api_response(201, data=data)
def _accepted(self):
return self.api_response(202)
def _no_content(self):
return self.api_response(204)
def _multi_status(self, data=None, headers=None):
return self.api_response(207, data=data, headers=headers)
def _bad_request(self, error):
return self.api_response(400, error=error)
def _unauthorized(self, error):
return self.api_response(401, error=error)
def _not_found(self, error='Resource not found'):
return self.api_response(404, error=error)
def _method_not_allowed(self, error):
return self.api_response(405, error=error)
def _conflict(self, error):
return self.api_response(409, error=error)
def _internal_server_error(self, error='Internal Server Error'):
return self.api_response(500, error=error)
def _not_implemented(self):
return self.api_response(501)
@classmethod
def _raise_bad_request_error(cls, error):
raise HTTPError(400, error)
def _get_sort(self, default):
values = self.get_argument('sort', default=default)
if values:
results = []
for value in values.split(','):
reverse = value.startswith('-')
if reverse or value.startswith('+'):
value = value[1:]
results.append((value, reverse))
return results
def _get_page(self):
try:
page = int(self.get_argument('page', default=1))
if page < 1:
self._raise_bad_request_error('Invalid page parameter')
return page
except ValueError:
self._raise_bad_request_error('Invalid page parameter')
def _get_limit(self, default=20, maximum=1000):
try:
limit = self._parse(self.get_argument('limit', default=default))
if limit < 1 or limit > maximum:
self._raise_bad_request_error('Invalid limit parameter')
return limit
except ValueError:
self._raise_bad_request_error('Invalid limit parameter')
def _paginate(self, data=None, data_generator=None, sort=None):
arg_page = self._get_page()
arg_limit = self._get_limit()
headers = {
'X-Pagination-Page': arg_page,
'X-Pagination-Limit': arg_limit
}
first_page = arg_page if arg_page > 0 else 1
previous_page = None if arg_page <= 1 else arg_page - 1
if data_generator:
results = list(data_generator())[:arg_limit]
next_page = None if len(results) < arg_limit else arg_page + 1
last_page = None
else:
arg_sort = self._get_sort(default=sort)
start = (arg_page - 1) * arg_limit
end = start + arg_limit
results = data
if arg_sort:
try:
for field, reverse in reversed(arg_sort):
results = sorted(results, key=operator.itemgetter(field), reverse=reverse)
except KeyError:
return self._bad_request('Invalid sort query parameter')
count = len(results)
headers['X-Pagination-Count'] = count
results = results[start:end]
next_page = None if end > count else arg_page + 1
last_page = ((count - 1) // arg_limit) + 1
if last_page <= arg_page:
last_page = None
# Reconstruct the query parameters
query_params = []
for arg, values in viewitems(self.request.query_arguments):
if arg in ('page', 'limit'):
continue
if not isinstance(values, list):
values = [values]
query_params += [(arg, value) for value in values]
bare_uri = url_concat(self.request.path, query_params)
links = []
for rel, page in (('next', next_page), ('last', last_page),
('first', first_page), ('previous', previous_page)):
if page is None:
continue
uri = url_concat(bare_uri, dict(page=page, limit=arg_limit))
link = '<{uri}>; rel="{rel}"'.format(uri=uri, rel=rel)
links.append(link)
self.set_header('Link', ', '.join(links))
return self._ok(data=results, headers=headers)
@classmethod
def _parse(cls, value, function=int):
"""Parse value using the specified function.
:param value:
:param function:
:type function: callable
:return:
"""
if value is not None:
try:
return function(value)
except ValueError:
cls._raise_bad_request_error('Invalid value {value!r}'.format(value=value))
@classmethod
def _parse_boolean(cls, value):
"""Parse value using the specified function.
:param value:
:return:
"""
if isinstance(value, text_type):
return value.lower() == 'true'
return cls._parse(value, bool)
@classmethod
def _parse_date(cls, value, fmt='%Y-%m-%d'):
"""Parse a date value using the specified format.
:param value:
:param fmt:
:return:
"""
return cls._parse(value, lambda d: datetime.strptime(d, fmt))
class NotFoundHandler(BaseRequestHandler):
"""A class used for the API v2 404 page."""
def http_get(self, *args, **kwargs):
"""Get."""
return self.api_response(status=404)
@classmethod
def create_app_handler(cls, base):
"""Capture everything."""
return r'{base}(/?.*)'.format(base=base), cls
def json_default_encoder(o):
"""Convert properties to string."""
if isinstance(o, Language):
return getattr(o, 'name')
if isinstance(o, set):
return list(o)
if isinstance(o, date):
return o.isoformat()
return text_type(o)
def iter_nested_items(data, prefix=''):
"""Iterate through the dictionary.
Nested keys are separated with dots.
"""
for key, value in viewitems(data):
p = prefix + key
if isinstance(value, collections.Mapping):
for inner_key, inner_value in iter_nested_items(value, prefix=p + '.'):
yield inner_key, inner_value
else:
yield p, value
def set_nested_value(data, key, value):
"""Set nested value to the dictionary."""
keys = key.split('.')
for k in keys[:-1]:
data = data.setdefault(k, {})
data[keys[-1]] = value
class PatchField(object):
"""Represent a field to be patched."""
def __init__(self, target, attr, attr_type, validator=None, converter=None,
default_value=None, setter=None, post_processor=None):
"""Constructor."""
if not hasattr(target, attr):
raise ValueError('{0!r} has no attribute {1}'.format(target, attr))
self.target = target
self.attr = attr
self.attr_type = attr_type
self.validator = validator or (lambda v: isinstance(v, self.attr_type))
self.converter = converter or (lambda v: v)
self.default_value = default_value
self.setter = setter
self.post_processor = post_processor
def patch(self, target, value):
"""Patch the field with the specified value."""
valid = self.validator(value)
if not valid and self.default_value is not None:
value = self.default_value
valid = True
if valid:
try:
if self.setter:
self.setter(target, self.attr, self.converter(value))
else:
setattr(target, self.attr, self.converter(value))
except AttributeError:
log.warning(
'Error trying to change attribute {attr} on target {target!r}'
' are you allowed to change this attribute?',
{'attr': self.attr, 'target': target}
)
return False
if self.post_processor:
self.post_processor(value)
return True
class StringField(PatchField):
"""Patch string fields."""
def __init__(self, target, attr, validator=None, converter=None, default_value=None,
setter=None, post_processor=None):
"""Constructor."""
super(StringField, self).__init__(target, attr, string_types, validator=validator, converter=converter,
default_value=default_value, setter=setter, post_processor=post_processor)
class IntegerField(PatchField):
"""Patch integer fields."""
def __init__(self, target, attr, validator=None, converter=None, default_value=None,
setter=None, post_processor=None):
"""Constructor."""
super(IntegerField, self).__init__(target, attr, int, validator=validator, converter=converter,
default_value=default_value, setter=setter, post_processor=post_processor)
class ListField(PatchField):
"""Patch list fields."""
def __init__(self, target, attr, validator=None, converter=None, default_value=None,
setter=None, post_processor=None):
"""Constructor."""
super(ListField, self).__init__(target, attr, list, validator=validator, converter=converter,
default_value=default_value, setter=setter, post_processor=post_processor)
class BooleanField(PatchField):
"""Patch boolean fields."""
def __init__(self, target, attr, validator=None, converter=int, default_value=None,
setter=None, post_processor=None):
"""Constructor."""
super(BooleanField, self).__init__(target, attr, bool, validator=validator, converter=converter,
default_value=default_value, setter=setter, post_processor=post_processor)
class EnumField(PatchField):
"""Patch enumeration fields."""
def __init__(self, target, attr, enums, attr_type=text_type, converter=None,
default_value=None, setter=None, post_processor=None):
"""Constructor."""
super(EnumField, self).__init__(target, attr, attr_type, validator=lambda v: v in enums,
converter=converter, default_value=default_value,
setter=setter, post_processor=post_processor)
# @TODO: Make this field more dynamic (a dict patch field)
class MetadataStructureField(PatchField):
"""Process the metadata structure."""
def __init__(self, target, attr):
"""Constructor."""
super(MetadataStructureField, self).__init__(target, attr, dict, validator=None, converter=None,
default_value=None, setter=None, post_processor=None)
def patch(self, target, value):
"""Patch the field with the specified value."""
map_values = {
'showMetadata': 'show_metadata',
'episodeMetadata': 'episode_metadata',
'episodeThumbnails': 'episode_thumbnails',
'seasonPosters': 'season_posters',
'seasonBanners': 'season_banners',
'seasonAllPoster': 'season_all_poster',
'seasonAllBanner': 'season_all_banner',
}
try:
for new_provider_config in itervalues(value):
for k, v in viewitems(new_provider_config):
setattr(target.metadata_provider_dict[new_provider_config['name']], map_values.get(k, k), v)
except Exception as error:
log.warning('Error trying to change attribute app.metadata_provider_dict: {0!r}', error)
return False
return True