Split Python methods in own files
This commit is contained in:
parent
6409f8c31c
commit
d88904e105
4 changed files with 77 additions and 66 deletions
43
cloudflare ddns/cloudflare_api.py
Normal file
43
cloudflare ddns/cloudflare_api.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import requests
|
||||
import logging
|
||||
|
||||
class CloudFlare:
|
||||
def __init__(self, api_token):
|
||||
self.headers = {'Content-Type': 'application/json', 'Authorization': f'Bearer {api_token}'}
|
||||
|
||||
def get_zone_id(self, domain):
|
||||
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...')
|
||||
|
||||
zone_id = response.json()['result'][0]['id']
|
||||
return zone_id
|
||||
|
||||
def get_record_id(self, zoneId, record):
|
||||
logging.debug('Getting record id...')
|
||||
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...')
|
||||
|
||||
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}',
|
||||
headers=self.headers,
|
||||
json={
|
||||
'content': targetIp,
|
||||
'name': subdomain,
|
||||
'proxied': False,
|
||||
'type': 'A',
|
||||
'comment': 'Is set to change with my DDNS script (on the Raspberry Pi).',
|
||||
'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...')
|
||||
|
||||
logging.info('Succesfully updated the record ✅!')
|
||||
Loading…
Add table
Add a link
Reference in a new issue