11pub mod response;
22
3+ use hyper:: { client:: HttpConnector , 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.
1014pub struct Client {
11- http_client : reqwest :: Client ,
15+ http_client : hyper :: Client < HttpsConnector < HttpConnector > > ,
1216}
1317
1418impl Default for Client {
@@ -19,40 +23,43 @@ impl Default for Client {
1923
2024impl Client {
2125 /// 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 ( ) ;
26+ pub fn new ( ) -> Self {
27+ #[ cfg( feature = "hyper-tls" ) ]
28+ let connector = HttpsConnector :: new ( ) ;
29+
30+ #[ cfg( feature = "hyper-rustls" ) ]
31+ let connector = HttpsConnector :: with_native_roots ( ) ;
2732
28- Client { http_client }
33+ Self {
34+ http_client : hyper:: Client :: builder ( ) . build :: < _ , Body > ( connector) ,
35+ }
2936 }
3037
3138 /// Try sending a `Message` to FCM.
3239 pub async fn send ( & self , message : Message < ' _ > ) -> Result < FcmResponse , FcmError > {
3340 let payload = serde_json:: to_vec ( & message. body ) . unwrap ( ) ;
3441
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 ?;
42+ let request = Request :: builder ( )
43+ . method ( "POST" )
44+ . uri ( "https://fcm.googleapis.com/fcm/send" )
45+ . header ( "Content-Type" , "application/json" )
46+ . header ( "Content-Length" , format ! ( "{}" , payload. len( ) as u64 ) )
47+ . header ( "Athorization" , format ! ( "key={}" , message. api_key) )
48+ . body ( Body :: from ( payload) ) ?;
49+ let response = self . http_client . request ( request) . await ?;
4450
4551 let response_status = response. status ( ) ;
4652
4753 let retry_after = response
4854 . headers ( )
49- . get ( RETRY_AFTER )
55+ . get ( "Retry-After" )
5056 . and_then ( |ra| ra. to_str ( ) . ok ( ) )
5157 . and_then ( |ra| ra. parse :: < RetryAfter > ( ) . ok ( ) ) ;
5258
5359 match response_status {
5460 StatusCode :: OK => {
55- let fcm_response: FcmResponse = response. json ( ) . await . unwrap ( ) ;
61+ let buf = hyper:: body:: to_bytes ( response) . await ?;
62+ let fcm_response: FcmResponse = serde_json:: from_slice ( & buf) ?;
5663
5764 match fcm_response. error {
5865 Some ( ErrorReason :: Unavailable ) => Err ( response:: FcmError :: ServerError ( retry_after) ) ,
0 commit comments