Skip to content

Commit

Permalink
Does not change default flask responses
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcaricio committed Feb 3, 2016
1 parent b28cd87 commit bf7638f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion connexion/decorators/decorator.py
Expand Up @@ -12,6 +12,7 @@
"""
import logging
import flask
from ..utils import is_flask_response

logger = logging.getLogger('connexion.decorators.decorator')

Expand All @@ -32,7 +33,7 @@ def get_full_response(data):
url = flask.request.url
logger.debug('Getting data and status code', extra={'data': data, 'data_type': type(data), 'url': url})
status_code, headers = 200, {}
if isinstance(data, flask.Response):
if is_flask_response(data):
data = data
status_code = data.status_code
headers = data.headers
Expand Down
5 changes: 3 additions & 2 deletions connexion/decorators/produces.py
Expand Up @@ -18,6 +18,7 @@
import json
import logging
from .decorator import BaseDecorator
from ..utils import is_flask_response

logger = logging.getLogger('connexion.decorators.produces')

Expand Down Expand Up @@ -83,7 +84,7 @@ def wrapper(*args, **kwargs):
url = flask.request.url
data, status_code, headers = self.get_full_response(function(*args, **kwargs))
logger.debug('Returning %s', url, extra={'url': url, 'mimetype': self.mimetype})
if isinstance(data, flask.Response): # if the function returns a Response object don't change it
if is_flask_response(data):
logger.debug('Endpoint returned a Flask Response', extra={'url': url, 'mimetype': data.mimetype})
return data

Expand Down Expand Up @@ -114,7 +115,7 @@ def wrapper(*args, **kwargs):
url = flask.request.url
logger.debug('Jsonifing %s', url, extra={'url': url, 'mimetype': self.mimetype})
data, status_code, headers = self.get_full_response(function(*args, **kwargs))
if isinstance(data, flask.Response): # if the function returns a Response object don't change it
if is_flask_response(data):
logger.debug('Endpoint returned a Flask Response', extra={'url': url, 'mimetype': data.mimetype})
return data
elif data is NoContent:
Expand Down
17 changes: 17 additions & 0 deletions connexion/utils.py
Expand Up @@ -14,6 +14,8 @@
import functools
import importlib
import re
import flask
import werkzeug.wrappers

PATH_PARAMETER = re.compile(r'\{([^}]*)\}')

Expand Down Expand Up @@ -59,6 +61,21 @@ def flaskify_path(swagger_path, types={}):
return PATH_PARAMETER.sub(convert_match, swagger_path)


def is_flask_response(obj):
"""
Verifies if obj is a default Flask response instance.
:type obj: object
:rtype bool
>>> is_flask_response(redirect('http://example.com/'))
True
>>> is_flask_response(flask.Response())
True
"""
return isinstance(obj, flask.Response) or isinstance(obj, werkzeug.wrappers.Response)


def deep_getattr(obj, attr):
"""
Recurses through an attribute chain to get the ultimate value.
Expand Down

0 comments on commit bf7638f

Please sign in to comment.