|
@@ -7,11 +7,10 @@ import requests
|
7
|
7
|
|
8
|
8
|
|
9
|
9
|
class PdnsApi:
|
10
|
|
- http_auth_user = None
|
11
|
|
- http_auth_pass = None
|
12
|
10
|
api_key = None
|
13
|
|
- api_pass = None
|
14
|
11
|
base_url = None
|
|
12
|
+ http_auth = None # Standard-value of requests-library will be used
|
|
13
|
+ verify_cert = None # Standard-value of requests-library will be used
|
15
|
14
|
|
16
|
15
|
def set_api_key(self, api_key):
|
17
|
16
|
self.api_key = api_key
|
|
@@ -19,14 +18,17 @@ class PdnsApi:
|
19
|
18
|
def set_base_url(self, base_url):
|
20
|
19
|
self.base_url = base_url
|
21
|
20
|
|
22
|
|
- def set_api_pass(self, api_pass):
|
23
|
|
- self.api_pass = api_pass
|
|
21
|
+ def set_verify_cert(self, verify_cert):
|
|
22
|
+ if verify_cert in ("True", "true"): # convert from string to real bool
|
|
23
|
+ self.verify_cert = True
|
|
24
|
+ elif verify_cert in ("False", "false"): # convert from string to real bool
|
|
25
|
+ self.verify_cert = False
|
|
26
|
+ elif isinstance(verify_cert, str): # alternative: path to local cert is given as string
|
|
27
|
+ self.verify_cert = verify_cert # see requests-documentation for more info
|
24
|
28
|
|
25
|
|
- def set_http_auth_user(self, http_auth_user):
|
26
|
|
- self.http_auth_user = http_auth_user
|
27
|
|
-
|
28
|
|
- def set_http_auth_pass(self, http_auth_pass):
|
29
|
|
- self.http_auth_pass = http_auth_pass
|
|
29
|
+ def set_http_auth(self, http_auth): # credentials should be given as list containing two elements
|
|
30
|
+ if len(http_auth == 2): # first: username, second: password for http-basic auth
|
|
31
|
+ self.http_auth = http_auth
|
30
|
32
|
|
31
|
33
|
def _query(self, uri, method, kwargs=None):
|
32
|
34
|
headers = {
|
|
@@ -38,15 +40,20 @@ class PdnsApi:
|
38
|
40
|
data = json.dumps(kwargs)
|
39
|
41
|
|
40
|
42
|
if method == "GET":
|
41
|
|
- request = requests.get(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass))
|
|
43
|
+ request = requests.get(self.base_url + uri, headers=headers,
|
|
44
|
+ auth=self.http_auth, verify=self.verify_cert)
|
42
|
45
|
elif method == "POST":
|
43
|
|
- request = requests.post(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass), data=data)
|
|
46
|
+ request = requests.post(self.base_url + uri, headers=headers, data=data,
|
|
47
|
+ auth=self.http_auth, verify=self.verify_cert)
|
44
|
48
|
elif method == "PUT":
|
45
|
|
- request = requests.put(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass), data=data)
|
|
49
|
+ request = requests.put(self.base_url + uri, headers=headers, data=data,
|
|
50
|
+ auth=self.http_auth, verify=self.verify_cert)
|
46
|
51
|
elif method == "PATCH":
|
47
|
|
- request = requests.patch(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass), data=data)
|
|
52
|
+ request = requests.patch(self.base_url + uri, headers=headers, data=data,
|
|
53
|
+ auth=self.http_auth, verify=self.verify_cert)
|
48
|
54
|
elif method == "DELETE":
|
49
|
|
- request = requests.delete(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass))
|
|
55
|
+ request = requests.delete(self.base_url + uri, headers=headers,
|
|
56
|
+ auth=self.http_auth, verify=self.verify_cert)
|
50
|
57
|
else:
|
51
|
58
|
raise ValueError("Invalid method '%s'" % method)
|
52
|
59
|
|