From 5fd22f9113567b1bd1562b593536afcc27e4da06 Mon Sep 17 00:00:00 2001 From: Bohdan Khorolets Date: Mon, 10 Oct 2016 09:45:02 +0300 Subject: [PATCH] Update flask_restplus_patched to have a possibility to use non model Schemas to response with code 200 (#37) --- flask_restplus_patched/_http.py | 1 + flask_restplus_patched/namespace.py | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/flask_restplus_patched/_http.py b/flask_restplus_patched/_http.py index b80dc080..0cd1cbc0 100644 --- a/flask_restplus_patched/_http.py +++ b/flask_restplus_patched/_http.py @@ -3,3 +3,4 @@ except ImportError: class HTTPStatus: NO_CONTENT = 204 + ACCEPTED = 202 diff --git a/flask_restplus_patched/namespace.py b/flask_restplus_patched/namespace.py index 9335fbb2..144e0df4 100644 --- a/flask_restplus_patched/namespace.py +++ b/flask_restplus_patched/namespace.py @@ -116,7 +116,9 @@ def response(self, model=None, code=200, description=None, **kwargs): ... abort(403) ... return Team.query.all() """ - if model is None and code != HTTPStatus.NO_CONTENT: + ALLOWED_EMPTY_BODY_STATUSES = (HTTPStatus.NO_CONTENT, HTTPStatus.ACCEPTED) + + if model is None and code not in ALLOWED_EMPTY_BODY_STATUSES: if code not in http_exceptions.default_exceptions: raise ValueError("`model` parameter is required for code %d" % code) model = self.model( @@ -126,7 +128,7 @@ def response(self, model=None, code=200, description=None, **kwargs): if description is None: if code in http_exceptions.default_exceptions: description = http_exceptions.default_exceptions[code].description - elif code == HTTPStatus.NO_CONTENT: + elif code in ALLOWED_EMPTY_BODY_STATUSES: description = 'Request fulfilled, nothing follows' def response_serializer_decorator(func): @@ -138,16 +140,15 @@ def dump_wrapper(*args, **kwargs): # pylint: disable=missing-docstring response = func(*args, **kwargs) - if isinstance(response, flask.Response): - return response - - elif response is None: - if code == HTTPStatus.NO_CONTENT: + if response is None: + if code in ALLOWED_EMPTY_BODY_STATUSES: return flask.Response( - status=HTTPStatus.NO_CONTENT, + status=code, content_type='application/json' ) raise ValueError("Reponse must not be empty with code 200") + elif isinstance(response, flask.Response) or model is None: + return response return model.dump(response).data @@ -169,7 +170,7 @@ def decorator(func_or_class): response_serializer_decorator(func_or_class) ) - if code == HTTPStatus.NO_CONTENT: + if code in ALLOWED_EMPTY_BODY_STATUSES: api_model = None else: if isinstance(model, Model):