Exemplo n.º 1
0
    def _on_message_finish(self, session, message, user_data=None):

        if user_data:
            logging.warning("in on_message, to handle %s" %
                            (user_data.__name__))
        else:
            print("there is no callback")

        status_code = message.props.status_code
        logging.debug('Got response code: {} ({})'.format(
            Soup.Status(status_code).value_name, status_code))
        print("status code = %d" % (status_code))

        self.wait_for_response = False

        if status_code == Soup.Status.UNAUTHORIZED:
            if not self.login or not self.password:
                self.emit('auth-failed')
                logging.warning('Requires login and password')
            else:
                self.emit('auth-failed')
                logging.warning('Failed to authenticate using {}'.format(
                    self.login))

        response_str = message.props.response_body_data.get_data().decode(
            'UTF-8')

        if not 200 <= status_code < 300:
            logging.warning('Response was not successful')
            print("output is:%s" % (response_str))
            return

        print("the json response is %s" % response_str)
        response = json.loads(response_str)

        #        logging.debug('<<<\n{}'.format(pprint.pformat(response)))
        if user_data:
            user_data(response)

        print("now checking to see if we have something else to send",
              self.sendqueue)

        # after we come back from handling the callback, we want to check if we have
        # something we have to send.

        if self.sendqueue and self.wait_for_response is False:  # check if we have anything in our sendqueue
            [method, url, content, query_params, headers,
             callback] = self.sendqueue.pop()
            print("making the call with:", method, url, content, query_params,
                  headers, callback)
            self._make_request_async(method,
                                     url,
                                     content,
                                     query_params={},
                                     headers={},
                                     callback=None)
Exemplo n.º 2
0
 def new_from_message(cls, soup_message: Soup.Message):
     data = soup_message.props.response_body_data.get_data()
     if data:
         data = json.loads(data.decode('utf-8'))
         message = data['message']
         error_code = data['errorCode']
         error_message = data['errorString']
     else:
         message = 'HTTP Error'
         error_code = soup_message.props.status_code
         error_message = Soup.Status(error_code).value_name
     return cls(message, error_code, error_message)
Exemplo n.º 3
0
    def _on_message_finish(self, session, message, user_data=None):
        status_code = message.props.status_code
        logging.debug('Got response code: {} ({})'.format(
            Soup.Status(status_code).value_name, status_code))

        if status_code == Soup.Status.UNAUTHORIZED:
            if not self.username or not self.password:
                logging.warning('Requires authentication')
            else:
                logging.warning('Failed to log in as {}'.format(self.username))
        elif status_code == Soup.Status.CONFLICT:
            self._session_id = message.props.response_headers.get(
                'X-Transmission-Session-Id')
            logging.info('Got new session id ({}), retrying'.format(
                self._session_id))
            message.props.request_headers.replace('X-Transmission-Session-Id',
                                                  self._session_id)
            # requeue_message fails?
            self._session.cancel_message(message, Soup.Status.CANCELLED)
            self._session.queue_message(message,
                                        self._on_message_finish,
                                        user_data=user_data)

        if not 200 <= status_code < 300:
            logging.warning('Response was not successful: {} ({})'.format(
                Soup.Status(status_code).value_name, status_code))
            return

        response_str = message.props.response_body_data.get_data().decode(
            'UTF-8')
        response = json.loads(response_str)
        logging.debug('<<<\n{}'.format(pprint.pformat(response)))

        if response.get('result') != 'success':
            logging.warning('Request failed: {}'.format(
                response.get('result')))
            return

        if user_data:
            user_data(response)