firebase_client/
lib.rs

1pub mod client;
2pub mod error;
3pub mod notification;
4
5#[cfg(test)]
6mod tests {
7    use dotenv::dotenv;
8    use serde_json::json;
9    use std::{convert::TryFrom, error::Error};
10
11    use google_authz::{Client, Credentials};
12    use hyper::{Body, Request, Uri};
13    use hyper_rustls::HttpsConnector;
14
15    #[tokio::test]
16    async fn test_google_authz() -> Result<(), Box<dyn Error>> {
17        dotenv().ok();
18
19        let credentials_file_path = std::env::var("CREDENTIALS_FILE_PATH").unwrap();
20        let project_id = std::env::var("PROJECT_ID").unwrap();
21        let test_token = std::env::var("TEST_TOKEN").unwrap();
22
23        let https = HttpsConnector::with_native_roots();
24        let client = hyper::Client::builder().build::<_, Body>(https);
25        let credentials = Credentials::from_file(
26            credentials_file_path,
27            &["https://www.googleapis.com/auth/firebase.messaging"],
28        );
29
30        let mut client = Client::new_with(client, credentials);
31
32        let uri = Uri::try_from(format!(
33            "https://fcm.googleapis.com/v1/projects/{}/messages:send",
34            project_id
35        ))?;
36
37        let request = Request::builder()
38            .method("POST")
39            .uri(uri)
40            .body(
41                json!({
42                  "message": {
43                    "token": test_token,
44                    "notification": {
45                      "title": "Breaking News",
46                      "body": "New news story available."
47                    },
48                    "data": {
49                      "story_id": "story_12345"
50                    }
51                  }
52                })
53                .to_string()
54                .into(),
55            )
56            .unwrap();
57
58        let _response = client.request(request).await?;
59
60        println!("{:?}", client);
61
62        Ok(())
63    }
64}