Add ensure_response_type decorator.

So that all further decorators or response-processors know they
have a Response object.
This commit is contained in:
voussoir 2021-06-04 21:26:23 -07:00
parent 45da148493
commit ee8c4b7fd8
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -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