From: Roland Häder Date: Sat, 1 Jul 2023 01:06:57 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3886e93412b151e0cb67049632515f3098f8001b;p=fba.git Continued: - need to set detection_mode and nodeinfo_url to None to avoid false instance data - setters may accept None now --- diff --git a/fba/http/federation.py b/fba/http/federation.py index b50328d..732dec2 100644 --- a/fba/http/federation.py +++ b/fba/http/federation.py @@ -450,22 +450,38 @@ def determine_software(domain: str, path: str = None) -> str: raise data["exception"] elif "error_message" in data: logger.debug("Returned error_message during fetching nodeinfo: '%s',status_code=%d", data['error_message'], data['status_code']) - return fetch_generator_from_path(domain) - elif "status" in data and data["status"] == "error" and "message" in data: - logger.warning("JSON response is an error: '%s'", data["message"]) - instances.set_last_error(domain, data["message"]) - return fetch_generator_from_path(domain) - elif "message" in data: - logger.warning("JSON response contains only a message: '%s'", data["message"]) - instances.set_last_error(domain, data["message"]) - return fetch_generator_from_path(domain) - elif "json" not in data or "software" not in data["json"] or "name" not in data["json"]["software"]: + software = fetch_generator_from_path(domain) + logger.debug("Generator for domain='%s' is: '%s'", domain, software) + elif "json" in data: + logger.debug("domain='%s',path='%s',data[json] found ...", domain, path) + if "status" in data["json"] and data["json"]["status"] == "error" and "message" in data["json"]: + logger.warning("JSON response is an error: '%s'", data["json"]["message"]) + instances.set_last_error(domain, data["json"]["message"]) + instances.set_detection_mode(domain, None) + instances.set_nodeinfo_url(domain, None) + software = fetch_generator_from_path(domain) + logger.debug("Generator for domain='%s' is: '%s'", domain, software) + elif "software" in data["json"] and "name" in data["json"]["software"]: + logger.debug("Found data[json][software][name] in JSON response") + software = data["json"]["software"]["name"] + logger.debug("software[%s]='%s' - FOUND!", type(software), software) + elif "message" in data["json"]: + logger.warning("JSON response contains only a message: '%s'", data["json"]["message"]) + instances.set_last_error(domain, data["json"]["message"]) + instances.set_detection_mode(domain, None) + instances.set_nodeinfo_url(domain, None) + software = fetch_generator_from_path(domain) + logger.debug("Generator for domain='%s' is: '%s'", domain, software) + elif "software" not in data["json"] or "name" not in data["json"]["software"]: + logger.debug("JSON response from domain='%s' does not include [software][name], fetching / ...", domain) + instances.set_detection_mode(domain, None) + instances.set_nodeinfo_url(domain, None) + software = fetch_generator_from_path(domain) + logger.debug("Generator for domain='%s' is: '%s'", domain, software) + elif "json" not in data: logger.debug("JSON response from domain='%s' does not include [software][name], fetching / ...", domain) software = fetch_generator_from_path(domain) logger.debug("Generator for domain='%s' is: '%s'", domain, software) - elif "json" in data and "software" in data["json"] and "name" in data["json"]["software"]: - logger.debug("Found data[json][software][name] in JSON response") - software = data["json"]["software"]["name"] logger.debug("software[%s]='%s'", type(software), software) if software is None: diff --git a/fba/http/network.py b/fba/http/network.py index 7081ba8..229c474 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -63,7 +63,7 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = dict() if not isinstance(path, str): raise ValueError(f"path[]='{type(path)}' is not 'str'") elif path == "": - raise ValueError("Parameter 'path' cannot be empty") + raise ValueError("Parameter 'path' is empty") elif not isinstance(data, str): raise ValueError(f"data[]='{type(data)}' is not 'str'") elif not isinstance(headers, dict): @@ -150,7 +150,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: if not isinstance(path, str): raise ValueError(f"path[]='{type(path)}' is not 'str'") elif path == "": - raise ValueError("Parameter 'path' cannot be empty") + raise ValueError("Parameter 'path' is empty") elif not isinstance(headers, dict): raise ValueError(f"headers[]='{type(headers)}' is not 'list'") elif not isinstance(timeout, tuple): diff --git a/fba/models/instances.py b/fba/models/instances.py index 25ea215..4ff0c18 100644 --- a/fba/models/instances.py +++ b/fba/models/instances.py @@ -69,7 +69,7 @@ def _set_data(key: str, domain: str, value: any): logger.debug("key='%s',domain='%s',value[]='%s' - CALLED!", key, domain, type(value)) domain_helper.raise_on(domain) if not isinstance(key, str): - raise ValueError("Parameter key[]='{type(key)}' is not 'str'") + raise ValueError(f"Parameter key[]='{type(key)}' is not 'str'") elif key == "": raise ValueError("Parameter 'key' is empty") elif not key in _pending: @@ -400,8 +400,8 @@ def set_nodeinfo_url(domain: str, url: str): logger.debug("domain='%s',url='%s' - CALLED!", domain, url) domain_helper.raise_on(domain) - if not isinstance(url, str): - raise ValueError("Parameter url[]='{type(url)}' is not 'list'") + if not isinstance(url, str) and url is not None: + raise ValueError(f"Parameter url[]='{type(url)}' is not 'str'") elif url == "": raise ValueError("Parameter 'url' is empty") @@ -413,8 +413,8 @@ def set_detection_mode(domain: str, mode: str): logger.debug("domain='%s',mode='%s' - CALLED!", domain, mode) domain_helper.raise_on(domain) - if not isinstance(mode, str): - raise ValueError("Parameter mode[]='{type(mode)}' is not 'list'") + if not isinstance(mode, str) and mode is not None: + raise ValueError(f"Parameter mode[]='{type(mode)}' is not 'str'") elif mode == "": raise ValueError("Parameter 'mode' is empty") diff --git a/fba/utils.py b/fba/utils.py index f2ff076..cd2db4d 100644 --- a/fba/utils.py +++ b/fba/utils.py @@ -39,7 +39,7 @@ logger = logging.getLogger(__name__) def is_primitive(var: any) -> bool: logger.debug("var[]='%s' - CALLED!", type(var)) - return type(var) in {int, str, float, bool} or var is None + return type(var) in {int, str, float, bool, None} or var is None def get_hash(domain: str) -> str: logger.debug("domain='%s' - CALLED!", domain)