Print server response on Cloudflare errors

This commit is contained in:
Thomas Kleinendorst 2024-04-10 19:20:36 +02:00
parent d88904e105
commit ca501f9b26

View file

@ -1,6 +1,11 @@
import requests import requests
import logging import logging
class CloudflareAPIException(Exception):
def __init__(self, message, response) -> None:
full_message = f'Cloudflare request failed (code: {response.status_code}): {message}\n\nServer body:\n{response.text}'
super().__init__(full_message)
class CloudFlare: class CloudFlare:
def __init__(self, api_token): def __init__(self, api_token):
self.headers = {'Content-Type': 'application/json', 'Authorization': f'Bearer {api_token}'} self.headers = {'Content-Type': 'application/json', 'Authorization': f'Bearer {api_token}'}
@ -9,7 +14,7 @@ class CloudFlare:
response = requests.get(f'https://api.cloudflare.com/client/v4/zones?name={domain}', headers=self.headers) response = requests.get(f'https://api.cloudflare.com/client/v4/zones?name={domain}', headers=self.headers)
if response.status_code != 200: if response.status_code != 200:
raise Exception('Something went wrong requesting the zone of the domain on Cloudflare...') raise CloudflareAPIException('Something went wrong requesting the zone of the domain on Cloudflare...', response)
zone_id = response.json()['result'][0]['id'] zone_id = response.json()['result'][0]['id']
return zone_id return zone_id
@ -19,14 +24,14 @@ class CloudFlare:
response = requests.get(f'https://api.cloudflare.com/client/v4/zones/{zoneId}/dns_records?name={record}', headers=self.headers) response = requests.get(f'https://api.cloudflare.com/client/v4/zones/{zoneId}/dns_records?name={record}', headers=self.headers)
if response.status_code != 200: if response.status_code != 200:
raise Exception('Something went wrong requesting the record id of the domain name on Cloudflare...') raise CloudflareAPIException('Something went wrong requesting the record id of the domain name on Cloudflare...', response)
return response.json()['result'][0]['id'] return response.json()['result'][0]['id']
def change_record(self, subdomain, zoneId, recordId, targetIp): def change_record(self, subdomain, zoneId, recordId, targetIp):
logging.info(f'Changing record for {subdomain} to point to {targetIp}...') logging.info(f'Changing record for {subdomain} to point to {targetIp}...')
dns_change_response = requests.put(f'https://api.cloudflare.com/client/v4/zones/{zoneId}ffff/dns_records/{recordId}', response = requests.put(f'https://api.cloudflare.com/client/v4/zones/{zoneId}/dns_records/{recordId}',
headers=self.headers, headers=self.headers,
json={ json={
'content': targetIp, 'content': targetIp,
@ -37,7 +42,7 @@ class CloudFlare:
'ttl': 1 # Meaning "Automatic", see: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-update-dns-record 'ttl': 1 # Meaning "Automatic", see: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-update-dns-record
}) })
if dns_change_response.status_code != 200: if response.status_code != 200:
raise Exception('Something went wrong updating the dns record...') raise CloudflareAPIException('Something went wrong updating the dns record...', response)
logging.info('Succesfully updated the record ✅!') logging.info('Succesfully updated the record ✅!')