mirror of
https://github.com/quantum5/purge-static.git
synced 2025-04-24 11:22:00 -04:00
Respect Cloudflare's per request purge limit
This commit is contained in:
parent
20606f2af2
commit
4092fd54b5
|
@ -4,6 +4,10 @@ import sys
|
||||||
import requests
|
import requests
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
from purge_static.utils import chunk
|
||||||
|
|
||||||
|
CLOUDFLARE_MAX_PURGE = 30
|
||||||
|
|
||||||
|
|
||||||
class CloudFlareCDN(object):
|
class CloudFlareCDN(object):
|
||||||
def __init__(self, args):
|
def __init__(self, args):
|
||||||
|
@ -31,15 +35,14 @@ class CloudFlareCDN(object):
|
||||||
sys.exit('No zone for CloudFlare, use --zone.')
|
sys.exit('No zone for CloudFlare, use --zone.')
|
||||||
|
|
||||||
def purge(self, urls):
|
def purge(self, urls):
|
||||||
resp = requests.post(
|
for group in chunk(urls, CLOUDFLARE_MAX_PURGE):
|
||||||
'https://api.cloudflare.com/client/v4/zones/%s/purge_cache' % (self.zone,),
|
resp = requests.post(
|
||||||
json={'files': urls}, headers={
|
'https://api.cloudflare.com/client/v4/zones/%s/purge_cache' % (self.zone,),
|
||||||
'X-Auth-Email': self.email,
|
json={'files': group}, headers={
|
||||||
'X-Auth-Key': self.api_key,
|
'X-Auth-Email': self.email,
|
||||||
}
|
'X-Auth-Key': self.api_key,
|
||||||
).json()
|
}
|
||||||
|
).json()
|
||||||
|
|
||||||
if resp.get('success'):
|
if not resp.get('success'):
|
||||||
return
|
sys.exit(resp)
|
||||||
|
|
||||||
sys.exit(resp)
|
|
||||||
|
|
6
purge_static/utils.py
Normal file
6
purge_static/utils.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from itertools import islice
|
||||||
|
|
||||||
|
|
||||||
|
def chunk(it, size):
|
||||||
|
it = iter(it)
|
||||||
|
return iter(lambda: list(islice(it, size)), [])
|
Loading…
Reference in a new issue