1
1
pub mod response;
2
2
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
+
3
9
pub use crate :: client:: response:: * ;
4
10
5
11
use crate :: message:: Message ;
6
- use reqwest:: header:: { AUTHORIZATION , CONTENT_LENGTH , CONTENT_TYPE , RETRY_AFTER } ;
7
- use reqwest:: { Body , StatusCode } ;
8
12
9
13
/// An async client for sending the notification payload.
10
- pub struct Client {
11
- http_client : reqwest:: Client ,
12
- }
14
+ pub struct Client { }
13
15
14
16
impl Default for Client {
15
17
fn default ( ) -> Self {
@@ -19,40 +21,42 @@ impl Default for Client {
19
21
20
22
impl Client {
21
23
/// 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 { }
29
26
}
30
27
31
28
/// Try sending a `Message` to FCM.
32
29
pub async fn send ( & self , message : Message < ' _ > ) -> Result < FcmResponse , FcmError > {
33
30
let payload = serde_json:: to_vec ( & message. body ) . unwrap ( ) ;
34
31
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 ?;
44
47
45
48
let response_status = response. status ( ) ;
46
49
47
50
let retry_after = response
48
51
. headers ( )
49
- . get ( RETRY_AFTER )
52
+ . get ( "Retry-After" )
50
53
. and_then ( |ra| ra. to_str ( ) . ok ( ) )
51
54
. and_then ( |ra| ra. parse :: < RetryAfter > ( ) . ok ( ) ) ;
52
55
53
56
match response_status {
54
57
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) ?;
56
60
57
61
match fcm_response. error {
58
62
Some ( ErrorReason :: Unavailable ) => Err ( response:: FcmError :: ServerError ( retry_after) ) ,
0 commit comments