in hugegraph-python-client/src/pyhugegraph/utils/util.py [0:0]
def __call__(self, response: requests.Response, method: str, path: str):
"""
Validate the HTTP response according to the provided content type and strictness.
:param response: HTTP response object
:param method: HTTP method used (e.g., 'GET', 'POST')
:param path: URL path of the request
:return: Parsed response content or empty dict if none applicable
"""
result = {}
try:
response.raise_for_status()
if response.status_code == 204:
log.debug("No content returned (204) for %s: %s", method, path)
else:
if self._content_type == "raw":
result = response
elif self._content_type == "json":
result = response.json()
elif self._content_type == "text":
result = response.text
else:
raise ValueError(f"Unknown content type: {self._content_type}")
except requests.exceptions.HTTPError as e:
if not self._strict and response.status_code == 404:
log.info("Resource %s not found (404)", path)
else:
try:
details = response.json().get("exception", "key 'exception' not found")
except (ValueError, KeyError):
details = "key 'exception' not found"
req_body = response.request.body if response.request.body else "Empty body"
req_body = req_body.encode('utf-8').decode('unicode_escape')
log.error("%s: %s\n[Body]: %s\n[Server Exception]: %s",
method, str(e).encode('utf-8').decode('unicode_escape'), req_body, details)
if response.status_code == 404:
raise NotFoundError(response.content) from e
if response.status_code == 400:
raise Exception(f"Server Exception: {details}") from e
raise e
except Exception: # pylint: disable=broad-exception-caught
log.error("Unhandled exception occurred: %s", traceback.format_exc())
return result