-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
62 lines (49 loc) · 1.92 KB
/
api.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
import json
import urllib.error
import urllib.request
class CaddyApi:
def __init__(self, base_url=None, server='server0'):
self.data = None
self.server = server
if not base_url:
self.base_url = 'http://localhost:2019/'
def add_route(self, data=None, route_id=None):
self.data = data
if route_id and self.is_idexists(route_id):
path = f"id/{route_id}/"
return self._api(path=path, method='PATCH', data=data)
else:
path = f'config/apps/http/servers/{self.server}/routes'
return self._api(path=path, method='POST', data=data)
def is_idexists(self, element_id) -> bool:
response = self._api(path=f"id/{element_id}/", method='GET')
return response["status"] == 200
def _api(self, path='', method='GET', data=None) -> dict:
result = dict({
"status": None,
"path": path,
"response": None,
"message": None
})
if data:
data = json.dumps(data, indent=2).encode('utf-8')
req = urllib.request.Request(self.base_url + path, data=data, method=method)
req.add_header('Content-Type', f'application/json')
try:
with urllib.request.urlopen(req) as response:
result["status"] = response.status
r = response.read().decode('utf-8')
if response.status != 200:
result["message"] = 'An Error Occurred!'
if len(r) == 0:
result["message"] = response.msg
else:
result["response"] = json.loads(r)
except urllib.error.HTTPError as e:
result["message"] = str(e)
except json.decoder.JSONDecodeError as e:
result["message"] = str(e)
except:
result["message"] = "Unknown error!"
finally:
return result