diff --git a/cloudflare ddns/cloudflare_api.py b/cloudflare ddns/cloudflare_api.py index a4d6f94..3d91ae4 100644 --- a/cloudflare ddns/cloudflare_api.py +++ b/cloudflare ddns/cloudflare_api.py @@ -1,6 +1,11 @@ import requests 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: def __init__(self, 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) 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'] 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) 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'] def change_record(self, subdomain, zoneId, recordId, 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, json={ '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 }) - if dns_change_response.status_code != 200: - raise Exception('Something went wrong updating the dns record...') + if response.status_code != 200: + raise CloudflareAPIException('Something went wrong updating the dns record...', response) logging.info('Succesfully updated the record ✅!')