aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r/lib/view_utils.py
blob: 0cfe62c46a2e14e667aa1866aaf3a2cd44a5272e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from functools import wraps
from flask import request, Response, make_response
from pyaggr3g470r.lib.utils import to_hash


def etag_match(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        response = func(*args, **kwargs)
        if isinstance(response, Response):
            etag = to_hash(response.data)
            headers = response.headers
        elif type(response) is str:
            etag = to_hash(response)
            headers = {}
        else:
            return response
        if request.headers.get('if-none-match') == etag:
            response = Response(status=304)
            response.headers['Cache-Control'] \
                    = headers.get('Cache-Control', 'pragma: no-cache')
        elif not isinstance(response, Response):
            response = make_response(response)
        response.headers['etag'] = etag
        return response
    return wrapper
bgstack15