mirror of
https://github.com/quantum5/purge-static.git
synced 2025-04-24 03:11:58 -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 six
|
||||
|
||||
from purge_static.utils import chunk
|
||||
|
||||
CLOUDFLARE_MAX_PURGE = 30
|
||||
|
||||
|
||||
class CloudFlareCDN(object):
|
||||
def __init__(self, args):
|
||||
|
@ -31,15 +35,14 @@ class CloudFlareCDN(object):
|
|||
sys.exit('No zone for CloudFlare, use --zone.')
|
||||
|
||||
def purge(self, urls):
|
||||
for group in chunk(urls, CLOUDFLARE_MAX_PURGE):
|
||||
resp = requests.post(
|
||||
'https://api.cloudflare.com/client/v4/zones/%s/purge_cache' % (self.zone,),
|
||||
json={'files': urls}, headers={
|
||||
json={'files': group}, headers={
|
||||
'X-Auth-Email': self.email,
|
||||
'X-Auth-Key': self.api_key,
|
||||
}
|
||||
).json()
|
||||
|
||||
if resp.get('success'):
|
||||
return
|
||||
|
||||
if not resp.get('success'):
|
||||
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