From: Roland Häder Date: Sun, 13 Aug 2023 13:53:13 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8e43697a0c349683c9ac77167131d7e668998215;p=fba.git Continued: - no need to enter json.from_response() when response isn't okay or status code != 200 was found --- diff --git a/fba/helpers/json.py b/fba/helpers/json.py index 0905341..9a86854 100644 --- a/fba/helpers/json.py +++ b/fba/helpers/json.py @@ -26,6 +26,8 @@ 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: + 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 3a72ff3..2b1a9bb 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -83,8 +83,10 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = dict() allow_redirects=False ) - logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) - json_reply["json"] = json_helper.from_response(response) + logger.debug("response.ok='%s',response.status_code=%d,response.reason='%s'", response.ok, response.status_code, response.reason) + if response.ok and response.status_code == 200: + logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) + 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: @@ -92,7 +94,6 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = dict() json_reply["status_code"] = response.status_code json_reply["error_message"] = response.reason instances.set_last_error(domain, response) - del json_reply["json"] except exceptions as exception: logger.debug("Fetching path='%s' from domain='%s' failed. exception[%s]='%s'", path, domain, type(exception), str(exception)) @@ -122,15 +123,16 @@ def fetch_api_url(url: str, timeout: tuple) -> dict: logger.debug("Fetching url='%s' ...", url) response = utils.fetch_url(url, api_headers, timeout) - logger.debug("Parsing JSON response from url='%s' ...", url) - json_reply["json"] = json_helper.from_response(response) + logger.debug("response.ok='%s',response.status_code=%d,response.reason='%s'", response.ok, response.status_code, response.reason) + if response.ok and response.status_code == 200: + logger.debug("Parsing JSON response from url='%s' ...", url) + 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: 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 - del json_reply["json"] except exceptions as exception: logger.debug("Fetching url='%s' failed. exception[%s]='%s'", url, type(exception), str(exception)) @@ -170,8 +172,10 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: instances.set_last_error(domain, exception) raise exception - logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) - json_reply["json"] = json_helper.from_response(response) + logger.debug("response.ok='%s',response.status_code=%d,response.reason='%s'", response.ok, response.status_code, response.reason) + if response.ok and response.status_code == 200: + logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) + 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: @@ -179,7 +183,6 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: json_reply["status_code"] = response.status_code json_reply["error_message"] = response.reason instances.set_last_error(domain, response) - del json_reply["json"] logger.debug("Returning json_reply(%d)[]='%s' - EXIT!", len(json_reply), type(json_reply)) return json_reply