Esempio n. 1
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 _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)
Esempio n. 3
0
 def test_json_string_to_dict_none(self):
     self.assertEqual(json_string_to_dict(None), {})
Esempio n. 4
0
 def test_json_string_to_dict_list(self):
     self.assertEqual(json_string_to_dict('["a", "b", "c"]'), {})
Esempio n. 5
0
 def test_json_string_to_dict(self):
     self.assertEqual(json_string_to_dict('{"abc": "def"}'), {'abc': 'def'})
 def test_json_string_to_dict_list(self):
     self.assertEqual(json_string_to_dict('["a", "b", "c"]'), {})
 def test_json_string_to_dict(self):
     self.assertEqual(json_string_to_dict('{"abc": "def"}'), {'abc': 'def'})
 def test_json_string_to_dict_none(self):
     self.assertEqual(json_string_to_dict(None), {})
 def _raw_data(self):
     if self._response:
         return json_string_to_dict(self._response.content)
     return {}
 def _raw_data(self):
     from tbans.utils.json_utils import json_string_to_dict
     if self._response:
         return json_string_to_dict(self._response.content)
     return {}