Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
) -> Dict[str, Any]:
"""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)
response: aiohttp.ClientResponse,
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)
**kwargs: Union[int, Optional[str]],
) -> None:
"""
Check if the response is an error
Keyword Arguments:
response_dict -- parsed response from the API call
is empty ({}) when there was a json.decoder.JSONDecodeError
response_status -- the corresponding http code for the response
kwargs -- keyword arguments
"""
if response_status_code >= 400:
error_msg = response_dict.get("error", "")
err_str: str = "{} {}: error for ".format(response_status_code, error_msg)
err_str += ", ".join("=".join((str(k), str(v))) for k, v in kwargs.items())
raise APIException(err_str)