-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuisp_client.py
71 lines (55 loc) · 2.18 KB
/
uisp_client.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
import os
import requests
import json
class UISPClient:
def __init__(self):
self.headers = {"x-auth-token": os.getenv("UISP_AUTH_TOKEN")}
self.endpoint = os.getenv("UISP_ENDPOINT")
if not self.endpoint or not self.headers:
raise ValueError("Missing environment variables.")
def get_devices(self):
response = requests.get(
f"{self.endpoint}/devices", headers=self.headers, verify=False
)
devices = json.loads(response.content)
if devices == []:
raise ValueError("Problem downloading UISP devices.")
return devices
def get_stats(self, device_id):
# Available interval values : hour, fourhours, day, week, month, quarter, year, range
endpoint = f"{self.endpoint}/devices/{device_id}/statistics"
params = {
# "start":int(time.time() * 1000),
# "period":int(11*60*1000),
"interval": "hour",
}
response = requests.get(
endpoint, headers=self.headers, params=params, verify=False
)
history = json.loads(response.content)
return history
def get_data_links(self, filter=False):
response = requests.get(
f"{self.endpoint}/data-links", headers=self.headers, verify=False
)
data_links = json.loads(response.content)
if data_links == []:
raise ValueError("Problem downloading UISP data_links.")
if filter:
return self._filter_data_links(data_links)
return data_links
# TODO: For now, this software is only interested in Point to Point links
def _filter_data_links(self, data_links):
filtered_data_links = []
for l in data_links:
wireless_modes = ("ap-ptp", "sta-ptp")
if (
l["from"]["device"]["overview"]["wirelessMode"] not in wireless_modes
or l["to"]["device"]["overview"]["wirelessMode"] not in wireless_modes
):
continue
# If the SSID doesn't exist, bail.
if l["ssid"] is None:
continue
filtered_data_links.append(l)
return filtered_data_links