Skip to content

Commit 59cb7fe

Browse files
committed
Move batch message decoding in to _messaging_utils class BatchMessageResponseDecoder
1 parent e126292 commit 59cb7fe

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

firebase_admin/_messaging_utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,10 +776,10 @@ def default(self, obj): # pylint: disable=method-hidden
776776

777777

778778
class BatchMessageEncoder(object):
779-
"""A custom encoder for serializing JSON for Messages into batch request data."""
779+
"""A custom encoder for converting JSON for Messages into batch request data."""
780780

781781
@classmethod
782-
def encode(cls, url, headers, boundary, message_data): # pylint: disable=method-hidden
782+
def encode(cls, url, headers, boundary, message_data):
783783
parts = [cls._batch_request_part(url, headers, boundary, md, index) for (index, md) in enumerate(message_data)]
784784
return '{}--{}--\r\n'.format(''.join(parts), boundary)
785785

@@ -805,3 +805,20 @@ def _batch_request_part_data(cls, url, headers, message_data):
805805
data += '\r\n\r\n'
806806
data += body
807807
return data
808+
809+
class BatchMessageResponseDecoder(object):
810+
"""A custom decoder for decoding batch message responses into JSON."""
811+
812+
@classmethod
813+
def decode(cls, response):
814+
# Split our batches responses in to groups
815+
response_regex = re.compile('--batch.*?$\n(.*?)(?=--batch)', re.DOTALL | re.MULTILINE)
816+
responses = response_regex.findall(response.content)
817+
818+
# First section is headers related to the batch request
819+
# Second section is headers related to the batched request
820+
# Third section is data related to the batched request
821+
response_sections = [r.split('\r\n\r\n') for r in responses]
822+
response_json = [json.loads(r[2]) for r in response_sections]
823+
824+
return response_json

firebase_admin/messaging.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""Firebase Cloud Messaging module."""
1616

17-
import json
1817
import requests
1918
import six
2019

@@ -253,6 +252,7 @@ class _MessagingService(object):
253252
IID_HEADERS = {'access_token_auth': 'true'}
254253
JSON_ENCODER = _messaging_utils.MessageEncoder()
255254
BATCH_ENCODER = _messaging_utils.BatchMessageEncoder()
255+
BATCH_DECODER = _messaging_utils.BatchMessageResponseDecoder()
256256

257257
BATCH_PART_BOUNDARY = '__END_OF_PART__'
258258

@@ -329,7 +329,7 @@ def sendAll(self, messages, dry_run=False):
329329
'Content-Type': 'multipart/mixed; boundary={}'.format(_MessagingService.BATCH_PART_BOUNDARY)
330330
}
331331
try:
332-
response = self._client.request(
332+
resp = self._client.request(
333333
'post', url=_MessagingService.FCM_BATCH_URL, headers=headers, data=data, timeout=self._timeout
334334
)
335335
except requests.exceptions.RequestException as error:
@@ -339,18 +339,7 @@ def sendAll(self, messages, dry_run=False):
339339
msg = 'Failed to call messaging API: {0}'.format(error)
340340
raise ApiCallError(self.INTERNAL_ERROR, msg, error)
341341
else:
342-
import re
343-
# Split our batches responses in to groups
344-
response_regex = re.compile('--batch.*?$\n(.*?)(?=--batch)', re.DOTALL | re.MULTILINE)
345-
responses = response_regex.findall(response.content)
346-
347-
# First section is headers related to the batch request
348-
# Second section is headers related to the batched request
349-
# Third section is data related to the batched request
350-
response_sections = [r.split('\r\n\r\n') for r in responses]
351-
response_json = [json.loads(r[2]) for r in response_sections]
352-
353-
return response_json
342+
return _MessagingService.BATCH_DECODER.decode(resp)
354343

355344
def make_topic_management_request(self, tokens, topic, operation):
356345
"""Invokes the IID service for topic management functionality."""

0 commit comments

Comments
 (0)