From 49df7b423ca50f88c449ff856290fdd068369ddc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 26 Aug 2023 01:31:52 +0200 Subject: [PATCH] Continued: - I hope this isn't to strict, some hosts return a "298 None" which the HTTP library doesn't see as failed (response.ok = False) but still doesn't return a JSON --- daemon.py | 2 +- fba/commands.py | 4 ++-- fba/helpers/json.py | 2 +- fba/http/network.py | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/daemon.py b/daemon.py index 53a50f6..9f96e11 100755 --- a/daemon.py +++ b/daemon.py @@ -427,7 +427,7 @@ def infos(request: Request, domain: str): response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/domain.json?domain={domain}") - if not response.ok or response.status_code >= 300 or response.text.strip() == "": + if not response.ok or response.status_code > 200 or response.text.strip() == "": raise HTTPException(status_code=response.status_code, detail=response.reason) domain_data = response.json() diff --git a/fba/commands.py b/fba/commands.py index e21a057..e1ffd37 100644 --- a/fba/commands.py +++ b/fba/commands.py @@ -1018,7 +1018,7 @@ def fetch_oliphant(args: argparse.Namespace) -> int: response = utils.fetch_url(f"{base_url}/{block['csv_url']}", network.web_headers, (config.get("connection_timeout"), config.get("read_timeout"))) logger.debug("response.ok='%s',response.status_code=%d,response.content()=%d", response.ok, response.status_code, len(response.content)) - if not response.ok or response.status_code >= 300 or response.content == "": + if not response.ok or response.status_code > 200 or response.content == "": logger.warning("Could not fetch csv_url='%s' for blocker='%s' - SKIPPED!", block["csv_url"], block["blocker"]) continue @@ -1662,7 +1662,7 @@ def fetch_fedilist(args: argparse.Namespace) -> int: ) logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text)) - if not response.ok or response.status_code >= 300 or len(response.content) == 0: + if not response.ok or response.status_code > 200 or len(response.content) == 0: logger.warning("Failed fetching url='%s': response.ok='%s',response.status_code=%d,response.content()=%d - EXIT!", url, response.ok, response.status_code, len(response.text)) return 1 diff --git a/fba/helpers/json.py b/fba/helpers/json.py index 9a86854..d20a2c7 100644 --- a/fba/helpers/json.py +++ b/fba/helpers/json.py @@ -26,7 +26,7 @@ def from_response(response: requests.models.Response) -> any: logger.debug("response[]='%s' - CALLED!", type(response)) if not isinstance(response, requests.models.Response): raise ValueError(f"Parameter response[]='{type(response)}' is not type of 'Response'") - elif not response.ok or response.status_code >= 300: + elif not response.ok or response.status_code > 200: raise ValueError(f"response.ok='{response.ok}',response.status_code={response.status_code},response.reason='{response.reason}' but function was invoked") elif response.text.strip() != "" and response.headers.get("content-type") is not None and response.headers.get("content-type").split(";")[0] not in ["*/*", "application/json", "application/jrd+json", "application/activity+json"]: logger.warning("response.headers[content-type]='%s' is not a JSON type, below json() invocation may raise an exception", response.headers.get("content-type")) diff --git a/fba/http/network.py b/fba/http/network.py index e7819f6..b16cd15 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -90,7 +90,7 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = dict() json_reply["json"] = json_helper.from_response(response) logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text)) - if not response.ok or response.status_code >= 300 or len(response.text.strip()) == 0: + if not response.ok or response.status_code > 200 or len(response.text.strip()) == 0: logger.debug("Cannot query JSON API: domain='%s',path='%s',data()=%d,response.status_code=%d,response.text()=%d", domain, path, len(data), response.status_code, len(response.text)) json_reply["status_code"] = response.status_code json_reply["error_message"] = response.reason @@ -130,7 +130,7 @@ def fetch_api_url(url: str, timeout: tuple) -> dict: json_reply["json"] = json_helper.from_response(response) logger.debug("response.ok='%s',response.status_code='%s',response.text()=%d", response.ok, response.status_code, len(response.text)) - if not response.ok or response.status_code >= 300 or len(response.text) == 0: + if not response.ok or response.status_code > 200 or len(response.text) == 0: logger.warning("Cannot query JSON API: url='%s',response.status_code=%d,response.text()=%d", url, response.status_code, len(response.text)) json_reply["status_code"] = response.status_code json_reply["error_message"] = response.reason @@ -179,7 +179,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: json_reply["json"] = json_helper.from_response(response) logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text)) - if not response.ok or response.status_code >= 300 or len(response.text) == 0: + if not response.ok or response.status_code > 200 or len(response.text) == 0: logger.debug("Cannot query JSON API: domain='%s',path='%s',response.status_code=%d,response.text()=%d", domain, path, response.status_code, len(response.text)) json_reply["status_code"] = response.status_code json_reply["error_message"] = response.reason -- 2.39.5