-
-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathutils.py
More file actions
48 lines (37 loc) · 1.38 KB
/
utils.py
File metadata and controls
48 lines (37 loc) · 1.38 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Utility functions for interacting with the Fastly CDN API."""
import requests
from django.conf import settings
def purge_url(path):
"""Purge a Fastly.com URL given a path. path argument must begin with a slash."""
if settings.DEBUG:
return None
api_key = getattr(settings, "FASTLY_API_KEY", None)
if api_key:
return requests.request(
"PURGE",
f"https://www.python.org{path}",
headers={"Fastly-Key": api_key},
timeout=30,
)
return None
def purge_surrogate_key(key):
"""Purge all Fastly cached content tagged with a surrogate key.
Common keys (set by GlobalSurrogateKey middleware):
- 'pydotorg-app': Purges entire site
- 'downloads': Purges all /downloads/* pages
- 'events': Purges all /events/* pages
- 'sponsors': Purges all /sponsors/* pages
- etc. (first path segment becomes the surrogate key)
Returns the response from Fastly API, or None if not configured.
"""
if settings.DEBUG:
return None
api_key = getattr(settings, "FASTLY_API_KEY", None)
service_id = getattr(settings, "FASTLY_SERVICE_ID", None)
if not api_key or not service_id:
return None
return requests.post(
f"https://api.fastly.com/service/{service_id}/purge/{key}",
headers={"Fastly-Key": api_key},
timeout=30,
)