where input/output includes files, JSON metadata and GET parameters. Additionally, formalize context with a new class, Context.
27 lines
936 B
Python
27 lines
936 B
Python
import types
|
|
|
|
def _bind_method(target, desired_method_name):
|
|
actual_method = getattr(target, desired_method_name)
|
|
def _wrapper_method(_self, request, _response, *args, **kwargs):
|
|
request.context.output = \
|
|
actual_method(request.context, *args, **kwargs)
|
|
return types.MethodType(_wrapper_method, target)
|
|
|
|
class BaseApi(object):
|
|
'''
|
|
A wrapper around falcon's API interface that eases input and output
|
|
management.
|
|
'''
|
|
|
|
def __init__(self):
|
|
self._translate_routes()
|
|
|
|
def _translate_routes(self):
|
|
for method_name in ['GET', 'PUT', 'POST', 'DELETE']:
|
|
desired_method_name = method_name.lower()
|
|
falcon_method_name = 'on_%s' % method_name.lower()
|
|
if hasattr(self, desired_method_name):
|
|
setattr(
|
|
self,
|
|
falcon_method_name,
|
|
_bind_method(self, desired_method_name))
|