Skip to content

Commit

Permalink
Update flask_restplus_patched to have a possibility to use non model …
Browse files Browse the repository at this point in the history
…Schemas to response with code 200
  • Loading branch information
khorolets committed Oct 10, 2016
1 parent 64bc2ad commit abb879f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions flask_restplus_patched/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
except ImportError:
class HTTPStatus:
NO_CONTENT = 204
ACCEPTED = 202
14 changes: 8 additions & 6 deletions flask_restplus_patched/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand All @@ -138,13 +140,13 @@ def dump_wrapper(*args, **kwargs):
# pylint: disable=missing-docstring
response = func(*args, **kwargs)

if isinstance(response, flask.Response):
if isinstance(response, flask.Response) or model is None:
return response

elif response is None:
if code == HTTPStatus.NO_CONTENT:
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")
Expand All @@ -169,7 +171,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):
Expand Down

0 comments on commit abb879f

Please sign in to comment.