Esempio n. 1
0
    def send(self):
        """ Attempt to send the WebhookRequest.

        Returns:
            NotificationResponse: content/status_code
        """
        # Build the request
        headers = {
            'Content-Type': 'application/json',
            'X-TBA-Version': '{}'.format(WEBHOOK_VERSION)
        }
        message_json = self.json_string()
        # Generate checksum
        headers['X-TBA-Checksum'] = self._generate_webhook_checksum(
            message_json)

        from google.appengine.api import urlfetch
        rpc = urlfetch.create_rpc()

        from tbans.models.requests.notifications.notification_response import NotificationResponse
        try:
            urlfetch.make_fetch_call(rpc,
                                     self.url,
                                     payload=message_json,
                                     method=urlfetch.POST,
                                     headers=headers)
            return NotificationResponse(200, None)
        except Exception, e:
            # https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.api.urlfetch
            return NotificationResponse(500, str(e))
Esempio n. 2
0
    def _transform_fcm_response(response):
        """ Transforms a HTTP response from FCM -> a NotificationResponse, adding error information.

        Taken from https://github.com/firebase/firebase-admin-python/blob/932cf17a6f222c627dbd1502658f3eb338077250/firebase_admin/messaging.py

        Error JSON from FCM will look like...
        {
          "error": {
            "code": 404,
            "message": "Requested entity was not found.",
            "status": "NOT_FOUND",
            "details": [
              {
                "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
                "errorCode": "UNREGISTERED"
              }
            ]
          }
        }

        Success JSON from FCM will look like...
        {
          "name": "projects/{project_id}/messages/1545762214218984"
        }
        """
        from tbans.models.requests.notifications.notification_response import NotificationResponse
        from tbans.utils.json_utils import json_string_to_dict

        data = json_string_to_dict(response.content)

        error_dict = data.get('error', None)
        # If we didn't error - go ahead and return the original response information
        if error_dict is None:
            return NotificationResponse(response.status_code, response.content)

        http_code = error_dict.get('code')

        # Get the FCM error code - we should try to act on this
        error_code = None
        # Pull the FCM v1 new error code
        for detail in error_dict.get('details', []):
            if detail.get(
                    '@type'
            ) == 'type.googleapis.com/google.firebase.fcm.v1.FcmError':
                error_code = detail.get('errorCode')
                break
        # Fall back to the FCM v1 canonical error code
        if not error_code:
            error_code = error_dict.get('status')

        from tbans.consts.fcm_error import FCMError
        fcm_error_code = FCMError.ERROR_CODES.get(error_code,
                                                  FCMError.UNKNOWN_ERROR)
        # Note - we lose the `message` field from the response
        return NotificationResponse(http_code, fcm_error_code)
 def test_init(self):
     _status_code = 404
     _content = 'Some content here'
     response = NotificationResponse(status_code=_status_code,
                                     content=_content)
     self.assertEqual(response.status_code, _status_code)
     self.assertEqual(response.content, _content)
Esempio n. 4
0
    def send(self):
        """ Attempt to send FCMRequest.

        Return:
            NotificationResponse: content/status_code
        """
        from google.appengine.api import urlfetch
        from tbans.models.requests.notifications.notification_response import NotificationResponse
        from tbans.utils.auth_utils import get_firebase_messaging_access_token

        # Build the request
        headers = {
            'Authorization': 'Bearer ' + get_firebase_messaging_access_token(),
            'Content-Type': 'application/json'
        }
        message_json = self.json_string()

        try:
            response = urlfetch.fetch(url=self._fcm_url,
                                      payload=message_json,
                                      method=urlfetch.POST,
                                      headers=headers)
            return FCMRequest._transform_fcm_response(response)
        except Exception, e:
            # https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.api.urlfetch_errors
            return NotificationResponse(500, str(e))
 def test_status_code(self):
     with self.assertRaises(TypeError):
         NotificationResponse(status_code=None)
 def test_init_empty(self):
     NotificationResponse(status_code=0, content='')
 def test_str_content(self):
     response = NotificationResponse(status_code=400,
                                     content='Some content here')
     self.assertEqual(
         str(response),
         'NotificationResponse(code=400 content="Some content here")')
 def test_str(self):
     response = NotificationResponse(status_code=400)
     self.assertEqual(str(response),
                      'NotificationResponse(code=400 content=None)')
 def test_content_type(self):
     with self.assertRaises(TypeError):
         NotificationResponse(status_code=200, content=200)
 def test_status_code_zero(self):
     NotificationResponse(status_code=0)