-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.py
82 lines (59 loc) · 2.2 KB
/
lib.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
from json.decoder import JSONDecodeError
from typing import Dict
import requests
class InvalidResponseError(Exception):
pass
class ErrorReceived(Exception):
pass
def send_cmd(url: str, secret_key: str, cmd: str) -> Dict:
"""Send a command to the endpoint and return the response."""
data = {"secret_key": secret_key, "command": cmd}
try:
response = requests.post(url, json=data, verify=False, timeout=5)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
code = e.response.status_code
if code == 423 or code == 401:
error_message = e.response.json().get("error")
elif code == 504:
error_message = e.response.content.decode("UTF-8")
elif code == 404:
error_message = "Endpoint URL not found"
elif code == 500:
error_message = "Internal SL server error"
else:
raise e
e.args = (error_message,)
raise e
try:
response_data = response.json()
except JSONDecodeError as e:
e.args = ("Response contains invalid json",)
raise e
error = response_data.get("error", None)
if error:
raise ErrorReceived(error)
return response_data
def connect(url: str, secret_key: str) -> str:
"""Connect to the given URL.
Returns the UUID of the endpoint."""
uuid = send_cmd(url, secret_key, "connect").get("uuid", None)
if not uuid:
raise InvalidResponseError("Endpoint did not return its UUID")
return uuid
def disconnect(url: str, secret_key: str) -> bool:
"""Disconnect from the endpoint.
Returns True if the endpoint responded with an acknlowledgement."""
try:
result = send_cmd(url, secret_key, "disconnect").get("result", None)
if result == "disconnected":
return True
except JSONDecodeError:
pass
return False
def get_available_commands(url: str, secret_key: str) -> Dict:
"""Get a list of available commands from the endpoint."""
cmds = send_cmd(url, secret_key, "get_commands").get("available_commands", None)
if not cmds:
raise InvalidResponseError("Endpoint did not return command list")
return cmds