Skip to content

Commit

Permalink
Remove useFcmV1 (#125)
Browse files Browse the repository at this point in the history
* Remove unnecessary code for Expo client initialization

* Bump version to 0.4.1 in Cargo.toml
  • Loading branch information
katayama8000 authored Aug 11, 2024
1 parent 34c80ce commit e9f02c6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "expo_push_notification_client"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
readme = "README.md"
authors = ["katayama8000 <https://github.com/katayama8000>"]
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage};
// Initialize Expo client
let expo = Expo::new(ExpoClientOptions {
access_token: Some(access_token),
use_fcm_v1: Some(false), // Set to true to use FCM v1 API
});

// Define Expo Push Tokens to send notifications to
Expand Down
121 changes: 16 additions & 105 deletions src/expo_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,26 @@ pub struct Expo {
access_token: Option<String>,
base_url: String,
client: reqwest::Client,
use_fcm_v1: Option<bool>,
}

#[derive(Clone, Debug, Default)]
pub struct ExpoClientOptions {
pub access_token: Option<String>,
pub use_fcm_v1: Option<bool>,
}

impl Expo {
pub fn new(options: ExpoClientOptions) -> Self {
Self::new_with_base_url(options.access_token, "https://exp.host", options.use_fcm_v1)
Self::new_with_base_url(options.access_token, "https://exp.host")
}

pub fn new_with_base_url(
access_token: Option<String>,
base_url: &str,
use_fcm_v1: Option<bool>,
) -> Self {
pub fn new_with_base_url(access_token: Option<String>, base_url: &str) -> Self {
Self {
access_token,
base_url: base_url.to_string(),
client: reqwest::Client::builder()
.gzip(true)
.build()
.expect("Client::new()"),
use_fcm_v1,
}
}

Expand Down Expand Up @@ -90,7 +83,7 @@ impl Expo {
/// # "#,
/// # )
/// # .create();
/// # let expo = Expo::new_with_base_url(None, &server.url(), None);
/// # let expo = Expo::new_with_base_url(None, &server.url());
/// #
/// let response = expo
/// .send_push_notifications(
Expand All @@ -115,13 +108,9 @@ impl Expo {
where
R: TryIntoSendPushNotificationsRequest,
{
let mut path = String::from("/--/api/v2/push/send");
if let Some(use_fcm_v1) = self.use_fcm_v1 {
path.push_str(&format!("?useFcmV1={}", use_fcm_v1));
}
let request = request.try_into_send_push_notifications_request()?;
let response: SendPushNotificationSuccessfulResponse = self
.send_request(Method::POST, path.as_str(), request)
.send_request(Method::POST, "/--/api/v2/push/send", request)
.await?;
Ok(response.data)
}
Expand Down Expand Up @@ -154,7 +143,7 @@ impl Expo {
/// # "#,
/// # )
/// # .create();
/// # let expo = Expo::new_with_base_url(None, &server.url(), None);
/// # let expo = Expo::new_with_base_url(None, &server.url());
/// let receipt_ids = expo.get_push_notification_receipts([
/// "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
/// ]).await?;
Expand Down Expand Up @@ -321,7 +310,7 @@ mod tests {
)
.create();

let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());

let response = expo
.get_push_notification_receipts([
Expand Down Expand Up @@ -373,7 +362,7 @@ mod tests {
"#,
)
.create();
let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());
let response = expo
.get_push_notification_receipts(["XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"])
.await?;
Expand Down Expand Up @@ -413,7 +402,7 @@ mod tests {
)
.create();

let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());

let result = expo
.get_push_notification_receipts(["XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"])
Expand Down Expand Up @@ -458,7 +447,7 @@ mod tests {
)
.create();

let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());
let receipts = expo.get_push_notification_receipts(ids).await?;
assert_eq!(receipts, {
let mut map = HashMap::new();
Expand Down Expand Up @@ -503,7 +492,7 @@ mod tests {
.await?,
)
.create();
let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());
let receipts = expo.get_push_notification_receipts(ids).await?;
assert_eq!(receipts, {
let mut map = HashMap::new();
Expand Down Expand Up @@ -537,7 +526,7 @@ mod tests {
"#,
)
.create();
let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());
let response = expo
.send_push_notifications(
ExpoPushMessage::builder(["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"]).build()?,
Expand Down Expand Up @@ -576,7 +565,7 @@ mod tests {
)
.create();

let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());

let response = expo
.send_push_notifications([
Expand Down Expand Up @@ -627,7 +616,7 @@ mod tests {
)
.create();

let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());

let response = expo
.send_push_notifications(
Expand Down Expand Up @@ -667,7 +656,7 @@ mod tests {
)
.create();

let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());

let result = expo
.send_push_notifications(
Expand Down Expand Up @@ -790,7 +779,7 @@ mod tests {
.await?,
)
.create();
let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());

let response = expo
.send_push_notifications(ExpoPushMessage::builder(to).build()?)
Expand Down Expand Up @@ -835,7 +824,7 @@ mod tests {
.await?,
)
.create();
let expo = Expo::new_with_base_url(None, &server.url(), None);
let expo = Expo::new_with_base_url(None, &server.url());

let response = expo
.send_push_notifications(ExpoPushMessage::builder(to).build()?)
Expand All @@ -850,84 +839,6 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn test_send_push_notifications_with_new_api() -> anyhow::Result<()> {
let mut server = mockito::Server::new_async().await;
let mock = server
.mock("POST", "/--/api/v2/push/send?useFcmV1=true")
.match_header("accept-encoding", "gzip")
.match_header("content-type", "application/json")
.match_body(r#"{"to":["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"]}"#)
.with_status(200)
.with_header("content-type", "application/json; charset=utf-8")
.with_body(
r#"
{
"data": [
{ "status": "ok", "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" }
]
}
"#,
)
.create();

let expo = Expo::new_with_base_url(None, &server.url(), Some(true));

let response = expo
.send_push_notifications(
ExpoPushMessage::builder(["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"]).build()?,
)
.await?;

assert_eq!(
response,
vec![ExpoPushTicket::Ok(ExpoPushSuccessTicket {
id: ExpoPushReceiptId::from_str("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")?
})]
);
mock.assert();
Ok(())
}

#[tokio::test]
async fn test_send_push_notifications_with_legacy_api() -> anyhow::Result<()> {
let mut server = mockito::Server::new_async().await;
let mock = server
.mock("POST", "/--/api/v2/push/send?useFcmV1=false")
.match_header("accept-encoding", "gzip")
.match_header("content-type", "application/json")
.match_body(r#"{"to":["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"]}"#)
.with_status(200)
.with_header("content-type", "application/json; charset=utf-8")
.with_body(
r#"
{
"data": [
{ "status": "ok", "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" }
]
}
"#,
)
.create();

let expo = Expo::new_with_base_url(None, &server.url(), Some(false));

let response = expo
.send_push_notifications(
ExpoPushMessage::builder(["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"]).build()?,
)
.await?;

assert_eq!(
response,
vec![ExpoPushTicket::Ok(ExpoPushSuccessTicket {
id: ExpoPushReceiptId::from_str("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")?
})]
);
mock.assert();
Ok(())
}

async fn gzip(src: &[u8]) -> std::io::Result<Vec<u8>> {
let mut encoder = GzipEncoder::new(vec![]);
tokio::io::AsyncWriteExt::write_all(&mut encoder, src).await?;
Expand Down

0 comments on commit e9f02c6

Please sign in to comment.