Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Improve readability with early returns
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Sep 22, 2020
1 parent d37fc84 commit 4800273
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
39 changes: 23 additions & 16 deletions kinto_changes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def _handle_old_since_redirect(request):
See https://bugzilla.mozilla.org/show_bug.cgi?id=1529685
and https://bugzilla.mozilla.org/show_bug.cgi?id=1665319#c2
"""
qs_since = request.validated["querystring"].get("_since")
if qs_since is None:
return

settings = request.registry.settings
max_age_since = int(settings.get("changes.since_max_age_days", 21))
if max_age_since < 0:
Expand All @@ -143,25 +147,28 @@ def _handle_old_since_redirect(request):
min_since_dt = datetime.now() - timedelta(days=max_age_since)
min_since = min_since_dt.timestamp() * 1000

http_scheme = request.registry.settings.get("http_scheme") or "https"
http_host = request.registry.settings.get(
if qs_since >= min_since:
# Since value is recent. No redirect.
return

http_scheme = settings.get("http_scheme") or "https"
http_host = settings.get(
"changes.http_host",
request.registry.settings.get("http_host")
)
redirect = f"{http_scheme}://{http_host}"

since = request.validated["querystring"].get("_since")
if since and since < min_since:
redirect += request.route_path(
"record-plural",
bucket_id=MONITOR_BUCKET,
collection_id=CHANGES_COLLECTION
)
queryparams = request.GET.copy()
del queryparams["_since"]
if queryparams:
redirect += "?" + urlencode(queryparams)
raise httpexceptions.HTTPTemporaryRedirect(redirect)
host_uri = f"{http_scheme}://{http_host}"
redirect = host_uri + request.route_path(
"record-plural",
bucket_id=MONITOR_BUCKET,
collection_id=CHANGES_COLLECTION
)

queryparams = request.GET.copy()
del queryparams["_since"]
if queryparams:
redirect += "?" + urlencode(queryparams)

raise httpexceptions.HTTPTemporaryRedirect(redirect)


@implementer(IAuthorizationPolicy)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


SAMPLE_RECORD = {'data': {'dev-edition': True}}
HOUR_AGO = int(datetime.datetime.now().timestamp() * 1000) - 3600


class UpdateChangesTest(BaseWebTest, unittest.TestCase):
Expand Down Expand Up @@ -163,23 +164,22 @@ def test_cache_expires_headers_are_supported(self):
assert "max-age=60" in resp.headers["Cache-Control"]

def test_cache_expires_header_is_maximum_with_cache_busting(self):
resp = self.app.get(self.changes_uri + "?_since=0&_expected=42")
resp = self.app.get(self.changes_uri + f"?_since={HOUR_AGO}&_expected=42")
assert "max-age=3600" in resp.headers["Cache-Control"]

def test_cache_expires_header_is_default_with_filter(self):
# The _since just filters on lower bound of timestamps, if data changes
# we don't want to cache for too long.
resp = self.app.get(self.changes_uri + "?_since=0")
resp = self.app.get(self.changes_uri + f"?_since={HOUR_AGO}")
assert "max-age=60" in resp.headers["Cache-Control"]

def test_cache_expires_header_is_default_with_concurrency_control(self):
# The `If-None-Match` header is just a way to obtain a 304 instead of a 200
# with an empty list. In the client code [0] it is always used in conjonction
# with _since={last-etag}
# [0] https://searchfox.org/mozilla-central/rev/93905b66/services/settings/Utils.jsm#70-73
hour_ago = int(datetime.datetime.now().timestamp() * 1000) - 3600
headers = {"If-None-Match": f'"{hour_ago}"'}
resp = self.app.get(self.changes_uri + f'?_since="{hour_ago}"', headers=headers)
headers = {"If-None-Match": f'"{HOUR_AGO}"'}
resp = self.app.get(self.changes_uri + f'?_since="{HOUR_AGO}"', headers=headers)
assert "max-age=60" in resp.headers["Cache-Control"]


Expand Down

0 comments on commit 4800273

Please sign in to comment.