API versioning (1.0)

This commit is contained in:
Davide Alberani 2015-05-03 01:43:30 +02:00
parent 686372961d
commit 7ffcad5ef7

View file

@ -38,6 +38,8 @@ import backend
ENCODING = 'utf-8' ENCODING = 'utf-8'
PROCESS_TIMEOUT = 60 PROCESS_TIMEOUT = 60
API_VERSION = '1.0'
re_env_key = re.compile('[^A-Z_]+') re_env_key = re.compile('[^A-Z_]+')
re_slashes = re.compile(r'//+') re_slashes = re.compile(r'//+')
@ -63,6 +65,9 @@ class BaseHandler(tornado.web.RequestHandler):
'true': True 'true': True
} }
def is_api(self):
return self.request.path.startswith('/v%s' % API_VERSION)
def tobool(self, obj): def tobool(self, obj):
if isinstance(obj, (list, tuple)): if isinstance(obj, (list, tuple)):
obj = obj[0] obj = obj[0]
@ -570,9 +575,16 @@ class LoginHandler(RootHandler):
if self._authorize(username, password): if self._authorize(username, password):
logging.info('successful login for user %s' % username) logging.info('successful login for user %s' % username)
self.set_secure_cookie("user", username) self.set_secure_cookie("user", username)
if self.is_api():
self.write({'error': None, 'message': 'successful login'})
else:
self.redirect('/') self.redirect('/')
return return
logging.info('login failed for user %s' % username) logging.info('login failed for user %s' % username)
if self.is_api():
self.set_status(403)
self.write({'error': None, 'message': 'successful login'})
else:
self.redirect('/login?failed=1') self.redirect('/login?failed=1')
@ -619,15 +631,21 @@ def run():
{'username': 'admin', 'password': utils.hash_password('eventman')}) {'username': 'admin', 'password': utils.hash_password('eventman')})
_ws_handler = (r"/ws/+event/+(?P<event_id>\w+)/+updates/?", WebSocketEventUpdatesHandler) _ws_handler = (r"/ws/+event/+(?P<event_id>\w+)/+updates/?", WebSocketEventUpdatesHandler)
_persons_path = r"/persons/?(?P<id_>\w+)?/?(?P<resource>\w+)?/?(?P<resource_id>\w+)?"
_events_path = r"/events/?(?P<id_>\w+)?/?(?P<resource>\w+)?/?(?P<resource_id>\w+)?"
application = tornado.web.Application([ application = tornado.web.Application([
(r"/persons/?(?P<id_>\w+)?/?(?P<resource>\w+)?/?(?P<resource_id>\w+)?", PersonsHandler, init_params), (_persons_path, PersonsHandler, init_params),
(r"/events/?(?P<id_>\w+)?/?(?P<resource>\w+)?/?(?P<resource_id>\w+)?", EventsHandler, init_params), (r'/v%s%s' % (API_VERSION, _persons_path), PersonsHandler, init_params),
(_events_path, EventsHandler, init_params),
(r'/v%s%s' % (API_VERSION, _events_path), EventsHandler, init_params),
(r"/(?:index.html)?", RootHandler, init_params), (r"/(?:index.html)?", RootHandler, init_params),
(r"/ebcsvpersons", EbCSVImportPersonsHandler, init_params), (r"/ebcsvpersons", EbCSVImportPersonsHandler, init_params),
(r"/settings", SettingsHandler, init_params), (r"/settings", SettingsHandler, init_params),
_ws_handler, _ws_handler,
(r'/login', LoginHandler, init_params), (r'/login', LoginHandler, init_params),
(r'/v%s/login' % API_VERSION, LoginHandler, init_params),
(r'/logout', LogoutHandler), (r'/logout', LogoutHandler),
(r'/v%s/logout' % API_VERSION, LogoutHandler),
(r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"}) (r'/(.*)', tornado.web.StaticFileHandler, {"path": "angular_app"})
], ],
template_path=os.path.join(os.path.dirname(__file__), "templates"), template_path=os.path.join(os.path.dirname(__file__), "templates"),