add: etag support
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
#api/__init__.py
|
||||
import base64
|
||||
import os
|
||||
from functools import wraps
|
||||
|
||||
from cryptography import x509
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from flask import jsonify, Blueprint, request
|
||||
from flask import jsonify, Blueprint, request, make_response
|
||||
from flask_limiter import Limiter
|
||||
from flask_limiter.util import get_remote_address
|
||||
from jwt import decode, ExpiredSignatureError, InvalidTokenError, get_unverified_header
|
||||
@@ -14,6 +13,11 @@ import requests
|
||||
from threading import Lock
|
||||
|
||||
import env_provider
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
|
||||
|
||||
|
||||
_public_key_cache = {}
|
||||
_lock = Lock()
|
||||
@@ -135,3 +139,24 @@ def register_blueprints(app):
|
||||
app.register_blueprint(bp)
|
||||
|
||||
|
||||
|
||||
def generate_etag(data):
|
||||
serialized = json.dumps(data, sort_keys=True).encode('utf-8')
|
||||
return hashlib.md5(serialized).hexdigest()
|
||||
|
||||
def etag_response(f):
|
||||
@wraps(f)
|
||||
def decorator(*args, **kwargs):
|
||||
response = f(*args, **kwargs)
|
||||
if response[1] in (200, 201):
|
||||
if isinstance(response[0], (dict, list)):
|
||||
etag = generate_etag(response[0])
|
||||
if_none_match = request.headers.get("if_none_match")
|
||||
if if_none_match == etag:
|
||||
return jsonify({}), 200
|
||||
resp = make_response(response[0], response[1])
|
||||
resp.headers["ETag"] = etag
|
||||
return resp
|
||||
return response
|
||||
return decorator
|
||||
|
||||
|
||||
Reference in New Issue
Block a user