def ping(self, request): """ Immediately dispatch a Ping to either FCM or a webhook """ self._validate_authentication() if request.fcm and request.webhook: return self._application_error('Cannot ping both FCM and webhook') from tbans.models.notifications.ping import PingNotification notification = PingNotification() if request.fcm: from tbans.models.requests.notifications.fcm_request import FCMRequest fcm_request = FCMRequest(notification, token=request.fcm.token, topic=request.fcm.topic, condition=request.fcm.condition) logging.info('Ping - {}'.format(str(fcm_request))) response = fcm_request.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) elif request.webhook: from tbans.models.requests.notifications.webhook_request import WebhookRequest webhook_request = WebhookRequest(notification, request.webhook.url, request.webhook.secret) logging.info('Ping - {}'.format(str(webhook_request))) response = webhook_request.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) else: return self._application_error( 'Did not specify FCM or webhook to ping')
def ping(self, request): """ Immediately dispatch a Ping to either FCM or a webhook """ if not self._authenticated: raise remote.ApplicationError('Unauthenticated') if request.fcm and request.webhook: return TBANSResponse(code=400, message='Cannot ping both FCM and webhook') from tbans.models.notifications.ping import PingNotification notification = PingNotification() if request.fcm: from tbans.models.messages.fcm_message import FCMMessage message = FCMMessage(notification, token=request.fcm.token, topic=request.fcm.topic, condition=request.fcm.condition) logging.info('Ping - {}'.format(str(message))) response = message.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) elif request.webhook: from tbans.models.messages.webhook_message import WebhookMessage message = WebhookMessage(notification, request.webhook.url, request.webhook.secret) logging.info('Ping - {}'.format(str(message))) response = message.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) else: return TBANSResponse(code=400, message='Did not specify FCM or webhook to ping')
def ping(self, request): """ Immediately dispatch a Ping to either FCM or a webhook """ self._validate_authentication() if request.fcm and request.webhook: return self._application_error('Cannot ping both FCM and webhook') from tbans.models.notifications.ping import PingNotification notification = PingNotification() if request.fcm: # An FCM request can still exist, I believe. It can take some notification and delivery options from tbans.requests.fcm_request import FCMRequest fcm_request = FCMRequest(self._firebase_app, notification, token=request.fcm.token, topic=request.fcm.topic, condition=request.fcm.condition) logging.info('Ping - {}'.format(str(fcm_request))) message_id = fcm_request.send() logging.info('Ping Sent - {}'.format(str(message_id))) return TBANSResponse(code=200, message=message_id) elif request.webhook: from tbans.requests.webhook_request import WebhookRequest webhook_request = WebhookRequest(notification, request.webhook.url, request.webhook.secret) logging.info('Ping - {}'.format(str(webhook_request))) webhook_request.send() logging.info('Ping Sent') return TBANSResponse(code=200) else: return self._application_error('Did not specify FCM or webhook to ping')
def _application_error(self, message): """ Helper method to log and return a 400 TBANSResponse """ # TODO: Monitor these logging.error(message) return TBANSResponse(code=400, message=message)
def test_tbans_response_message(self): message = 'Some message here' response = TBANSResponse(message=message) self.assertTrue(response.is_initialized()) self.assertEqual(response.code, 200) self.assertEqual(response.message, message)
def test_tbans_response_code(self): response = TBANSResponse(code=500) self.assertTrue(response.is_initialized()) self.assertEqual(response.code, 500) self.assertEqual(response.message, None)
def test_tbans_response_default(self): response = TBANSResponse() self.assertTrue(response.is_initialized()) self.assertEqual(response.code, 200) self.assertEqual(response.message, None)