15
15
"""Firebase Cloud Messaging module."""
16
16
17
17
from __future__ import annotations
18
- from typing import Callable , List , Optional
18
+ from typing import Any , Callable , Dict , List , Optional , cast
19
19
import concurrent .futures
20
20
import json
21
21
import warnings
37
37
exceptions ,
38
38
App
39
39
)
40
- from firebase_admin ._retry import HttpxRetryTransport
41
40
42
41
logger = logging .getLogger (__name__ )
43
42
111
110
def _get_messaging_service (app : Optional [App ]) -> _MessagingService :
112
111
return _utils .get_app_service (app , _MESSAGING_ATTRIBUTE , _MessagingService )
113
112
114
- def send (message , dry_run = False , app : Optional [App ] = None ):
113
+ def send (message : Message , dry_run : bool = False , app : Optional [App ] = None ) -> str :
115
114
"""Sends the given message via Firebase Cloud Messaging (FCM).
116
115
117
116
If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
@@ -131,7 +130,11 @@ def send(message, dry_run=False, app: Optional[App] = None):
131
130
"""
132
131
return _get_messaging_service (app ).send (message , dry_run )
133
132
134
- def send_each (messages , dry_run = False , app = None ):
133
+ def send_each (
134
+ messages : List [Message ],
135
+ dry_run : bool = False ,
136
+ app : Optional [App ] = None
137
+ ) -> BatchResponse :
135
138
"""Sends each message in the given list via Firebase Cloud Messaging.
136
139
137
140
If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
@@ -451,7 +454,7 @@ class _MessagingService:
451
454
'UNREGISTERED' : UnregisteredError ,
452
455
}
453
456
454
- def __init__ (self , app ) -> None :
457
+ def __init__ (self , app : App ) -> None :
455
458
project_id = app .project_id
456
459
if not project_id :
457
460
raise ValueError (
@@ -476,7 +479,7 @@ def encode_message(cls, message):
476
479
raise ValueError ('Message must be an instance of messaging.Message class.' )
477
480
return cls .JSON_ENCODER .default (message )
478
481
479
- def send (self , message , dry_run = False ):
482
+ def send (self , message : Message , dry_run : bool = False ) -> str :
480
483
"""Sends the given message to FCM via the FCM v1 API."""
481
484
data = self ._message_data (message , dry_run )
482
485
try :
@@ -489,9 +492,9 @@ def send(self, message, dry_run=False):
489
492
except requests .exceptions .RequestException as error :
490
493
raise self ._handle_fcm_error (error )
491
494
else :
492
- return resp ['name' ]
495
+ return cast ( str , resp ['name' ])
493
496
494
- def send_each (self , messages , dry_run = False ):
497
+ def send_each (self , messages : List [ Message ] , dry_run : bool = False ) -> BatchResponse :
495
498
"""Sends the given messages to FCM via the FCM v1 API."""
496
499
if not isinstance (messages , list ):
497
500
raise ValueError ('messages must be a list of messaging.Message instances.' )
@@ -683,7 +686,10 @@ def _build_fcm_error_requests(cls, error, message, error_dict):
683
686
684
687
@classmethod
685
688
def _build_fcm_error_httpx (
686
- cls , error : httpx .HTTPError , message , error_dict
689
+ cls ,
690
+ error : httpx .HTTPError ,
691
+ message : str ,
692
+ error_dict : Optional [Dict [str , Any ]]
687
693
) -> Optional [exceptions .FirebaseError ]:
688
694
"""Parses a httpx error response from the FCM API and creates a FCM-specific exception if
689
695
appropriate."""
@@ -702,7 +708,11 @@ def _build_fcm_error_googleapiclient(cls, error, message, error_dict, http_respo
702
708
return exc_type (message , cause = error , http_response = http_response ) if exc_type else None
703
709
704
710
@classmethod
705
- def _build_fcm_error (cls , error_dict ) -> Optional [Callable [..., exceptions .FirebaseError ]]:
711
+ def _build_fcm_error (
712
+ cls ,
713
+ error_dict : Optional [Dict [str , Any ]]
714
+ ) -> Optional [Callable [..., exceptions .FirebaseError ]]:
715
+ """Parses an error response to determine the appropriate FCM-specific error type."""
706
716
if not error_dict :
707
717
return None
708
718
fcm_code = None
0 commit comments