]> git.mxchange.org Git - fba.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 13 Aug 2023 13:53:13 +0000 (15:53 +0200)
committerRoland Häder <roland@mxchange.org>
Sun, 13 Aug 2023 13:53:13 +0000 (15:53 +0200)
- no need to enter json.from_response() when response isn't okay or status code
  != 200 was found

fba/helpers/json.py
fba/http/network.py

index 0905341ed118a48c95843dc4512c934d40d29d8b..9a86854749b81b5b305aa2ac98df50976919a9af 100644 (file)
@@ -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"))
 
index 3a72ff3e4be895868ece575e7b47a34c622c44b9..2b1a9bbac8a2b3ee29c6d5db5e59a244dbd68c50 100644 (file)
@@ -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