Skip to main content

pushover_rs/
lib.rs

1#[cfg(test)]
2mod tests;
3mod pushover;
4
5use pushover::constants::PUSHOVER_API_ENDPOINT;
6pub use pushover::data::{MessageBuilder, AttachmentMessageBuilder};
7pub use pushover::data::PushoverSound;
8pub use pushover::data::Message;
9pub use pushover::data::AttachmentMessage;
10pub use pushover::data::PushoverResponse;
11
12/// Send a push notification without attachment (non-blocking)
13pub async fn send_pushover_request(message: Message) -> Result<PushoverResponse, Box<dyn std::error::Error>> {
14    let client: reqwest::Client = reqwest::Client::new();
15    let response: reqwest::Response = client
16        .post(PUSHOVER_API_ENDPOINT)
17        .json(&message)
18        .send()
19        .await?;
20    PushoverResponse::try_from_reqwest_response(response).await
21}
22
23/// Send a push notification with attachment (! blocking)
24pub fn send_pushover_request_with_attachment(message: AttachmentMessage) -> Result<PushoverResponse, Box<dyn std::error::Error>> {
25    let client: reqwest::blocking::Client = reqwest::blocking::Client::new();
26    let form: reqwest::blocking::multipart::Form = message.into_form()?;
27    let response: Result<reqwest::blocking::Response, reqwest::Error> = client
28        .post(PUSHOVER_API_ENDPOINT)
29        .multipart(form)
30        .send();
31    PushoverResponse::try_from_blocking_reqwest_response(response?)
32}
33
34/// Send a push notification with attachment asynchronously (non-blocking)
35pub async fn send_pushover_request_with_attachment_async(message: AttachmentMessage) -> Result<PushoverResponse, Box<dyn std::error::Error>> {
36    let client: reqwest::Client = reqwest::Client::new();
37    let form: reqwest::multipart::Form = message.into_form_async().await?;
38    let response: Result<reqwest::Response, reqwest::Error> = client
39        .post(PUSHOVER_API_ENDPOINT)
40        .multipart(form)
41        .send()
42        .await;
43    PushoverResponse::try_from_reqwest_response(response?).await
44}