Exemple #1
0
 def _check_response(self, response, **kwargs):
     """Overrides _check_response in AbstractJikan"""
     if response.status >= 400:
         json = yield from response.json()
         err_str = '{} {}: error for '.format(response.status,
                                              json.get('error'))
         err_str += ', '.join('='.join((str(k), str(v)))
                              for k, v in kwargs.items())
         raise APIException(err_str)
Exemple #2
0
 async def _check_response(  # type: ignore
         self, response: Any, **kwargs: Union[int, Optional[str]]) -> None:
     """Overrides _check_response in AbstractJikan"""
     if response.status >= 400:
         try:
             json_resp = await response.json()
             error_msg = json_resp.get("error")
         except json.decoder.JSONDecodeError:
             error_msg = ""
         err_str: str = "{} {}: error for ".format(response.status,
                                                   error_msg)
         err_str += ", ".join("=".join((str(k), str(v)))
                              for k, v in kwargs.items())
         raise APIException(err_str)
Exemple #3
0
    def _check_response(self, response, **kwargs):
        """
        Check if the response is an error

        Keyword Arguments:
        response -- response from the API call
        kwargs -- keyword arguments
        """
        if response.status_code >= 400:
            err_str = '{} {}: error for '.format(response.status_code,
                                                 response.json().get('error'))
            err_str += ', '.join('='.join((str(k), str(v)))
                                 for k, v in kwargs.items())
            raise APIException(err_str)
Exemple #4
0
    def _get(self, endpoint, id, extension):
        url = self.base.format(endpoint=endpoint, id=id)
        if extension is not None:
            if extension not in EXTENSIONS[endpoint]:
                raise ClientException
            url += '/' + extension

        response = session.get(url)
        if response.status_code >= 400:
            err_str = '{}: error for id {} on endpoint {}'.format(
                response.status_code, id, endpoint)
            raise APIException(err_str)

        return response.json()
Exemple #5
0
 def _wrap_response(response: requests.Response, 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 = 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)
Exemple #6
0
 async def _wrap_response(
     self,
     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)
    def _check_response(
        self, response: Any, **kwargs: Union[int, Optional[str]]
    ) -> None:
        """
        Check if the response is an error

        Keyword Arguments:
        response -- response from the API call
        kwargs -- keyword arguments
        """
        if response.status_code >= 400:
            try:
                json_resp = response.json()
                error_msg = json_resp.get("error")
            except json.decoder.JSONDecodeError:
                error_msg = ""
            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)
Exemple #8
0
    def _check_response(
        self,
        response_dict: Dict,
        response_status_code: int,
        **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)