firebae_cm/client/
client.rs1use reqwest::header::{AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE};
2
3use crate::{FcmResponse, Message};
4
5pub struct Client {
7 client: reqwest::Client,
8}
9
10impl Default for Client {
11 fn default() -> Self {
12 Self::new()
13 }
14}
15
16impl Client {
17 pub fn new() -> Self {
19 let client = reqwest::ClientBuilder::new()
20 .use_rustls_tls()
21 .build()
22 .unwrap();
23
24 Self { client }
25 }
26
27 pub async fn send(&self, message: Message) -> crate::Result<String> {
44 let payload = serde_json::to_vec(&message)?;
45
46 let fcm_response = self
47 .client
48 .post(format!(
49 "https://fcm.googleapis.com/v1/projects/{}/messages:send",
50 message.project_id
51 ))
52 .header(CONTENT_TYPE, "application/json; UTF-8")
53 .header(CONTENT_LENGTH, payload.len())
54 .header(AUTHORIZATION, format!("Bearer {}", message.jwt).as_bytes())
55 .json(&message)
56 .send()
57 .await?
58 .json()
59 .await?;
60
61 match fcm_response {
62 FcmResponse::Error(e) => Err(crate::Error::FcmError(e)),
63 FcmResponse::Success(s) => Ok(s),
64 }
65 }
66}