-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_handler.py
41 lines (34 loc) · 1.14 KB
/
base_handler.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
"""
Indico Request Handler
"""
import json, traceback
import logging
import tornado.web
import tornado.escape
from app.exceptions import ApplicationError, RouteNotFound, ServerError
logger = logging.getLogger("app")
class BaseAPIHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self, action):
try:
# Fetch appropriate handler
if not hasattr(self, str(action)):
raise RouteNotFound(action)
# Pass along the data and get a result
handler = getattr(self, str(action))
data = tornado.escape.json_decode(self.request.body)
handler(data)
except ApplicationError as e:
logger.warning(e.message, e.code)
self.respond(e.message, e.code)
except Exception as e:
logger.error(traceback.format_exc())
error = ServerError()
self.respond(error.message, error.code)
def respond(self, data, code=200):
self.set_status(code)
self.write(json.JSONEncoder().encode({
"status": code,
"data": data
}))
self.finish()