11pub mod response;
22
3+ use hyper:: { Body , Request , StatusCode } ;
4+ #[ cfg( feature = "hyper-rustls" ) ]
5+ use hyper_rustls:: HttpsConnector ;
6+ #[ cfg( feature = "hyper-tls" ) ]
7+ use hyper_tls:: HttpsConnector ;
8+
39pub use crate :: client:: response:: * ;
410
511use crate :: message:: Message ;
6- use reqwest:: header:: { AUTHORIZATION , CONTENT_LENGTH , CONTENT_TYPE , RETRY_AFTER } ;
7- use reqwest:: { Body , StatusCode } ;
812
913/// An async client for sending the notification payload.
10- pub struct Client {
11- http_client : reqwest:: Client ,
12- }
14+ pub struct Client { }
1315
1416impl Default for Client {
1517 fn default ( ) -> Self {
@@ -19,40 +21,42 @@ impl Default for Client {
1921
2022impl Client {
2123 /// Get a new instance of Client.
22- pub fn new ( ) -> Client {
23- let http_client = reqwest:: ClientBuilder :: new ( )
24- . pool_max_idle_per_host ( std:: usize:: MAX )
25- . build ( )
26- . unwrap ( ) ;
27-
28- Client { http_client }
24+ pub fn new ( ) -> Self {
25+ Self { }
2926 }
3027
3128 /// Try sending a `Message` to FCM.
3229 pub async fn send ( & self , message : Message < ' _ > ) -> Result < FcmResponse , FcmError > {
3330 let payload = serde_json:: to_vec ( & message. body ) . unwrap ( ) ;
3431
35- let request = self
36- . http_client
37- . post ( "https://fcm.googleapis.com/fcm/send" )
38- . header ( CONTENT_TYPE , "application/json" )
39- . header ( CONTENT_LENGTH , format ! ( "{}" , payload. len( ) as u64 ) . as_bytes ( ) )
40- . header ( AUTHORIZATION , format ! ( "key={}" , message. api_key) . as_bytes ( ) )
41- . body ( Body :: from ( payload) )
42- . build ( ) ?;
43- let response = self . http_client . execute ( request) . await ?;
32+ #[ cfg( feature = "hyper-tls" ) ]
33+ let connector = HttpsConnector :: new ( ) ;
34+
35+ #[ cfg( feature = "hyper-rustls" ) ]
36+ let connector = HttpsConnector :: with_native_roots ( ) ;
37+
38+ let client = hyper:: Client :: builder ( ) . build :: < _ , Body > ( connector) ;
39+ let request = Request :: builder ( )
40+ . method ( "POST" )
41+ . uri ( "https://fcm.googleapis.com/fcm/send" )
42+ . header ( "Content-Type" , "application/json" )
43+ . header ( "Content-Length" , format ! ( "{}" , payload. len( ) as u64 ) )
44+ . header ( "Athorization" , format ! ( "key={}" , message. api_key) )
45+ . body ( Body :: from ( payload) ) ?;
46+ let response = client. request ( request) . await ?;
4447
4548 let response_status = response. status ( ) ;
4649
4750 let retry_after = response
4851 . headers ( )
49- . get ( RETRY_AFTER )
52+ . get ( "Retry-After" )
5053 . and_then ( |ra| ra. to_str ( ) . ok ( ) )
5154 . and_then ( |ra| ra. parse :: < RetryAfter > ( ) . ok ( ) ) ;
5255
5356 match response_status {
5457 StatusCode :: OK => {
55- let fcm_response: FcmResponse = response. json ( ) . await . unwrap ( ) ;
58+ let buf = hyper:: body:: to_bytes ( response) . await ?;
59+ let fcm_response: FcmResponse = serde_json:: from_slice ( & buf) ?;
5660
5761 match fcm_response. error {
5862 Some ( ErrorReason :: Unavailable ) => Err ( response:: FcmError :: ServerError ( retry_after) ) ,
0 commit comments