Exemplo n.º 1
0
    def transition(self,
                   link,
                   decoders,
                   params=None,
                   link_ancestors=None,
                   force_codec=False):
        session = self._session
        method = _get_method(link.action)
        encoding = _get_encoding(link.encoding)
        params = _get_params(method, encoding, link.fields, params)
        url = _get_url(link.url, params.path)
        headers = _get_headers(url, decoders, self.credentials)
        headers.update(self.headers)

        request = _build_http_request(session, url, method, headers, encoding,
                                      params)

        if self._request_callback is not None:
            self._request_callback(request)

        response = session.send(request)

        if self._response_callback is not None:
            self._response_callback(response)

        result = _decode_result(response, decoders, force_codec)

        if isinstance(result, Document) and link_ancestors:
            result = _handle_inplace_replacements(result, link, link_ancestors)

        if isinstance(result, Error):
            raise exceptions.ErrorMessage(result)

        return result
Exemplo n.º 2
0
    def transition(self,
                   link,
                   decoders,
                   params=None,
                   link_ancestors=None,
                   force_codec=None):
        encoding = link.encoding if link.encoding else 'application/x-www-form-urlencoded'
        params = _get_params(link.action.upper(), encoding, link.fields,
                             params)
        url = _get_url(link.url, params.path)
        headers = _get_headers(url, decoders)
        headers.update(self._headers)

        connection_error_message = self.error_color("\n".join([
            '', 'The Zimagi client failed to connect with the server.', '',
            'This could indicate the server is down or restarting.',
            'If restarting, retry in a few minutes...'
        ]))
        if link.action == 'get':
            try:
                result = self.request_page(url, headers, params, decoders)

                if isinstance(result, Document) and link_ancestors:
                    result = _handle_inplace_replacements(
                        result, link, link_ancestors)

                if isinstance(result, Error):
                    raise exceptions.ErrorMessage(result)

                return result

            except ConnectionError as e:
                self.print(connection_error_message)
                raise CommandError()
        else:
            if self._params_callback and callable(self._params_callback):
                self._params_callback(params.data)

            try:
                return self.request_stream(url, headers, params, decoders)
            except ConnectionError as e:
                self.print(connection_error_message)
                raise CommandError()