Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #353 from alphagov/feature/cache-control
Browse files Browse the repository at this point in the history
HTTP validation is different to staleness
  • Loading branch information
robyoung committed Aug 26, 2014
2 parents e80cb28 + ba1f75f commit 0599928
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
14 changes: 1 addition & 13 deletions backdrop/core/cache_control.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
Cache control decorators for flask apps
"""
import hashlib
from flask import make_response, request
from flask import make_response
from functools import wraps


Expand All @@ -21,14 +20,3 @@ def new_func(*args, **kwargs):
return resp
return new_func
return decorator


def etag(func):
@wraps(func)
def new_func(*args, **kwargs):
resp = make_response(func(*args, **kwargs))
resp.set_etag(hashlib.sha1(resp.data).hexdigest())
resp.make_conditional(request)
return resp

return new_func
18 changes: 18 additions & 0 deletions backdrop/core/http_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
HTTP Validation decorators for flask apps
See http://tools.ietf.org/html/rfc7234#section-4.3
"""
import hashlib
from flask import make_response, request
from functools import wraps


def etag(func):
@wraps(func)
def new_func(*args, **kwargs):
resp = make_response(func(*args, **kwargs))
resp.set_etag(hashlib.sha1(resp.data).hexdigest())
resp.make_conditional(request)
return resp

return new_func
4 changes: 2 additions & 2 deletions backdrop/read/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .query import parse_query_from_request
from .validation import validate_request_args
from ..core import log_handler, cache_control
from ..core import log_handler, cache_control, http_validation
from ..core.data_set import DataSet
from ..core.errors import InvalidOperationError
from ..core.timeutils import as_utc
Expand Down Expand Up @@ -157,7 +157,7 @@ def data(data_group, data_type):


@app.route('/<data_set_name>', methods=['GET', 'OPTIONS'])
@cache_control.etag
@http_validation.etag
def query(data_set_name):
with statsd.timer('read.route.{data_set_name}'.format(
data_set_name=data_set_name)):
Expand Down

0 comments on commit 0599928

Please sign in to comment.