11pub mod response;
22
3+ use http:: header:: { AUTHORIZATION , CONTENT_LENGTH , CONTENT_TYPE , RETRY_AFTER } ;
4+ use hyper:: { client:: HttpConnector , Body , Request , StatusCode } ;
5+ #[ cfg( feature = "hyper-rustls" ) ]
6+ use hyper_rustls:: HttpsConnector ;
7+ #[ cfg( feature = "hyper-tls" ) ]
8+ use hyper_tls:: HttpsConnector ;
9+
310pub use crate :: client:: response:: * ;
411
512use crate :: message:: Message ;
6- use reqwest:: header:: { AUTHORIZATION , CONTENT_LENGTH , CONTENT_TYPE , RETRY_AFTER } ;
7- use reqwest:: { Body , StatusCode } ;
813
914/// An async client for sending the notification payload.
1015pub struct Client {
11- http_client : reqwest :: Client ,
16+ http_client : hyper :: Client < HttpsConnector < HttpConnector > > ,
1217}
1318
1419impl Default for Client {
@@ -19,28 +24,30 @@ impl Default for Client {
1924
2025impl Client {
2126 /// 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+ pub fn new ( ) -> Self {
28+ #[ cfg( feature = "hyper-tls" ) ]
29+ let connector = HttpsConnector :: new ( ) ;
30+
31+ #[ cfg( feature = "hyper-rustls" ) ]
32+ let connector = HttpsConnector :: with_native_roots ( ) ;
2733
28- Client { http_client }
34+ Self {
35+ http_client : hyper:: Client :: builder ( ) . build :: < _ , Body > ( connector) ,
36+ }
2937 }
3038
3139 /// Try sending a `Message` to FCM.
3240 pub async fn send ( & self , message : Message < ' _ > ) -> Result < FcmResponse , FcmError > {
3341 let payload = serde_json:: to_vec ( & message. body ) . unwrap ( ) ;
3442
35- let request = self
36- . http_client
37- . post ( "https://fcm.googleapis.com/fcm/send" )
43+ let request = Request :: builder ( )
44+ . method ( "POST" )
45+ . uri ( "https://fcm.googleapis.com/fcm/send" )
3846 . 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 ?;
47+ . header ( CONTENT_LENGTH , format ! ( "{}" , payload. len( ) as u64 ) )
48+ . header ( AUTHORIZATION , format ! ( "key={}" , message. api_key) )
49+ . body ( Body :: from ( payload) ) ?;
50+ let response = self . http_client . request ( request) . await ?;
4451
4552 let response_status = response. status ( ) ;
4653
@@ -52,7 +59,8 @@ impl Client {
5259
5360 match response_status {
5461 StatusCode :: OK => {
55- let fcm_response: FcmResponse = response. json ( ) . await . unwrap ( ) ;
62+ let buf = hyper:: body:: to_bytes ( response) . await ?;
63+ let fcm_response: FcmResponse = serde_json:: from_slice ( & buf) ?;
5664
5765 match fcm_response. error {
5866 Some ( ErrorReason :: Unavailable ) => Err ( response:: FcmError :: ServerError ( retry_after) ) ,
0 commit comments