diff --git a/wsme/api.py b/wsme/api.py index 368b488..bfcb133 100644 --- a/wsme/api.py +++ b/wsme/api.py @@ -66,6 +66,9 @@ class FunctionDefinition(object): #: If the body carry the datas of a single argument, its type self.body_type = None + #: Status code + self.status_code = 200 + #: True if extra arguments should be ignored, NOT inserted in #: the kwargs of the function and not raise UnknownArgument #: exceptions @@ -99,8 +102,10 @@ class FunctionDefinition(object): for arg in self.arguments: 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.status_code = status_code self.ignore_extra_args = ignore_extra_args self.extra_options = extra_options @@ -148,6 +153,18 @@ class signature(object): 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): """Extract informations that can be sent to the client.""" error = excinfo[1] diff --git a/wsmeext/flask.py b/wsmeext/flask.py index 0c282db..e0a543e 100644 --- a/wsmeext/flask.py +++ b/wsmeext/flask.py @@ -58,16 +58,16 @@ def signature(*args, **kw): dataformat = get_dataformat() try: - status_code = None result = f(*args, **kwargs) - # Status code in result - if isinstance(result, (list, tuple)) and len(result) == 2: - result, status_code = result + # NOTE: Support setting of status_code with default 200 + status_code = 200 + if isinstance(result, wsme.api.Response): + result = result.result + status_code = result.status_code - # Status code is attached to request - if not status_code and hasattr(flask.request, 'status_code'): - status_code = flask.request.status_code + if funcdef.status_code: + status_code = funcdef.status_code res = flask.make_response( dataformat.encode_result( @@ -76,7 +76,7 @@ def signature(*args, **kw): ) ) res.mimetype = dataformat.content_type - res.status_code = status_code or 200 + res.status_code = status_code except: data = wsme.api.format_exception(sys.exc_info()) res = flask.make_response(dataformat.encode_error(None, data))