Implement Cloudflare API token support

This commit is contained in:
Quantum 2022-01-27 22:19:49 -05:00
parent 47dc38f4ba
commit c31c04eaa6

View file

@ -22,6 +22,13 @@ class CloudFlareCDN(object):
except ValueError:
sys.exit('Credentials file not valid JSON: %s' % (args.credentials,))
self.api_token = credentials.get('api_token')
if self.api_token:
if not isinstance(self.api_token, six.string_types):
sys.exit('In credentials file: key "api_token" should map to a string')
self.email = self.api_key = None
else:
self.email = credentials.get('email')
if not isinstance(self.email, six.string_types):
sys.exit('In credentials file: key "email" should map to a string')
@ -35,13 +42,18 @@ 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': group}, headers={
if self.api_token:
headers = {'Authorization': f'Bearer {self.api_token}'}
else:
headers = {
'X-Auth-Email': self.email,
'X-Auth-Key': self.api_key,
}
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': group}, headers=headers
).json()
if not resp.get('success'):