mirror of
https://codeberg.org/privacy1st/netcup-dns
synced 2024-12-22 23:36:04 +01:00
feat: custom exception classes
This commit is contained in:
parent
74b7747a94
commit
76f19a960a
6
src/netcup_dns/exception.py
Normal file
6
src/netcup_dns/exception.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class UnknownIPException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MultipleRecordsException(Exception):
|
||||||
|
pass
|
@ -10,6 +10,8 @@ from typing import Type
|
|||||||
import requests
|
import requests
|
||||||
from nc_dnsapi import Client, DNSRecord
|
from nc_dnsapi import Client, DNSRecord
|
||||||
|
|
||||||
|
from netcup_dns.exception import UnknownIPException, MultipleRecordsException
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
@ -49,7 +51,7 @@ def main():
|
|||||||
# Lazy: Only determine external IPv6 if an AAAA record shall be updated.
|
# Lazy: Only determine external IPv6 if an AAAA record shall be updated.
|
||||||
destination = external_ipv6()
|
destination = external_ipv6()
|
||||||
else:
|
else:
|
||||||
raise Exception(f'DNS record type {type_} is not supported.')
|
raise ValueError(f'DNS record type {type_} is not supported.')
|
||||||
|
|
||||||
if update_record_destination(api, domain, hostname, type_, destination=destination):
|
if update_record_destination(api, domain, hostname, type_, destination=destination):
|
||||||
print(f'Set {hostname}.{domain} {type_} record to {destination}')
|
print(f'Set {hostname}.{domain} {type_} record to {destination}')
|
||||||
@ -86,13 +88,14 @@ def get_record(api: Client, domain: str, hostname: str, type_: str) -> DNSRecord
|
|||||||
:param hostname:
|
:param hostname:
|
||||||
:param type_:
|
:param type_:
|
||||||
:return: The DNS record identified by `domain`, `hostname` and `type`.
|
:return: The DNS record identified by `domain`, `hostname` and `type`.
|
||||||
|
:raises MultipleRecordsException:
|
||||||
"""
|
"""
|
||||||
records: list[DNSRecord] = api.dns_records(domain)
|
records: list[DNSRecord] = api.dns_records(domain)
|
||||||
record: DNSRecord
|
record: DNSRecord
|
||||||
|
|
||||||
matches = [record for record in records if record.hostname == hostname and record.type == type_]
|
matches = [record for record in records if record.hostname == hostname and record.type == type_]
|
||||||
if len(matches) != 1:
|
if len(matches) != 1:
|
||||||
raise Exception(f'Expected one DNSRecord for {hostname}.{domain}, but got {len(matches)}')
|
raise MultipleRecordsException(f'Expected one DNSRecord for {hostname}.{domain}, but got {len(matches)}')
|
||||||
return matches[0]
|
return matches[0]
|
||||||
|
|
||||||
|
|
||||||
@ -127,6 +130,7 @@ def external_ip(version: Type[ipaddress.IPv4Address | ipaddress.IPv6Address], ti
|
|||||||
:param endpoints: List of webservices that return ones public IP IPv4/IPv6 address.
|
:param endpoints: List of webservices that return ones public IP IPv4/IPv6 address.
|
||||||
:argument timeout: Timeout for each webservice in seconds.
|
:argument timeout: Timeout for each webservice in seconds.
|
||||||
:return: Public IPv4/IPv6 address.
|
:return: Public IPv4/IPv6 address.
|
||||||
|
:raises UnknownIPException:
|
||||||
"""
|
"""
|
||||||
if endpoints is None:
|
if endpoints is None:
|
||||||
if version == ipaddress.IPv4Address:
|
if version == ipaddress.IPv4Address:
|
||||||
@ -159,7 +163,7 @@ def external_ip(version: Type[ipaddress.IPv4Address | ipaddress.IPv6Address], ti
|
|||||||
# Return IP address as string.
|
# Return IP address as string.
|
||||||
return parsed_ip.exploded
|
return parsed_ip.exploded
|
||||||
|
|
||||||
raise Exception('Could not determine public IP address.')
|
raise UnknownIPException('Could not determine public IP address.')
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=None)
|
@lru_cache(maxsize=None)
|
||||||
|
Loading…
Reference in New Issue
Block a user