Skip to content

Commit

Permalink
Implement 'purge_everything' method the Cloudflare front-end cache ba…
Browse files Browse the repository at this point in the history
…ckend
  • Loading branch information
ababic committed May 10, 2024
1 parent a09bba6 commit 6932cfd
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions wagtail/contrib/frontend_cache/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def invalidates_hostname(self, hostname):
"""
return validate_host(hostname, self.hostnames)

def purge_everything(self):
raise NotImplementedError


class HTTPBackend(BaseBackend):
def __init__(self, params):
Expand Down Expand Up @@ -118,42 +121,28 @@ def __init__(self, params):
"The setting 'WAGTAILFRONTENDCACHE' requires both 'EMAIL' and 'API_KEY', or 'BEARER_TOKEN' to be specified."
)

def _purge_urls(self, urls):
try:
purge_url = (
"https://api.cloudflare.com/client/v4/zones/{}/purge_cache".format(
self.cloudflare_zoneid
)
)

headers = {"Content-Type": "application/json"}

if self.cloudflare_token:
headers["Authorization"] = f"Bearer {self.cloudflare_token}"
else:
headers["X-Auth-Email"] = self.cloudflare_email
headers["X-Auth-Key"] = self.cloudflare_api_key
def _make_purge_request(self, urls, method="DELETE"):

headers = {"Content-Type": "application/json"}
if self.cloudflare_token:
headers["Authorization"] = f"Bearer {self.cloudflare_token}"
else:
headers["X-Auth-Email"] = self.cloudflare_email
headers["X-Auth-Key"] = self.cloudflare_api_key
if urls:
data = {"files": urls}
else:
data = None

response = requests.delete(
purge_url,
try:
response = requests.request(
method=method,
url="https://api.cloudflare.com/client/v4/zones/{}/purge_cache".format(
self.cloudflare_zoneid
),
json=data,
headers=headers,
)

try:
response_json = response.json()
except ValueError:
if response.status_code != 200:
response.raise_for_status()
else:
for url in urls:
logger.error(
"Couldn't purge '%s' from Cloudflare. Unexpected JSON parse error.",
url,
)

except requests.exceptions.HTTPError as e:
for url in urls:
logging.exception(
Expand All @@ -163,6 +152,19 @@ def _purge_urls(self, urls):
)
return

try:
response_json = response.json()
except ValueError:
if response.status_code != 200:
response.raise_for_status()
else:
for url in urls:
logger.error(
"Couldn't purge '%s' from Cloudflare. Unexpected JSON parse error.",
url,
)
return

if response_json["success"] is False:
error_messages = ", ".join(
[str(err["message"]) for err in response_json["errors"]]
Expand All @@ -179,11 +181,13 @@ def purge_batch(self, urls):
# Break the batched URLs in to chunks to fit within Cloudflare's maximum size for
# the purge_cache call (https://api.cloudflare.com/#zone-purge-files-by-url)
for i in range(0, len(urls), self.CHUNK_SIZE):
chunk = urls[i : i + self.CHUNK_SIZE]
self._purge_urls(chunk)
self._make_purge_request(urls[i : i + self.CHUNK_SIZE])

def purge(self, url):
self.purge_batch([url])
self._make_purge_request(urls=[url])

def purge_everything(self):
self._make_purge_request(urls=None, method="POST")


class CloudfrontBackend(BaseBackend):
Expand Down

0 comments on commit 6932cfd

Please sign in to comment.