How to use the hangups.exceptions.NetworkError function in hangups

To help you get started, we’ve selected a few hangups examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ZeWaren / jabber-hangouts-transport / lib / hangups / hangups / channel.py View on Github external
raise exceptions.NetworkError('Server disconnected error: {}'
                                          .format(e))
        if res.status == 400 and res.reason == 'Unknown SID':
            raise UnknownSIDError('SID became invalid')
        elif res.status != 200:
            raise exceptions.NetworkError(
                'Request return unexpected status: {}: {}'
                .format(res.status, res.reason)
            )
        while True:
            try:
                chunk = yield from asyncio.wait_for(
                    res.content.read(MAX_READ_BYTES), PUSH_TIMEOUT
                )
            except asyncio.TimeoutError:
                raise exceptions.NetworkError('Request timed out')
            except aiohttp.ClientError as e:
                raise exceptions.NetworkError('Request connection error: {}'
                                              .format(e))
            except aiohttp.ServerDisconnectedError as e:
                raise exceptions.NetworkError('Server disconnected error: {}'
                                              .format(e))
            except asyncio.CancelledError:
                # Prevent ResourceWarning when channel is disconnected.
                res.close()
                raise
            if chunk:
                yield from self._on_push_data(chunk)
            else:
                # Close the response to allow the connection to be reused for
                # the next request.
                res.close()
github tdryer / hangups / hangups / channel.py View on Github external
'CI': 0,
            'ctype': 'hangouts',  # client type
            'TYPE': 'xmlhttp',
        }
        headers = get_authorization_headers(self._cookies['SAPISID'])
        logger.info('Opening new long-polling request')
        try:
            res = yield from asyncio.wait_for(aiohttp.request(
                'get', CHANNEL_URL_PREFIX.format('channel/bind'),
                params=params, cookies=self._cookies, headers=headers,
                connector=self._connector
            ), CONNECT_TIMEOUT)
        except asyncio.TimeoutError:
            raise exceptions.NetworkError('Request timed out')
        except aiohttp.errors.ConnectionError as e:
            raise exceptions.NetworkError('Request connection error: {}'
                                          .format(e))
        if res.status == 400 and res.reason == 'Unknown SID':
            raise UnknownSIDError('SID became invalid')
        elif res.status != 200:
            raise exceptions.NetworkError(
                'Request return unexpected status: {}: {}'
                .format(res.status, res.reason)
            )
        while True:
            try:
                chunk = yield from asyncio.wait_for(
                    res.content.read(MAX_READ_BYTES), PUSH_TIMEOUT
                )
            except asyncio.TimeoutError:
                raise exceptions.NetworkError('Request timed out')
            except aiohttp.errors.ConnectionError as e:
github tdryer / hangups / hangups / channel.py View on Github external
't': 1, # trial
            'SID': self._sid_param,
            'CI': 0,
            'ctype': 'hangouts',  # client type
            'TYPE': 'xmlhttp',
        }
        headers = get_authorization_headers(self._cookies['SAPISID'])
        logger.info('Opening new long-polling request')
        try:
            res = yield from asyncio.wait_for(aiohttp.request(
                'get', CHANNEL_URL_PREFIX.format('channel/bind'),
                params=params, cookies=self._cookies, headers=headers,
                connector=self._connector
            ), CONNECT_TIMEOUT)
        except asyncio.TimeoutError:
            raise exceptions.NetworkError('Request timed out')
        except aiohttp.errors.ConnectionError as e:
            raise exceptions.NetworkError('Request connection error: {}'
                                          .format(e))
        if res.status == 400 and res.reason == 'Unknown SID':
            raise UnknownSIDError('SID became invalid')
        elif res.status != 200:
            raise exceptions.NetworkError(
                'Request return unexpected status: {}: {}'
                .format(res.status, res.reason)
            )
        while True:
            try:
                chunk = yield from asyncio.wait_for(
                    res.content.read(MAX_READ_BYTES), PUSH_TIMEOUT
                )
            except asyncio.TimeoutError:
github ZeWaren / jabber-hangouts-transport / lib / hangups / hangups / channel.py View on Github external
't': 1,  # trial
            'SID': self._sid_param,  # session ID
            'CI': 0,  # 0 if streaming/chunked requests should be used
            'ctype': 'hangouts',  # client type
            'TYPE': 'xmlhttp',  # type of request
        }
        headers = get_authorization_headers(self._cookies['SAPISID'])
        logger.info('Opening new long-polling request')
        try:
            res = yield from asyncio.wait_for(aiohttp.request(
                'get', CHANNEL_URL_PREFIX.format('channel/bind'),
                params=params, cookies=self._cookies, headers=headers,
                connector=self._connector
            ), CONNECT_TIMEOUT)
        except asyncio.TimeoutError:
            raise exceptions.NetworkError('Request timed out')
        except aiohttp.ClientError as e:
            raise exceptions.NetworkError('Request connection error: {}'
                                          .format(e))
        except aiohttp.ServerDisconnectedError as e:
            raise exceptions.NetworkError('Server disconnected error: {}'
                                          .format(e))
        if res.status == 400 and res.reason == 'Unknown SID':
            raise UnknownSIDError('SID became invalid')
        elif res.status != 200:
            raise exceptions.NetworkError(
                'Request return unexpected status: {}: {}'
                .format(res.status, res.reason)
            )
        while True:
            try:
                chunk = yield from asyncio.wait_for(
github xolox / python-chat-archive / chat_archive / backends / hangouts.py View on Github external
async def download_message_batch(self, conversation, event_id):
        """Try to download a batch of messages (retrying according to :attr:`retry_count`)."""
        back_off = 0.5
        for request_nr in range(1, self.retry_count):
            try:
                logger.verbose(
                    "Attempt %i/%i: Requesting messages in conversation (%s) before given message id (%s) ..",
                    request_nr,
                    self.retry_count,
                    conversation.id_,
                    event_id,
                )
                return await conversation.get_events(event_id=event_id)
            except hangups.exceptions.NetworkError:
                if request_nr < self.retry_count:
                    logger.notice(
                        "Attempt %i/%i: Sleeping for %s before retrying failed request ..",
                        request_nr,
                        self.retry_count + 1,
                        format_timespan(back_off),
                    )
                    time.sleep(back_off)
                    back_off = min(back_off * 2, 10)
                else:
                    logger.warning("Giving up on conversation after %i failed requests!", request_nr)
                    raise
github ZeWaren / jabber-hangouts-transport / lib / hangups / hangups / channel.py View on Github external
if res.status == 400 and res.reason == 'Unknown SID':
            raise UnknownSIDError('SID became invalid')
        elif res.status != 200:
            raise exceptions.NetworkError(
                'Request return unexpected status: {}: {}'
                .format(res.status, res.reason)
            )
        while True:
            try:
                chunk = yield from asyncio.wait_for(
                    res.content.read(MAX_READ_BYTES), PUSH_TIMEOUT
                )
            except asyncio.TimeoutError:
                raise exceptions.NetworkError('Request timed out')
            except aiohttp.ClientError as e:
                raise exceptions.NetworkError('Request connection error: {}'
                                              .format(e))
            except aiohttp.ServerDisconnectedError as e:
                raise exceptions.NetworkError('Server disconnected error: {}'
                                              .format(e))
            except asyncio.CancelledError:
                # Prevent ResourceWarning when channel is disconnected.
                res.close()
                raise
            if chunk:
                yield from self._on_push_data(chunk)
            else:
                # Close the response to allow the connection to be reused for
                # the next request.
                res.close()
                break
github tdryer / hangups / hangups / channel.py View on Github external
if res.status == 400 and res.reason == 'Unknown SID':
            raise UnknownSIDError('SID became invalid')
        elif res.status != 200:
            raise exceptions.NetworkError(
                'Request return unexpected status: {}: {}'
                .format(res.status, res.reason)
            )
        while True:
            try:
                chunk = yield from asyncio.wait_for(
                    res.content.read(MAX_READ_BYTES), PUSH_TIMEOUT
                )
            except asyncio.TimeoutError:
                raise exceptions.NetworkError('Request timed out')
            except aiohttp.errors.ConnectionError as e:
                raise exceptions.NetworkError('Request connection error: {}'
                                              .format(e))
            if chunk:
                yield from self._on_push_data(chunk)
            else:
                # Close the response to allow the connection to be reused for
                # the next request.
                res.close()
                break
github hangoutsbot / hangoutsbot / hangupsbot / monkeypatch / otr_support.py View on Github external
self._get_request_header(),
        None,
        [[str(chat_id), None, None, "unknown", None, []]
         for chat_id in chat_id_list],
        None,
        [
            [conversation_id], client_generated_id, otr_status.value, None, 4
        ]
    ]

    res = yield from self._request('conversations/adduser', body)
    # can return 200 but still contain an error
    res = json.loads(res.body.decode())
    res_status = res['response_header']['status']
    if res_status != 'OK':
        raise exceptions.NetworkError('Unexpected status: {}'
                                      .format(res_status))
    return res
github ZeWaren / jabber-hangouts-transport / lib / hangups / hangups / channel.py View on Github external
}
        headers = get_authorization_headers(self._cookies['SAPISID'])
        logger.info('Opening new long-polling request')
        try:
            res = yield from asyncio.wait_for(aiohttp.request(
                'get', CHANNEL_URL_PREFIX.format('channel/bind'),
                params=params, cookies=self._cookies, headers=headers,
                connector=self._connector
            ), CONNECT_TIMEOUT)
        except asyncio.TimeoutError:
            raise exceptions.NetworkError('Request timed out')
        except aiohttp.ClientError as e:
            raise exceptions.NetworkError('Request connection error: {}'
                                          .format(e))
        except aiohttp.ServerDisconnectedError as e:
            raise exceptions.NetworkError('Server disconnected error: {}'
                                          .format(e))
        if res.status == 400 and res.reason == 'Unknown SID':
            raise UnknownSIDError('SID became invalid')
        elif res.status != 200:
            raise exceptions.NetworkError(
                'Request return unexpected status: {}: {}'
                .format(res.status, res.reason)
            )
        while True:
            try:
                chunk = yield from asyncio.wait_for(
                    res.content.read(MAX_READ_BYTES), PUSH_TIMEOUT
                )
            except asyncio.TimeoutError:
                raise exceptions.NetworkError('Request timed out')
            except aiohttp.ClientError as e:
github tdryer / hangups / hangups / conversation.py View on Github external
Args:
            state_update: hangouts_pb2.StateUpdate instance
        """
        # The state update will include some type of notification:
        notification_type = state_update.WhichOneof('state_update')

        # If conversation fields have been updated, the state update will have
        # a conversation containing changed fields. Handle updating the
        # conversation from this delta:
        if state_update.HasField('conversation'):
            try:
                await self._handle_conversation_delta(
                    state_update.conversation
                )
            except exceptions.NetworkError:
                logger.warning(
                    'Discarding %s for %s: Failed to fetch conversation',
                    notification_type.replace('_', ' '),
                    state_update.conversation.conversation_id.id
                )
                return

        if notification_type == 'typing_notification':
            await self._handle_set_typing_notification(
                state_update.typing_notification
            )
        elif notification_type == 'watermark_notification':
            await self._handle_watermark_notification(
                state_update.watermark_notification
            )
        elif notification_type == 'event_notification':