-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
80 lines (71 loc) · 2.78 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
"""
Base Handlers to overide
"""
import logging as logger
import json
import tornado.web
import tornado.options
class BaseHandler(tornado.web.RequestHandler):
"""A class to collect common handler methods - all other handlers should
subclass this one.
"""
def set_default_headers(self):
"""Default set of headers for the application.
"""
self.set_header('Server', tornado.options.options.SERVER)
self.set_header("Content-Type", "application/json")
logger.debug("Default headers set")
def prepare(self):
"""Incorporate request JSON into arguments dictionary.
"""
# Set up response dictionary.
self.response = dict()
def load_json(self):
"""Load JSON from the request body and store them in
self.request.arguments, like Tornado does by default for POSTed form
parameters.
If JSON cannot be decoded, raises an HTTPError with status 400.
"""
logger.debug("load_json called")
try:
self.request.arguments = json.loads(self.request.body.decode('utf-8'))
except (ValueError, TypeError):
msg = "Could not decode JSON: %s" % self.request.body
logger.debug(msg)
self.send_error(400, message=msg) # Bad Request
def get_json_argument(self, name, default=None):
"""Find and return the argument with key 'name' from JSON request data.
Similar to Tornado's get_argument() method.
"""
if default is None:
default = self._ARG_DEFAULT
if not self.request.arguments:
self.load_json()
if name not in self.request.arguments:
if default is self._ARG_DEFAULT:
msg = "Missing argument '%s'" % name
logger.debug(msg)
raise tornado.web.HTTPError(400, msg)
logger.debug("Returning default argument %s, as we couldn't find '%s' in %s", default, name, self.request.arguments)
return default
arg = self.request.arguments[name]
logger.debug("Found '%s': %s in JSON arguments", name, arg)
return arg
def write_error(self, status_code, **kwargs):
if 'message' not in kwargs:
if status_code == 405:
kwargs['message'] = 'Invalid HTTP method.'
else:
kwargs['message'] = 'Unknown error.'
self.response = kwargs
self.write_json()
def write_json(self):
"""Returns Data as json to user
"""
output = json.dumps(self.response)
self.write(output)
def get_short_url(self, url_hash=""):
"""Returns formed short url from url_hash
"""
short_url = ("%s://%s/%s" % (self.request.protocol, self.request.host, url_hash))
return short_url