Skip to content

Commit fd6822c

Browse files
committed
Fix CI
1 parent 13de0ff commit fd6822c

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

firebase_admin/messaging.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ def send_all(messages, dry_run=False, app=None):
120120
return _get_messaging_service(app).send_all(messages, dry_run)
121121

122122
def send_multicast(multicast_message, dry_run=False, app=None):
123-
"""Sends the given mutlicast message to the mutlicast message tokens via Firebase Cloud Messaging (FCM).
123+
"""Sends the given mutlicast message to all tokens via Firebase Cloud Messaging (FCM).
124124
125125
If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
126126
recipients. Instead FCM performs all the usual validations, and emulates the send operation.
127127
128128
Args:
129-
message: An instance of ``messaging.MulticastMessage``.
129+
multicast_message: An instance of ``messaging.MulticastMessage``.
130130
dry_run: A boolean indicating whether to run the operation in dry run mode (optional).
131131
app: An App instance (optional).
132132
@@ -139,14 +139,14 @@ def send_multicast(multicast_message, dry_run=False, app=None):
139139
"""
140140
if not isinstance(multicast_message, MulticastMessage):
141141
raise ValueError('Message must be an instance of messaging.MulticastMessage class.')
142-
messages = map(lambda token: Message(
142+
messages = [Message(
143143
data=multicast_message.data,
144144
notification=multicast_message.notification,
145145
android=multicast_message.android,
146146
webpush=multicast_message.webpush,
147147
apns=multicast_message.apns,
148148
token=token
149-
), multicast_message.tokens)
149+
) for token in multicast_message.tokens]
150150
return _get_messaging_service(app).send_all(messages, dry_run)
151151

152152
def subscribe_to_topic(tokens, topic, app=None):
@@ -254,6 +254,7 @@ def __init__(self, code, message, detail=None):
254254

255255

256256
class BatchResponse(object):
257+
"""The response received from a batch request to the FCM API."""
257258

258259
def __init__(self, responses):
259260
self._responses = responses
@@ -277,6 +278,7 @@ def failure_count(self):
277278

278279

279280
class SendResponse(object):
281+
"""The response received from an individual batched request to the FCM API."""
280282

281283
def __init__(self, resp, exception):
282284
self._exception = exception
@@ -361,7 +363,12 @@ def send(self, message, dry_run=False):
361363
data = self._message_data(message, dry_run)
362364
try:
363365
resp = self._client.body(
364-
'post', url=self._fcm_url, headers=self._fcm_headers, json=data, timeout=self._timeout)
366+
'post',
367+
url=self._fcm_url,
368+
headers=self._fcm_headers,
369+
json=data,
370+
timeout=self._timeout
371+
)
365372
except requests.exceptions.RequestException as error:
366373
if error.response is not None:
367374
self._handle_fcm_error(error)
@@ -372,12 +379,13 @@ def send(self, message, dry_run=False):
372379
return resp['name']
373380

374381
def send_all(self, messages, dry_run=False):
382+
"""Sends the given messages to FCM via the batch API."""
375383
if not isinstance(messages, list):
376384
raise ValueError('Messages must be an list of messaging.Message instances.')
377385

378386
responses = []
379387

380-
def batch_callback(request_id, response, error):
388+
def batch_callback(_, response, error):
381389
exception = None
382390
if error:
383391
exception = self._parse_batch_error(error)
@@ -388,7 +396,13 @@ def batch_callback(request_id, response, error):
388396
for message in messages:
389397
body = json.dumps(self._message_data(message, dry_run))
390398
req = http.HttpRequest(
391-
http=self._transport, postproc=self._postproc, uri=self._fcm_url, method='POST', body=body, headers=self._fcm_headers)
399+
http=self._transport,
400+
postproc=self._postproc,
401+
uri=self._fcm_url,
402+
method='POST',
403+
body=body,
404+
headers=self._fcm_headers
405+
)
392406
batch.add(req)
393407

394408
try:
@@ -455,7 +469,8 @@ def _handle_fcm_error(self, error):
455469
except ValueError:
456470
pass
457471

458-
raise _MessagingService._parse_fcm_error(data, error.response.content, error.response.status_code, error)
472+
raise _MessagingService._parse_fcm_error(
473+
data, error.response.content, error.response.status_code, error)
459474

460475
def _handle_iid_error(self, error):
461476
"""Handles errors received from the Instance ID API."""
@@ -476,6 +491,7 @@ def _handle_iid_error(self, error):
476491
raise ApiCallError(code, msg, error)
477492

478493
def _parse_batch_error(self, error):
494+
"""Parses a googleapiclient.http.HttpError content in to an ApiCallError."""
479495
if error.content is None:
480496
msg = 'Failed to call messaging API: {0}'.format(error)
481497
return ApiCallError(self.INTERNAL_ERROR, msg, error)
@@ -504,6 +520,7 @@ def _parse_fcm_error(cls, data, content, status_code, error):
504520

505521
msg = error_dict.get('message')
506522
if not msg:
507-
msg = 'Unexpected HTTP response with status: {0}; body: {1}'.format(status_code, content.decode())
523+
msg = 'Unexpected HTTP response with status: {0}; body: {1}'.format(
524+
status_code, content.decode())
508525

509526
return ApiCallError(code, msg, error)

tests/test_messaging.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import pytest
2121
import six
2222

23-
import googleapiclient
2423
from googleapiclient.http import HttpMockSequence
2524

2625
import firebase_admin
@@ -1338,7 +1337,9 @@ def test_send_fcm_error_code(self, status):
13381337

13391338
class TestSendAll(object):
13401339

1341-
_PAYLOAD_FORMAT = """--boundary\r\nContent-Type: application/http\r\nContent-ID: <uuid + 1>\r\n\r\nHTTP/1.1 {} Success\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n{}\r\n\r\n--boundary--"""
1340+
_PAYLOAD_FORMAT = """--boundary\r\nContent-Type: application/http\r\n\
1341+
Content-ID: <uuid + 1>\r\n\r\nHTTP/1.1 {} Success\r\n\
1342+
Content-Type: application/json; charset=UTF-8\r\n\r\n{}\r\n\r\n--boundary--"""
13421343
_CLIENT_VERSION = 'fire-admin-python/{0}'.format(firebase_admin.__version__)
13431344

13441345
@classmethod
@@ -1386,7 +1387,8 @@ def test_invalid_send_all(self, msg):
13861387

13871388
def test_send_all(self):
13881389
payload = json.dumps({'name': 'message-id'})
1389-
_ = self._instrument_batch_messaging_service(payload=self._PAYLOAD_FORMAT.format('200', payload))
1390+
_ = self._instrument_batch_messaging_service(
1391+
payload=self._PAYLOAD_FORMAT.format('200', payload))
13901392
msg = messaging.Message(topic='foo')
13911393
batch_response = messaging.send_all([msg], dry_run=True)
13921394
assert batch_response.success_count is 1
@@ -1488,7 +1490,7 @@ def test_send_all_batch_detailed_error(self, status):
14881490
assert str(excinfo.value.code) == 'invalid-argument'
14891491

14901492
@pytest.mark.parametrize('status', HTTP_ERRORS)
1491-
def test_send_all_canonical_error_code(self, status):
1493+
def test_send_all_batch_canonical_error_code(self, status):
14921494
payload = json.dumps({
14931495
'error': {
14941496
'status': 'NOT_FOUND',
@@ -1503,7 +1505,7 @@ def test_send_all_canonical_error_code(self, status):
15031505
assert str(excinfo.value.code) == 'registration-token-not-registered'
15041506

15051507
@pytest.mark.parametrize('status', HTTP_ERRORS)
1506-
def test_send_all_fcm_error_code(self, status):
1508+
def test_send_all_batch_fcm_error_code(self, status):
15071509
payload = json.dumps({
15081510
'error': {
15091511
'status': 'INVALID_ARGUMENT',
@@ -1526,7 +1528,9 @@ def test_send_all_fcm_error_code(self, status):
15261528

15271529
class TestSendMulticast(object):
15281530

1529-
_PAYLOAD_FORMAT = """--boundary\r\nContent-Type: application/http\r\nContent-ID: <uuid + 1>\r\n\r\nHTTP/1.1 {} Success\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n{}\r\n\r\n--boundary--"""
1531+
_PAYLOAD_FORMAT = """--boundary\r\nContent-Type: application/http\r\n\
1532+
Content-ID: <uuid + 1>\r\n\r\nHTTP/1.1 {} Success\r\n\
1533+
Content-Type: application/json; charset=UTF-8\r\n\r\n{}\r\n\r\n--boundary--"""
15301534
_CLIENT_VERSION = 'fire-admin-python/{0}'.format(firebase_admin.__version__)
15311535

15321536
@classmethod
@@ -1570,7 +1574,8 @@ def test_invalid_send_multicast(self, msg):
15701574

15711575
def test_send_multicast(self):
15721576
payload = json.dumps({'name': 'message-id'})
1573-
_ = self._instrument_batch_messaging_service(payload=self._PAYLOAD_FORMAT.format('200', payload))
1577+
_ = self._instrument_batch_messaging_service(
1578+
payload=self._PAYLOAD_FORMAT.format('200', payload))
15741579
msg = messaging.MulticastMessage(tokens=['foo'])
15751580
batch_response = messaging.send_multicast(msg, dry_run=True)
15761581
assert batch_response.success_count is 1
@@ -1672,7 +1677,7 @@ def test_send_multicast_batch_detailed_error(self, status):
16721677
assert str(excinfo.value.code) == 'invalid-argument'
16731678

16741679
@pytest.mark.parametrize('status', HTTP_ERRORS)
1675-
def test_send_multicast_canonical_error_code(self, status):
1680+
def test_send_multicast_batch_canonical_error_code(self, status):
16761681
payload = json.dumps({
16771682
'error': {
16781683
'status': 'NOT_FOUND',
@@ -1687,7 +1692,7 @@ def test_send_multicast_canonical_error_code(self, status):
16871692
assert str(excinfo.value.code) == 'registration-token-not-registered'
16881693

16891694
@pytest.mark.parametrize('status', HTTP_ERRORS)
1690-
def test_send_multicast_fcm_error_code(self, status):
1695+
def test_send_multicast_batch_fcm_error_code(self, status):
16911696
payload = json.dumps({
16921697
'error': {
16931698
'status': 'INVALID_ARGUMENT',

0 commit comments

Comments
 (0)