From ee8c4b7fd8396abc998c72d0b7a0995a1ebe2d6c Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 4 Jun 2021 21:26:23 -0700 Subject: [PATCH] Add ensure_response_type decorator. So that all further decorators or response-processors know they have a Response object. --- voussoirkit/flasktools.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/voussoirkit/flasktools.py b/voussoirkit/flasktools.py index b532b11..f183082 100644 --- a/voussoirkit/flasktools.py +++ b/voussoirkit/flasktools.py @@ -1,4 +1,5 @@ import flask +import functools import gzip import io import werkzeug.wrappers @@ -12,6 +13,15 @@ GZIP_LEVEL = 3 REQUEST_TYPES = (flask.Request, werkzeug.wrappers.Request, werkzeug.local.LocalProxy) RESPONSE_TYPES = (flask.Response, werkzeug.wrappers.Response) +def ensure_response_type(function): + @functools.wraps(function) + def wrapped(*args, **kwargs): + response = function(*args, **kwargs) + if not isinstance(response, RESPONSE_TYPES): + response = flask.Response(response) + return response + return wrapped + def gzip_response(request, response): if response.direct_passthrough: return response