Re-iterate status_code code

This commit is contained in:
Endre Karlson 2013-03-28 12:23:32 +00:00
parent 3d69ac33d8
commit 0222cbab00
2 changed files with 26 additions and 9 deletions

View File

@ -66,6 +66,9 @@ class FunctionDefinition(object):
#: If the body carry the datas of a single argument, its type #: If the body carry the datas of a single argument, its type
self.body_type = None self.body_type = None
#: Status code
self.status_code = 200
#: True if extra arguments should be ignored, NOT inserted in #: True if extra arguments should be ignored, NOT inserted in
#: the kwargs of the function and not raise UnknownArgument #: the kwargs of the function and not raise UnknownArgument
#: exceptions #: exceptions
@ -99,8 +102,10 @@ class FunctionDefinition(object):
for arg in self.arguments: for arg in self.arguments:
arg.resolve_type(registry) arg.resolve_type(registry)
def set_options(self, body=None, ignore_extra_args=False, **extra_options): def set_options(self, body=None, ignore_extra_args=False, status_code=200,
**extra_options):
self.body_type = body self.body_type = body
self.status_code = status_code
self.ignore_extra_args = ignore_extra_args self.ignore_extra_args = ignore_extra_args
self.extra_options = extra_options self.extra_options = extra_options
@ -148,6 +153,18 @@ class signature(object):
sig = signature sig = signature
class Response(object):
"""
Object to hold the "response" from a view function
"""
def __init__(self, obj, status_code=None):
#: Store the result object from the view
self.obj = obj
#: Store an optional status_code
self.status_code = status_code
def format_exception(excinfo, debug=False): def format_exception(excinfo, debug=False):
"""Extract informations that can be sent to the client.""" """Extract informations that can be sent to the client."""
error = excinfo[1] error = excinfo[1]

View File

@ -58,16 +58,16 @@ def signature(*args, **kw):
dataformat = get_dataformat() dataformat = get_dataformat()
try: try:
status_code = None
result = f(*args, **kwargs) result = f(*args, **kwargs)
# Status code in result # NOTE: Support setting of status_code with default 200
if isinstance(result, (list, tuple)) and len(result) == 2: status_code = 200
result, status_code = result if isinstance(result, wsme.api.Response):
result = result.result
status_code = result.status_code
# Status code is attached to request if funcdef.status_code:
if not status_code and hasattr(flask.request, 'status_code'): status_code = funcdef.status_code
status_code = flask.request.status_code
res = flask.make_response( res = flask.make_response(
dataformat.encode_result( dataformat.encode_result(
@ -76,7 +76,7 @@ def signature(*args, **kw):
) )
) )
res.mimetype = dataformat.content_type res.mimetype = dataformat.content_type
res.status_code = status_code or 200 res.status_code = status_code
except: except:
data = wsme.api.format_exception(sys.exc_info()) data = wsme.api.format_exception(sys.exc_info())
res = flask.make_response(dataformat.encode_error(None, data)) res = flask.make_response(dataformat.encode_error(None, data))