Ejemplo n.º 1
0
    def make_request(self, request, captcha_response=None):
        """Make api request helper function

        :param request: vk_requests.api.Request instance
        :param captcha_response: None or dict, e.g {'sid': <sid>, 'key': <key>}
        :return: dict: json decoded http response
        """
        logger.debug('Prepare API Method request %r', request)
        response = self._send_api_request(request=request,
                                          captcha_response=captcha_response)
        response.raise_for_status()
        response_or_error = json.loads(response.text)
        logger.debug('response: %s', response_or_error)

        if 'error' in response_or_error:
            error_data = response_or_error['error']
            vk_error = VkAPIError(error_data)

            if vk_error.is_captcha_needed():
                captcha_key = self.get_captcha_key(vk_error.captcha_img_url)
                if not captcha_key:
                    raise vk_error

                # Retry http request with captcha info attached
                captcha_response = {
                    'sid': vk_error.captcha_sid,
                    'key': captcha_key,
                }
                return self.make_request(request,
                                         captcha_response=captcha_response)

            elif vk_error.is_access_token_incorrect():
                logger.info(
                    'Authorization failed. Access token will be dropped')
                self._access_token = None
                return self.make_request(request)

            else:
                raise vk_error
        elif 'execute_errors' in response_or_error:
            # can take place while running .execute vk method
            # See more: https://vk.com/dev/execute
            raise VkAPIError(response_or_error['execute_errors'][0])
        elif 'response' in response_or_error:
            return response_or_error['response']
Ejemplo n.º 2
0
    def make_request(self, request_obj, captcha_response=None):
        logger.debug('Prepare API Method request %r', request_obj)
        response = self.send_api_request(request=request_obj,
                                         captcha_response=captcha_response)
        response.raise_for_status()

        # there are may be 2 dicts in one JSON
        # for example: "{'error': ...}{'response': ...}"
        for response_or_error in json_iter_parse(response.text):
            if 'error' in response_or_error:
                error_data = response_or_error['error']
                vk_error = VkAPIError(error_data)

                if vk_error.is_captcha_needed():
                    captcha_key = self.auth_api.get_captcha_key(
                        vk_error.captcha_img)

                    if not captcha_key:
                        raise vk_error

                    captcha_response = {
                        'sid': vk_error.captcha_sid,
                        'key': captcha_key,
                    }
                    return self.make_request(request_obj,
                                             captcha_response=captcha_response)

                elif vk_error.is_access_token_incorrect():
                    logger.info(
                        'Authorization failed. Access token will be dropped')
                    self.access_token = None
                    return self.make_request(request_obj)

                else:
                    raise vk_error
            elif 'execute_errors' in response_or_error:
                # can take place while running .execute vk method
                # See more: https://vk.com/dev/execute
                raise VkAPIError(response_or_error['execute_errors'][0])
            elif 'response' in response_or_error:
                return response_or_error['response']
Ejemplo n.º 3
0
    def make_request(self, request_obj, captcha_response=None):
        logger.debug('Prepare API Method request %r', request_obj)
        response = self.send_api_request(request=request_obj,
                                         captcha_response=captcha_response)
        response.raise_for_status()

        # there are may be 2 dicts in one JSON
        # for example: "{'error': ...}{'response': ...}"
        for response_or_error in json_iter_parse(response.text):
            if 'error' in response_or_error:
                error_data = response_or_error['error']
                vk_error = VkAPIError(error_data)

                if vk_error.is_captcha_needed():
                    captcha_key = self.auth_api.get_captcha_key(
                        vk_error.captcha_img)

                    if not captcha_key:
                        raise vk_error

                    captcha_response = {
                        'sid': vk_error.captcha_sid,
                        'key': captcha_key,
                    }
                    return self.make_request(
                        request_obj, captcha_response=captcha_response)

                elif vk_error.is_access_token_incorrect():
                    logger.info(
                        'Authorization failed. Access token will be dropped')
                    self.access_token = None
                    return self.make_request(request_obj)

                else:
                    raise vk_error
            elif 'execute_errors' in response_or_error:
                # can take place while running .execute vk method
                # See more: https://vk.com/dev/execute
                raise VkAPIError(response_or_error['execute_errors'][0])
            elif 'response' in response_or_error:
                return response_or_error['response']