-
Notifications
You must be signed in to change notification settings - Fork 2
/
yggdrasilctl.py
36 lines (29 loc) · 1.1 KB
/
yggdrasilctl.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
import json
import socket
class APIError(Exception):
"Base class for Yggdrasil admin API related errors."
class APIUnreachable(APIError):
"Exception raised in case of unsucessful attemption to connect to API."
class AdminAPI:
def __init__(self, address: str):
self.address = address
def _get_connection(self):
connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
connection.connect(self.address)
except Exception as err:
raise APIUnreachable(f"Can't connect to {self.address}: {err}")
return connection
def request(self, method: str, **kwargs)->dict:
req = {'request': method, 'arguments': kwargs}
connection = self._get_connection()
try:
json.dump(req, connection.makefile('w'))
response = json.load(connection.makefile('r'))
finally:
connection.close()
if not response['status']=='success':
if 'error' in response:
response = response['error']
raise APIError(response)
return response['response']