Skip to content

Switch to Actix (awc) #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion examples/simple_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct CustomData {
message: &'static str,
}

#[tokio::main]
#[actix_rt::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
pretty_env_logger::init();

Expand Down
33 changes: 11 additions & 22 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -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<FcmResponse, FcmError> {
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();

Expand Down
5 changes: 3 additions & 2 deletions src/client/response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use awc::error::SendRequestError;
pub use chrono::{DateTime, Duration, FixedOffset};
use serde::Deserialize;
use std::{error::Error, fmt, str::FromStr};
Expand Down Expand Up @@ -158,8 +159,8 @@ impl fmt::Display for FcmError {
}
}

impl From<reqwest::Error> for FcmError {
fn from(_: reqwest::Error) -> Self {
impl From<SendRequestError> for FcmError {
fn from(_: SendRequestError) -> Self {
Self::ServerError(None)
}
}
Expand Down