Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Parses the response as json, then runs check_response and
add_jikan_metadata
"""
json_response: Dict[str, Any] = {}
try:
json_response = response.json()
if not isinstance(json_response, dict):
json_response = {"data": json_response}
except (json.decoder.JSONDecodeError, simplejson.JSONDecodeError):
# json failed to be parsed
# this could happen, for example, when someone has been IP banned
# and it returns the typical nginx 403 forbidden page
json_response = {"error": response.text}
if response.status_code >= 400:
raise APIException(response.status_code, json_response, **kwargs)
return utils.add_jikan_metadata(response, json_response, url)
url: str,
**kwargs: Union[int, Optional[str]],
) -> Dict[str, Any]:
"""Parses the response as json, then runs check_response and
add_jikan_metadata
"""
json_response: Dict[str, Any] = {}
try:
json_response = await response.json()
if not isinstance(json_response, dict):
json_response = {"data": json_response}
except (json.decoder.JSONDecodeError, simplejson.JSONDecodeError):
json_response = {"error": await response.text()}
if response.status >= 400:
raise APIException(response.status, json_response, **kwargs)
return utils.add_jikan_metadata(response, json_response, url)