logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
+def is_json_response(response: requests.models.Response) -> bool:
+ 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 > 200:
+ raise ValueError(f"response.ok='{response.ok}',response.status_code={response.status_code},response.reason='{response.reason}' but function was invoked")
+
+ is_json = response.headers.get("content-type") is None or response.headers.get("content-type").split(";")[0] in ["*/*", "application/json", "application/jrd+json", "application/activity+json"]
+
+ logger.debug("is_json='%s' - EXIT!", is_json)
+ return is_json
+
def from_response(response: requests.models.Response) -> any:
logger.debug("response[]='%s' - CALLED!", type(response))
raise ValueError(f"Parameter response[]='{type(response)}' is not type of 'Response'")
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"]:
+ elif response.text.strip() != "" and not is_json_response(response):
logger.warning("response.headers[content-type]='%s' is not a JSON type, below json() invocation may raise an exception", response.headers.get("content-type"))
data = list()
raise exception
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:
+ if not json_helper.is_json_response(response):
+ json_reply["status_code"] = 999
+ json_reply["error_message"] = f"content-type='{response.headers.get('content-type')}' is not a JSON response!"
+ elif 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("json_reply[json][]='%s'", type(json_reply["json"]))