-
Notifications
You must be signed in to change notification settings - Fork 1
/
hetzner_dns.py
46 lines (38 loc) · 1.48 KB
/
hetzner_dns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPClientError
from hetzner_dns_zone import HetznerDNSZone
class HetznerDNS:
def __init__(self, api_token: str):
self.api_token: str = api_token
async def get_zones(self) -> dict:
try:
response = await AsyncHTTPClient().fetch(HTTPRequest(
url='https://dns.hetzner.com/api/v1/zones',
headers={
'Auth-API-Token': self.api_token
}
))
except HTTPClientError as e:
print(f'get_zones failed, {e}')
return {}
else:
return json.loads(response.body)['zones']
async def get_zone(self, name: str) -> HetznerDNSZone:
for zone in await self.get_zones():
if zone['name'] == name:
return HetznerDNSZone.from_dict(self.api_token, zone)
async def print_zones(self):
for zone in await self.get_zones():
print(f"id: {zone['id']} name: {zone['name']}")
async def get_record(self, record_id: str):
try:
response = await AsyncHTTPClient().fetch(HTTPRequest(
url=f'https://dns.hetzner.com/api/v1/records/{record_id}',
headers={
'Auth-API-Token': self.api_token
}
))
except HTTPClientError as e:
print(f'get_record failed, {e}')
else:
return json.loads(response.body)['record']