Skip to content

Commit bd1866c

Browse files
authored
Merge branch 'master' into vageli/flake8
2 parents 83e1d9d + 0e5a0a0 commit bd1866c

File tree

2 files changed

+150
-9
lines changed

2 files changed

+150
-9
lines changed

setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from setuptools import setup
2+
3+
setup(name='ubiquiti',
4+
version='0.2',
5+
description='Library for interacting with Ubiquity Cloudkey',
6+
url='http://github.com/vageli/Unifi-Python-API',
7+
author='vagelim',
8+
author_email='code@evangelosm.com',
9+
license='MIT',
10+
packages=['ubiquiti'],
11+
zip_safe=False)

ubiquiti/unifi.py

Lines changed: 139 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@ def __init__(self, username: str="ubnt", password: str="ubnt", site: str="defaul
3737

3838
def __enter__(self):
3939
"""
40-
Contextmanager entry handle
40+
Contextmanager entry handle.
4141
42-
:return: isntance object of class
42+
:return: instance object of class
4343
"""
4444
self.login()
4545
return self
4646

4747
def __exit__(self, *args):
4848
"""
49-
Contextmanager exit handle
49+
Contextmanager exit handle.
5050
5151
:return: None
5252
"""
5353
self.logout()
5454

5555
def login(self):
5656
"""
57-
Log the user in
57+
Log the user in.
5858
5959
:return: None
6060
"""
@@ -64,7 +64,7 @@ def login(self):
6464

6565
def logout(self):
6666
"""
67-
Log the user out
67+
Log the user out.
6868
6969
:return: None
7070
"""
@@ -73,16 +73,146 @@ def logout(self):
7373

7474
def list_clients(self, filters: Dict[str, Union[str, Pattern]]=None, order_by: str=None) -> list:
7575
"""
76-
List all available clients from the api
76+
List all available clients from the api.
7777
78-
:param filters: dict with valid key, value pairs, string supplied is compiled to a regular expression
79-
:param order_by: order by a valid client key, defaults to '_id' if key is not found
78+
:param filters: dict of k/v pairs; string is compiled to regex
79+
:param order_by: order by a key; defaults to '_id'
8080
:return: A list of clients on the format of a dict
8181
"""
8282

8383
r = self._session.get("{}/api/s/{}/stat/sta".format(self._baseurl, self._site, verify=self._verify_ssl), data="json={}")
8484
self._current_status_code = r.status_code
85-
85+
86+
if self._current_status_code == 401:
87+
raise LoggedInException("Invalid login, or login has expired")
88+
89+
data = r.json()['data']
90+
91+
if filters:
92+
for term, value in filters.items():
93+
value_re = value if isinstance(value, Pattern) else re.compile(value)
94+
95+
data = [x for x in data if term in x.keys() and re.fullmatch(value_re, x[term])]
96+
97+
if order_by:
98+
data = sorted(data, key=lambda x: x[order_by] if order_by in x.keys() else x['_id'])
99+
100+
return data
101+
102+
def health(self) -> dict:
103+
"""
104+
List site health information.
105+
:return: A dict of network health information (see below)
106+
num_adopted
107+
num_ap
108+
num_disabled
109+
num_disconnected
110+
num_guest
111+
num_iot
112+
num_pending
113+
num_user
114+
rx_bytes-r
115+
status
116+
subsystem
117+
tx_bytes-r
118+
"""
119+
r = self._session.get("{}/api/s/{}/stat/health".format(self._baseurl, self._site, verify=False), data="json={}")
120+
self._current_status_code = r.status_code
121+
if self._current_status_code == 401:
122+
raise LoggedInException("Invalid login, or login has expired")
123+
124+
data = r.json()['data']
125+
126+
return data[0]
127+
128+
def info(self) -> dict:
129+
"""
130+
List site information.
131+
:return: A dict of site information (see below for a sample)
132+
autobackup
133+
build
134+
cloudkey_update_version
135+
cloudkey_version
136+
data_retention_days
137+
debug_system
138+
eol_pending_device_count
139+
hostname
140+
https_port
141+
inform_port
142+
ip_addrs
143+
name
144+
timezone
145+
unifi_go_enabled
146+
update_available
147+
version
148+
"""
149+
r = self._session.get("{}/api/s/{}/stat/sysinfo".format(self._baseurl, self._site, verify=False), data="json={}")
150+
self._current_status_code = r.status_code
151+
if self._current_status_code == 401:
152+
raise LoggedInException("Invalid login, or login has expired")
153+
154+
data = r.json()['data']
155+
156+
return data[0]
157+
158+
def events(self, filters: Dict[str, Union[str, Pattern]]=None, order_by: str=None) -> list:
159+
"""
160+
List site events.
161+
162+
:param filters: dict of k/v pairs; string is compiled to regex
163+
:param order_by: order by a key; defaults to '_id'
164+
:return: A list of events as dicts (see below for sample keys)
165+
app_proto
166+
datetime
167+
dest_ip
168+
dest_port
169+
event_type
170+
host
171+
key
172+
msg
173+
proto
174+
site_id
175+
src_ip
176+
src_mac
177+
src_port
178+
srcipCountry
179+
subsystem
180+
time
181+
"""
182+
r = self._session.get("{}/api/s/{}/stat/event".format(self._baseurl, self._site, verify=self._verify_ssl), data="json={}")
183+
self._current_status_code = r.status_code
184+
185+
if self._current_status_code == 401:
186+
raise LoggedInException("Invalid login, or login has expired")
187+
188+
data = r.json()['data']
189+
190+
if filters:
191+
for term, value in filters.items():
192+
value_re = value if isinstance(value, Pattern) else re.compile(value)
193+
194+
data = [x for x in data if term in x.keys() and re.fullmatch(value_re, x[term])]
195+
196+
if order_by:
197+
data = sorted(data, key=lambda x: x[order_by] if order_by in x.keys() else x['_id'])
198+
199+
return data
200+
201+
def routes(self, filters: Dict[str, Union[str, Pattern]]=None, order_by: str=None) -> list:
202+
"""
203+
List site routes.
204+
205+
:param filters: dict of k/v pairs; string is compiled to regex
206+
:param order_by: order by a key; defaults to '_id'
207+
:return: A list of routes as dicts (see below for example data)
208+
nh: [{'intf': 'eth0',
209+
't': 'C>*'
210+
}]
211+
pfx: 192.168.1.0/24
212+
"""
213+
r = self._session.get("{}/api/s/{}/stat/routing".format(self._baseurl, self._site, verify=self._verify_ssl), data="json={}")
214+
self._current_status_code = r.status_code
215+
86216
if self._current_status_code == 401:
87217
raise LoggedInException("Invalid login, or login has expired")
88218

0 commit comments

Comments
 (0)