Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress (gzip) the response of Flask-Restless-ng #44

Open
Cabu opened this issue Nov 9, 2023 · 0 comments
Open

Compress (gzip) the response of Flask-Restless-ng #44

Cabu opened this issue Nov 9, 2023 · 0 comments

Comments

@Cabu
Copy link

Cabu commented Nov 9, 2023

If flask, I can have a decorator like:

def gzipped(function):
    @functools.wraps(function)
    def decorated_function(*args, **kwargs):
        @flask.after_this_request
        def zipper(response):
            accept_encoding = flask.request.headers.get('Accept-Encoding', '')

            if 'gzip' not in accept_encoding.lower():
                return response

            response.direct_passthrough = False

            if response.status_code < 200 or response.status_code >= 300 or 'Content-Encoding' in response.headers:
                return response

            gzip_buffer = io.BytesIO()
            gzip_file = gzip.GzipFile(mode='wb', fileobj=gzip_buffer)
            gzip_file.write(response.data)
            gzip_file.close()

            response.data = gzip_buffer.getvalue()
            response.headers['Content-Encoding'] = 'gzip'
            response.headers['Vary'] = 'Accept-Encoding'
            response.headers['Content-Length'] = len(response.data)

            return response
        return function(*args, **kwargs)
    return decorated_function

and add @gzipped to my route and have its result compressed before sending it back to the browser. But, how can I do something similar for the response of Flask-Restless-ng query?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant