2016-03-05 16:49:26 -08:00
|
|
|
from webagg.liverec import request as remote_request
|
|
|
|
|
2016-03-03 11:55:43 -08:00
|
|
|
from webagg.inputrequest import DirectWSGIInputRequest, POSTInputRequest
|
2016-03-06 23:10:30 -08:00
|
|
|
from bottle import route, request, response, abort, Bottle
|
2016-03-01 14:46:05 -08:00
|
|
|
import bottle
|
2016-02-28 14:33:08 -08:00
|
|
|
|
2016-02-29 12:34:06 -08:00
|
|
|
import traceback
|
|
|
|
import json
|
|
|
|
|
2016-03-02 18:13:13 -08:00
|
|
|
JSON_CT = 'application/json; charset=utf-8'
|
|
|
|
|
2016-03-05 16:49:26 -08:00
|
|
|
|
|
|
|
#=============================================================================
|
2016-03-06 23:10:30 -08:00
|
|
|
class ResAggApp(object):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.application = Bottle()
|
|
|
|
self.application.default_error_handler = self.err_handler
|
|
|
|
self.route_dict = {}
|
|
|
|
|
|
|
|
@self.application.route('/')
|
|
|
|
def list_routes():
|
|
|
|
return self.route_dict
|
|
|
|
|
|
|
|
def add_route(self, path, handler):
|
|
|
|
@self.application.route([path, path + '/<mode:path>'], 'ANY')
|
|
|
|
@wrap_error
|
|
|
|
def direct_input_request(mode=''):
|
|
|
|
params = dict(request.query)
|
|
|
|
params['mode'] = mode
|
|
|
|
params['_input_req'] = DirectWSGIInputRequest(request.environ)
|
|
|
|
return handler(params)
|
|
|
|
|
|
|
|
@self.application.route([path + '/postreq', path + '/<mode:path>/postreq'], 'POST')
|
|
|
|
@wrap_error
|
|
|
|
def post_fullrequest(mode=''):
|
|
|
|
params = dict(request.query)
|
|
|
|
params['mode'] = mode
|
|
|
|
params['_input_req'] = POSTInputRequest(request.environ)
|
|
|
|
return handler(params)
|
|
|
|
|
|
|
|
handler_dict = handler.get_supported_modes()
|
|
|
|
self.route_dict[path] = handler_dict
|
|
|
|
self.route_dict[path + '/postreq'] = handler_dict
|
|
|
|
|
|
|
|
def err_handler(self, exc):
|
|
|
|
if bottle.debug:
|
|
|
|
print(exc)
|
|
|
|
traceback.print_exc()
|
|
|
|
response.status = exc.status_code
|
|
|
|
response.content_type = JSON_CT
|
|
|
|
err_msg = json.dumps({'message': exc.body})
|
|
|
|
response.headers['ResErrors'] = err_msg
|
|
|
|
return err_msg
|
2016-02-29 12:34:06 -08:00
|
|
|
|
2016-03-01 14:46:05 -08:00
|
|
|
|
2016-03-05 16:49:26 -08:00
|
|
|
#=============================================================================
|
2016-02-29 12:34:06 -08:00
|
|
|
def wrap_error(func):
|
2016-03-01 14:46:05 -08:00
|
|
|
def wrap_func(*args, **kwargs):
|
2016-02-29 12:34:06 -08:00
|
|
|
try:
|
2016-03-05 16:49:26 -08:00
|
|
|
out_headers, res, errs = func(*args, **kwargs)
|
|
|
|
|
|
|
|
if out_headers:
|
|
|
|
for n, v in out_headers.items():
|
|
|
|
response.headers[n] = v
|
2016-03-02 18:13:13 -08:00
|
|
|
|
|
|
|
if res:
|
|
|
|
if errs:
|
|
|
|
response.headers['ResErrors'] = json.dumps(errs)
|
|
|
|
return res
|
|
|
|
|
|
|
|
last_exc = errs.pop('last_exc', None)
|
|
|
|
if last_exc:
|
|
|
|
if bottle.debug:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
response.status = last_exc.status()
|
|
|
|
message = last_exc.msg
|
|
|
|
else:
|
|
|
|
response.status = 404
|
|
|
|
message = 'No Resource Found'
|
|
|
|
|
|
|
|
response.content_type = JSON_CT
|
|
|
|
res = {'message': message}
|
|
|
|
if errs:
|
|
|
|
res['errors'] = errs
|
|
|
|
|
|
|
|
err_msg = json.dumps(res)
|
|
|
|
response.headers['ResErrors'] = err_msg
|
|
|
|
return err_msg
|
|
|
|
|
2016-02-29 12:34:06 -08:00
|
|
|
except Exception as e:
|
2016-03-01 14:46:05 -08:00
|
|
|
if bottle.debug:
|
2016-02-28 14:33:08 -08:00
|
|
|
traceback.print_exc()
|
2016-02-29 12:34:06 -08:00
|
|
|
abort(500, 'Internal Error: ' + str(e))
|
2016-02-28 14:33:08 -08:00
|
|
|
|
2016-03-01 14:46:05 -08:00
|
|
|
return wrap_func
|
|
|
|
|
2016-02-28 14:33:08 -08:00
|
|
|
|