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 85c675b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 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
19 changes: 10 additions & 9 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,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

Expand All @@ -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):
Expand Down

0 comments on commit 85c675b

Please sign in to comment.