diff --git a/Cargo.toml b/Cargo.toml index c0ebb2c8f..7607c679d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,20 +14,20 @@ keywords = ["fcm", "firebase", "notification"] edition = "2018" [features] -default = ["native-tls"] -native-tls = ["reqwest/native-tls"] -rustls = ["reqwest/rustls-tls"] -vendored-tls = ["reqwest/native-tls-vendored"] +default = ["rustls"] +rustls = ["awc/rustls"] +openssl = ["awc/openssl"] + [dependencies] serde = { version = "1", features = ["derive"] } serde_json = "1" erased-serde = "0.3" -reqwest = {version = "0.11.0", features = ["json"], default-features=false} +awc = "3.0.0-beta" chrono = "0.4" log = "0.4" [dev-dependencies] argparse = "0.2.1" -tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } +actix-rt = "2.2.0" pretty_env_logger = "0.3" diff --git a/examples/simple_sender.rs b/examples/simple_sender.rs index 0256c3e12..185695d38 100644 --- a/examples/simple_sender.rs +++ b/examples/simple_sender.rs @@ -7,7 +7,7 @@ struct CustomData { message: &'static str, } -#[tokio::main] +#[actix_rt::main] async fn main() -> Result<(), Box> { pretty_env_logger::init(); diff --git a/src/client/mod.rs b/src/client/mod.rs index debdb5ebe..8c752b453 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -1,46 +1,35 @@ pub mod response; +use awc::http::{header::{CONTENT_TYPE, CONTENT_LENGTH, AUTHORIZATION, RETRY_AFTER}, StatusCode}; + pub use crate::client::response::*; use crate::message::Message; -use reqwest::header::{AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE, RETRY_AFTER}; -use reqwest::{Body, StatusCode}; /// An async client for sending the notification payload. +#[derive(Default)] pub struct Client { - http_client: reqwest::Client, -} - -impl Default for Client { - fn default() -> Self { - Self::new() - } + http_client: awc::Client, } impl Client { /// Get a new instance of Client. pub fn new() -> Client { - let http_client = reqwest::ClientBuilder::new() - .pool_max_idle_per_host(std::usize::MAX) - .build() - .unwrap(); - - Client { http_client } + Default::default() } /// Try sending a `Message` to FCM. pub async fn send(&self, message: Message<'_>) -> Result { let payload = serde_json::to_vec(&message.body).unwrap(); - let request = self + let mut response = self .http_client .post("https://fcm.googleapis.com/fcm/send") - .header(CONTENT_TYPE, "application/json") - .header(CONTENT_LENGTH, format!("{}", payload.len() as u64).as_bytes()) - .header(AUTHORIZATION, format!("key={}", message.api_key).as_bytes()) - .body(Body::from(payload)) - .build()?; - let response = self.http_client.execute(request).await?; + .insert_header((CONTENT_TYPE, "application/json")) + .insert_header((CONTENT_LENGTH, format!("{}", payload.len() as u64).as_bytes())) + .insert_header((AUTHORIZATION, format!("key={}", message.api_key).as_bytes())) + .send_body(payload) + .await?; let response_status = response.status(); diff --git a/src/client/response.rs b/src/client/response.rs index 6da22f3b7..670f11fa9 100644 --- a/src/client/response.rs +++ b/src/client/response.rs @@ -1,3 +1,4 @@ +use awc::error::SendRequestError; pub use chrono::{DateTime, Duration, FixedOffset}; use serde::Deserialize; use std::{error::Error, fmt, str::FromStr}; @@ -158,8 +159,8 @@ impl fmt::Display for FcmError { } } -impl From for FcmError { - fn from(_: reqwest::Error) -> Self { +impl From for FcmError { + fn from(_: SendRequestError) -> Self { Self::ServerError(None) } }