@@ -120,13 +120,13 @@ def send_all(messages, dry_run=False, app=None):
120
120
return _get_messaging_service (app ).send_all (messages , dry_run )
121
121
122
122
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).
124
124
125
125
If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
126
126
recipients. Instead FCM performs all the usual validations, and emulates the send operation.
127
127
128
128
Args:
129
- message : An instance of ``messaging.MulticastMessage``.
129
+ multicast_message : An instance of ``messaging.MulticastMessage``.
130
130
dry_run: A boolean indicating whether to run the operation in dry run mode (optional).
131
131
app: An App instance (optional).
132
132
@@ -139,14 +139,14 @@ def send_multicast(multicast_message, dry_run=False, app=None):
139
139
"""
140
140
if not isinstance (multicast_message , MulticastMessage ):
141
141
raise ValueError ('Message must be an instance of messaging.MulticastMessage class.' )
142
- messages = map ( lambda token : Message (
142
+ messages = [ Message (
143
143
data = multicast_message .data ,
144
144
notification = multicast_message .notification ,
145
145
android = multicast_message .android ,
146
146
webpush = multicast_message .webpush ,
147
147
apns = multicast_message .apns ,
148
148
token = token
149
- ), multicast_message .tokens )
149
+ ) for token in multicast_message .tokens ]
150
150
return _get_messaging_service (app ).send_all (messages , dry_run )
151
151
152
152
def subscribe_to_topic (tokens , topic , app = None ):
@@ -254,6 +254,7 @@ def __init__(self, code, message, detail=None):
254
254
255
255
256
256
class BatchResponse (object ):
257
+ """The response received from a batch request to the FCM API."""
257
258
258
259
def __init__ (self , responses ):
259
260
self ._responses = responses
@@ -277,6 +278,7 @@ def failure_count(self):
277
278
278
279
279
280
class SendResponse (object ):
281
+ """The response received from an individual batched request to the FCM API."""
280
282
281
283
def __init__ (self , resp , exception ):
282
284
self ._exception = exception
@@ -361,7 +363,12 @@ def send(self, message, dry_run=False):
361
363
data = self ._message_data (message , dry_run )
362
364
try :
363
365
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
+ )
365
372
except requests .exceptions .RequestException as error :
366
373
if error .response is not None :
367
374
self ._handle_fcm_error (error )
@@ -372,12 +379,13 @@ def send(self, message, dry_run=False):
372
379
return resp ['name' ]
373
380
374
381
def send_all (self , messages , dry_run = False ):
382
+ """Sends the given messages to FCM via the batch API."""
375
383
if not isinstance (messages , list ):
376
384
raise ValueError ('Messages must be an list of messaging.Message instances.' )
377
385
378
386
responses = []
379
387
380
- def batch_callback (request_id , response , error ):
388
+ def batch_callback (_ , response , error ):
381
389
exception = None
382
390
if error :
383
391
exception = self ._parse_batch_error (error )
@@ -388,7 +396,13 @@ def batch_callback(request_id, response, error):
388
396
for message in messages :
389
397
body = json .dumps (self ._message_data (message , dry_run ))
390
398
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
+ )
392
406
batch .add (req )
393
407
394
408
try :
@@ -455,7 +469,8 @@ def _handle_fcm_error(self, error):
455
469
except ValueError :
456
470
pass
457
471
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 )
459
474
460
475
def _handle_iid_error (self , error ):
461
476
"""Handles errors received from the Instance ID API."""
@@ -476,13 +491,14 @@ def _handle_iid_error(self, error):
476
491
raise ApiCallError (code , msg , error )
477
492
478
493
def _parse_batch_error (self , error ):
494
+ """Parses a googleapiclient.http.HttpError content in to an ApiCallError."""
479
495
if error .content is None :
480
496
msg = 'Failed to call messaging API: {0}' .format (error )
481
497
return ApiCallError (self .INTERNAL_ERROR , msg , error )
482
498
483
499
data = {}
484
500
try :
485
- parsed_body = json .loads (error .content )
501
+ parsed_body = json .loads (str ( error .content ) )
486
502
if isinstance (parsed_body , dict ):
487
503
data = parsed_body
488
504
except ValueError :
@@ -504,6 +520,7 @@ def _parse_fcm_error(cls, data, content, status_code, error):
504
520
505
521
msg = error_dict .get ('message' )
506
522
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 ())
508
525
509
526
return ApiCallError (code , msg , error )
0 commit comments