|
@@ -1,3 +1,6 @@
|
|
1
|
+#!/usr/bin/env python
|
|
2
|
+# -*- coding: utf-8 -*-
|
|
3
|
+
|
1
|
4
|
import json
|
2
|
5
|
|
3
|
6
|
import requests
|
|
@@ -6,6 +9,8 @@ import requests
|
6
|
9
|
class PdnsApi:
|
7
|
10
|
api_key = None
|
8
|
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
|
9
|
14
|
|
10
|
15
|
def set_api_key(self, api_key):
|
11
|
16
|
self.api_key = api_key
|
|
@@ -13,6 +18,18 @@ class PdnsApi:
|
13
|
18
|
def set_base_url(self, base_url):
|
14
|
19
|
self.base_url = base_url
|
15
|
20
|
|
|
21
|
+ def set_verify_cert(self, verify_cert):
|
|
22
|
+ if verify_cert in ("True", "true", True): # convert from string to real bool if needed
|
|
23
|
+ self.verify_cert = True
|
|
24
|
+ elif verify_cert in ("False", "false", False): # convert from string to real bool if needed
|
|
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
|
|
28
|
+
|
|
29
|
+ def set_http_auth(self, http_auth): # credentials should be given as list containing two string-elements
|
|
30
|
+ if len(http_auth) == 2: # first: username, second: password for http-basic auth
|
|
31
|
+ self.http_auth = (str(http_auth[0]), str(http_auth[1])) # ensure right format of credentials
|
|
32
|
+
|
16
|
33
|
def _query(self, uri, method, kwargs=None):
|
17
|
34
|
headers = {
|
18
|
35
|
'X-API-Key': self.api_key,
|
|
@@ -23,15 +40,20 @@ class PdnsApi:
|
23
|
40
|
data = json.dumps(kwargs)
|
24
|
41
|
|
25
|
42
|
if method == "GET":
|
26
|
|
- request = requests.get(self.base_url + uri, headers=headers)
|
|
43
|
+ request = requests.get(self.base_url + uri, headers=headers,
|
|
44
|
+ auth=self.http_auth, verify=self.verify_cert)
|
27
|
45
|
elif method == "POST":
|
28
|
|
- request = requests.post(self.base_url + uri, headers=headers, data=data)
|
|
46
|
+ request = requests.post(self.base_url + uri, headers=headers, data=data,
|
|
47
|
+ auth=self.http_auth, verify=self.verify_cert)
|
29
|
48
|
elif method == "PUT":
|
30
|
|
- request = requests.put(self.base_url + uri, headers=headers, data=data)
|
|
49
|
+ request = requests.put(self.base_url + uri, headers=headers, data=data,
|
|
50
|
+ auth=self.http_auth, verify=self.verify_cert)
|
31
|
51
|
elif method == "PATCH":
|
32
|
|
- request = requests.patch(self.base_url + uri, headers=headers, data=data)
|
|
52
|
+ request = requests.patch(self.base_url + uri, headers=headers, data=data,
|
|
53
|
+ auth=self.http_auth, verify=self.verify_cert)
|
33
|
54
|
elif method == "DELETE":
|
34
|
|
- request = requests.delete(self.base_url + uri, headers=headers)
|
|
55
|
+ request = requests.delete(self.base_url + uri, headers=headers,
|
|
56
|
+ auth=self.http_auth, verify=self.verify_cert)
|
35
|
57
|
else:
|
36
|
58
|
raise ValueError("Invalid method '%s'" % method)
|
37
|
59
|
|