-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetcup.py
100 lines (76 loc) · 3.24 KB
/
netcup.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import logging
import requests
NETCUP_API_URL: str = 'https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON'
class Netcup:
logger: logging.Logger
customer_number: int
api_key: str
api_password: str
http_session: requests.Session
api_session: str
def __init__(self, logger: logging.Logger, customer_number: int, api_key: str, api_password: str):
self.logger = logger
self.customer_number = customer_number
self.api_key = api_key
self.api_password = api_password
self.http_session = requests.Session()
self.http_session.headers.update({
"Content-Type": "application/json; charset=utf-8",
"Accept": "application/json; charset=utf-8"
})
def login(self):
action = 'login'
param = {'customernumber': self.customer_number,
'apikey': self.api_key,
'apipassword': self.api_password}
response = self.send_http_request(action, param)
self.api_session = response["responsedata"]["apisessionid"]
def logout(self):
action = 'logout'
param = {'customernumber': self.customer_number,
'apikey': self.api_key,
'apisessionid': self.api_session}
self.send_http_request(action, param)
self.api_session = 'invalid'
self.http_session.close()
def getRecords(self, domain: str):
action = 'infoDnsRecords'
param = {'customernumber': self.customer_number,
'apikey': self.api_key,
'apisessionid': self.api_session,
'domainname': domain}
response = self.send_http_request(action, param)
return response['responsedata']['dnsrecords']
def updateRecords(self, domain: str, records: list):
action = 'updateDnsRecords'
param = {'customernumber': self.customer_number,
'apikey': self.api_key,
'apisessionid': self.api_session,
'domainname': domain,
'dnsrecordset': {
'dnsrecords': records
}}
return self.send_http_request(action, param)
def handle_response(self, action: str, response: requests.Response):
payload = json.loads(response.text)
message = "Netcup API: action={}, HTTP status={}, API status={}".format(action, response.reason,
payload["status"])
if response.ok:
self.logger.info(message)
if payload["status"] == "error":
self.logger.error("API error message: {}".format(payload["longmessage"]))
raise Exception()
else:
self.logger.error(message)
response.raise_for_status()
return payload
def send_http_request(self, action: str, param: dict):
payload = json.dumps({
'action': action,
'param': param
})
if self.logger.isEnabledFor(logging.DEBUG):
self.logger.debug('Requesting action={} with payload={}'.format(action, payload))
response = self.handle_response(action, self.http_session.post(NETCUP_API_URL, payload))
return response