From d246c540abb052b329fb23f83bd0474dfaa21e71 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 23 Jun 2023 21:07:52 +0200 Subject: [PATCH] Continued: - paths is not a dict, it must be a list (ops) - some JSON APIs don't response with a list of peers, but a dict instead --- fba/helpers/domain.py | 2 +- fba/http/federation.py | 12 ++++++++---- fba/models/instances.py | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/fba/helpers/domain.py b/fba/helpers/domain.py index ac32cf1..2606f14 100644 --- a/fba/helpers/domain.py +++ b/fba/helpers/domain.py @@ -22,7 +22,7 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def raise_on(domain: str): - logger.debug("domain='%s' - CALLED!") + logger.debug("domain='%s' - CALLED!", domain) if not isinstance(domain, str): raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'") elif domain == "": diff --git a/fba/http/federation.py b/fba/http/federation.py index fdaec59..ee2fadc 100644 --- a/fba/http/federation.py +++ b/fba/http/federation.py @@ -152,10 +152,10 @@ def fetch_peers(domain: str, software: str) -> list: instances.set_last_error(domain, exception) return list() - paths = { + paths = [ "/api/v1/instance/peers", "/api/v3/site", - } + ] # Init peers variable peers = list() @@ -175,11 +175,15 @@ def fetch_peers(domain: str, software: str) -> list: logger.debug("Was not able to fetch peers from path='%s',domain='%s' ...", path, domain) instances.set_last_error(domain, data) elif "json" in data and len(data["json"]) > 0: - logger.debug("Querying API path='%s' was successful: domain='%s',data[json]()=%d", path, domain, len(data['json'])) + logger.debug("Querying API path='%s' was successful: domain='%s',data[json][%s]()=%d", path, domain, type(data['json']), len(data['json'])) peers = data["json"] instances.set_success(domain) break + if not isinstance(peers, list): + logger.warning("peers[]='%s' is not 'list', maybe bad API response?", type(peers)) + peers = list() + logger.debug("Invoking instances.set_total_peers(%s,%d) ...", domain, len(peers)) instances.set_total_peers(domain, peers) @@ -223,7 +227,7 @@ def fetch_nodeinfo(domain: str, path: str = None) -> dict: "/nodeinfo/2.0.json", "/nodeinfo/2.0", "/nodeinfo/1.0", - "/api/v1/instance" + "/api/v1/instance", ] for request in request_paths: diff --git a/fba/models/instances.py b/fba/models/instances.py index 3ba561f..8c977fe 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -366,7 +366,7 @@ def set_total_peers(domain: str, peers: list): domain_helper.raise_on(domain) if not isinstance(peers, list): - raise ValueError(f"Parameter peers[]='{type(peers)}' is not 'list'") + raise ValueError(f"Parameter peers[]='{type(peers)}' is not 'list': '%s'") # Set timestamp _set_data("total_peers", domain, len(peers)) -- 2.39.5