def on_response(self, message): self.refresh_inactivity_timer() body = json.loads(message.fcm_payload.text) try: message_id = body['message_id'] message_type = body['message_type'] except KeyError: logger.warning('Got strange response: %s', body) return if message_type not in (FCMMessageType.ACK, FCMMessageType.NACK): return request = self.requests.pop(message_id, None) if not request: logger.warning('Got response for unknown message %s', message_id) return if message_type == FCMMessageType.ACK: result = MessageResponse(message_id, STATUS_SUCCESS) request.set_result(result) elif message_type == FCMMessageType.NACK: status = body['error'] description = body['error_description'] result = MessageResponse(message_id, status, description) request.set_result(result)
async def send_message(self, message: Message) -> MessageResponse: attempt = 0 while True: attempt += 1 if attempt > self.MAX_ATTEMPTS: msg_error = MessageResponse( message.message_id, 'ERROR', "connection") return msg_error if attempt > 1: logger.warning('Trying to send message %s: attempt #%s', message.message_id, attempt) logger.debug('Message %s: waiting for connection', message.message_id) try: connection = await self.acquire() except ConnectionError: logger.warning('Could not send notification %s due to ' 'connection problem', message.message_id) await asyncio.sleep(1) continue logger.debug('Message %s: connection %s acquired', message.message_id, connection) try: response = await connection.send_message(message) return response except ConnectionClosed: logger.warning('Could not send message %s: ' 'ConnectionClosed', message.message_id) except Exception as e: logger.error('Could not send message %s: %s', message.message_id, e)
def on_response(self, message): self.refresh_inactivity_timer() body = json.loads(message.fcm_payload.text) handle_upstream = False try: message_id = body['message_id'] message_type = body['message_type'] except KeyError: try: message_id = body['message_id'] category = body['category'] device_token = body['from'] data = body['data'] handle_upstream = True except KeyError: logger.warning('Got strange response: %s', body) return if handle_upstream: asyncio.ensure_future(self.send_ack(device_token, message_id)) if self.upstream_callback is not None: asyncio.ensure_future( self.upstream_callback(device_token, category, data)) return if message_type not in (FCMMessageType.ACK, FCMMessageType.NACK): return request = self.requests.pop(message_id, None) if not request: logger.warning('Got response for unknown message %s', message_id) return if message_type == FCMMessageType.ACK: result = MessageResponse(message_id, STATUS_SUCCESS) request.set_result(result) elif message_type == FCMMessageType.NACK: status = body['error'] description = body['error_description'] result = MessageResponse(message_id, status, description) request.set_result(result)