Merge pull request #1 from ivanpavlina/dev

Add ability to return list results of API call instead of generator. …
This commit is contained in:
Ivan Pavlina 2020-04-04 17:55:14 +02:00 committed by GitHub
commit c06c8e6da2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 41 deletions

View file

@ -57,6 +57,8 @@ def parse_api(
data = fill_defaults(data, vals) data = fill_defaults(data, vals)
return data return data
_LOGGER.debug("Processing source %s", source)
keymap = generate_keymap(data, key_search) keymap = generate_keymap(data, key_search)
for entry in source: for entry in source:
if only and not matches_only(entry, only): if only and not matches_only(entry, only):
@ -74,7 +76,7 @@ def parse_api(
if uid not in data: if uid not in data:
data[uid] = {} data[uid] = {}
_LOGGER.debug("Processing entry %s, entry %s", source, entry) _LOGGER.debug("Processing entry %s", entry)
if vals: if vals:
data = fill_vals(data, entry, uid, vals) data = fill_vals(data, entry, uid, vals)

View file

@ -229,7 +229,7 @@ class MikrotikControllerData:
"""Get all interfaces data from Mikrotik""" """Get all interfaces data from Mikrotik"""
self.data["interface"] = parse_api( self.data["interface"] = parse_api(
data=self.data["interface"], data=self.data["interface"],
source=self.api.path("/interface"), source=self.api.path("/interface", return_list=True),
key="default-name", key="default-name",
vals=[ vals=[
{"name": "default-name"}, {"name": "default-name"},
@ -331,7 +331,7 @@ class MikrotikControllerData:
# --------------------------- # ---------------------------
def update_arp(self, mac2ip, bridge_used): def update_arp(self, mac2ip, bridge_used):
"""Get list of hosts in ARP for interface client data from Mikrotik""" """Get list of hosts in ARP for interface client data from Mikrotik"""
data = self.api.path("/ip/arp") data = self.api.path("/ip/arp", return_list=True)
if not data: if not data:
return mac2ip, bridge_used return mac2ip, bridge_used
@ -382,7 +382,7 @@ class MikrotikControllerData:
# --------------------------- # ---------------------------
def update_bridge_hosts(self, mac2ip): def update_bridge_hosts(self, mac2ip):
"""Get list of hosts in bridge for interface client data from Mikrotik""" """Get list of hosts in bridge for interface client data from Mikrotik"""
data = self.api.path("/interface/bridge/host") data = self.api.path("/interface/bridge/host", return_list=True)
if not data: if not data:
return return
@ -437,7 +437,7 @@ class MikrotikControllerData:
"""Get NAT data from Mikrotik""" """Get NAT data from Mikrotik"""
self.data["nat"] = parse_api( self.data["nat"] = parse_api(
data=self.data["nat"], data=self.data["nat"],
source=self.api.path("/ip/firewall/nat"), source=self.api.path("/ip/firewall/nat", return_list=True, return_list=True),
key=".id", key=".id",
vals=[ vals=[
{"name": ".id"}, {"name": ".id"},
@ -473,7 +473,7 @@ class MikrotikControllerData:
"""Get routerboard data from Mikrotik""" """Get routerboard data from Mikrotik"""
self.data["routerboard"] = parse_api( self.data["routerboard"] = parse_api(
data=self.data["routerboard"], data=self.data["routerboard"],
source=self.api.path("/system/routerboard"), source=self.api.path("/system/routerboard", return_list=True),
vals=[ vals=[
{"name": "routerboard", "type": "bool"}, {"name": "routerboard", "type": "bool"},
{"name": "model", "default": "unknown"}, {"name": "model", "default": "unknown"},
@ -489,7 +489,7 @@ class MikrotikControllerData:
"""Get system resources data from Mikrotik""" """Get system resources data from Mikrotik"""
self.data["resource"] = parse_api( self.data["resource"] = parse_api(
data=self.data["resource"], data=self.data["resource"],
source=self.api.path("/system/resource"), source=self.api.path("/system/resource", return_list=True),
vals=[ vals=[
{"name": "platform", "default": "unknown"}, {"name": "platform", "default": "unknown"},
{"name": "board-name", "default": "unknown"}, {"name": "board-name", "default": "unknown"},
@ -538,7 +538,7 @@ class MikrotikControllerData:
"""Check for firmware update on Mikrotik""" """Check for firmware update on Mikrotik"""
self.data["fw-update"] = parse_api( self.data["fw-update"] = parse_api(
data=self.data["fw-update"], data=self.data["fw-update"],
source=self.api.path("/system/package/update"), source=self.api.path("/system/package/update", return_list=True),
vals=[ vals=[
{"name": "status"}, {"name": "status"},
{"name": "channel", "default": "unknown"}, {"name": "channel", "default": "unknown"},
@ -563,7 +563,7 @@ class MikrotikControllerData:
"""Get list of all scripts from Mikrotik""" """Get list of all scripts from Mikrotik"""
self.data["script"] = parse_api( self.data["script"] = parse_api(
data=self.data["script"], data=self.data["script"],
source=self.api.path("/system/script"), source=self.api.path("/system/script", return_list=True),
key="name", key="name",
vals=[ vals=[
{"name": "name"}, {"name": "name"},
@ -580,7 +580,7 @@ class MikrotikControllerData:
"""Get Queue data from Mikrotik""" """Get Queue data from Mikrotik"""
self.data["queue"] = parse_api( self.data["queue"] = parse_api(
data=self.data["queue"], data=self.data["queue"],
source=self.api.path("/queue/simple"), source=self.api.path("/queue/simple", return_list=True),
key="name", key="name",
vals=[ vals=[
{"name": ".id"}, {"name": ".id"},

View file

@ -167,8 +167,9 @@ class MikrotikAPI:
# --------------------------- # ---------------------------
# path # path
# --------------------------- # ---------------------------
def path(self, path) -> Optional(list): def path(self, path, return_list=False) -> Optional(list):
"""Retrieve data from Mikrotik API.""" """Retrieve data from Mikrotik API."""
"""Returns generator object, unless return_list passed as True"""
if not self._connected or not self._connection: if not self._connected or not self._connection:
if self._connection_epoch > time.time() - self._connection_retry_sec: if self._connection_epoch > time.time() - self._connection_retry_sec:
return None return None
@ -189,18 +190,17 @@ class MikrotikAPI:
self.lock.release() self.lock.release()
return None return None
except ( except (
librouteros_custom.exceptions.TrapError, librouteros_custom.exceptions.TrapError,
librouteros_custom.exceptions.MultiTrapError, librouteros_custom.exceptions.MultiTrapError,
librouteros_custom.exceptions.ProtocolError, librouteros_custom.exceptions.ProtocolError,
librouteros_custom.exceptions.FatalError, librouteros_custom.exceptions.FatalError,
ssl.SSLError, ssl.SSLError,
BrokenPipeError, BrokenPipeError,
OSError, OSError,
ValueError, ValueError,
) as api_error: ) as api_error:
if not self.connection_error_reported: if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, _LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
api_error)
self.connection_error_reported = True self.connection_error_reported = True
self.disconnect() self.disconnect()
@ -208,34 +208,32 @@ class MikrotikAPI:
return None return None
except: except:
if not self.connection_error_reported: if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, _LOGGER.error("Mikrotik %s error while path %s", self._host, "unknown")
"unknown")
self.connection_error_reported = True self.connection_error_reported = True
self.disconnect() self.disconnect()
self.lock.release() self.lock.release()
return None return None
try: if return_list:
tuple(response) try:
except librouteros_custom.exceptions.ConnectionClosed as api_error: response = list(response)
if not self.connection_error_reported: except librouteros_custom.exceptions.ConnectionClosed as api_error:
_LOGGER.error("Mikrotik %s error while path %s", self._host, if not self.connection_error_reported:
api_error) _LOGGER.error("Mikrotik %s error while building list for path %s", self._host, api_error)
self.connection_error_reported = True self.connection_error_reported = True
self.disconnect() self.disconnect()
self.lock.release() self.lock.release()
return None return None
except: except:
if not self.connection_error_reported: if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, _LOGGER.error("Mikrotik %s error while building list for path %s", self._host, "unknown")
"unknown") self.connection_error_reported = True
self.connection_error_reported = True
self.disconnect() self.disconnect()
self.lock.release() self.lock.release()
return None return None
self.lock.release() self.lock.release()
return response if response else None return response if response else None