Skip to main content

pushinator/
lib.rs

1use reqwest::blocking::Client as SyncClient;
2use reqwest::Client as AsyncClient;
3
4use serde_json::json;
5use std::error::Error;
6/// A client for interacting with the Pushinator API.
7
8pub struct PushinatorClient {
9    /// Pushinator API token used for authentication.
10    api_token: String,
11    /// Base URL of the Pushinator API.
12    base_url: String,
13}
14
15impl PushinatorClient {
16    /// Creates a new `PushinatorClient` instance with the default Pushinator API base URL.
17    ///
18    /// # Arguments
19    /// * `api_token` - the Pushinator API token for authentication.
20    ///
21    /// # Returns
22    /// A new instance of `PushinatorClient`.
23    pub fn new(api_token: String) -> Self {
24        PushinatorClient {
25            api_token: api_token.to_string(),
26            base_url: "https://api.pushinator.com".to_string(),
27        }
28    }
29
30    /// Creates a new `PushinatorClient` instance with a custom base URL, typically for testing purposes.
31    ///
32    /// # Arguments
33    /// * `api_token` - the API token for authentication.
34    /// * `mock_url` - the mock URL for the API.
35    ///
36    /// # Returns
37    /// A new instance of `PushinatorClient`.
38    pub fn new_test(api_token: String, mock_url: String) -> Self {
39        PushinatorClient {
40            api_token: api_token.to_string(),
41            base_url: mock_url,
42        }
43    }
44
45    /// Sends a notification to a specific channel synchronously.
46    ///
47    /// # Arguments
48    /// * `channel_id` - The ID of the channel to send the notification to.
49    /// * `notification` - notification message.
50    ///
51    /// # Returns
52    /// * `Ok(())` if the notification was sent successfully.
53    /// * `Err` containing an error if the operation failed.
54    pub fn send_notification_sync(
55        &self,
56        channel_id: String,
57        notification: &str,
58    ) -> Result<(), Box<dyn Error>> {
59        let api_url = format!("{}/api/v2/notifications/send", self.base_url);
60
61        let client = SyncClient::new();
62
63        let response = client
64            .post(api_url)
65            .header("Authorization", format!("Bearer {}", self.api_token))
66            .header("Content-Type", "application/json")
67            .header("User-Agent", "pushinator-rust/1.0")
68            .json(&json!({
69                "channel_id": channel_id,
70                "content": notification
71            }))
72            .send()?;
73
74        if response.status().is_success() {
75            Ok(())
76        } else {
77            let status = response.status();
78            let error_body = response.text().unwrap();
79            Err(From::from(format!(
80                "Failed to send notification. Status: {}, Body: {}",
81                status, error_body
82            )))
83        }
84    }
85
86    /// Sends a notification to a specific channel asynchronously.
87    ///
88    /// # Arguments
89    /// * `channel_id` - The ID of the channel to send the notification to.
90    /// * `notification` - notification message.
91    ///
92    /// # Returns
93    /// * `Ok(())` if the notification was sent successfully.
94    /// * `Err` containing an error if the operation failed.
95    pub async fn send_notification(
96        &self,
97        channel_id: String,
98        notification: &str,
99    ) -> Result<(), Box<dyn Error>> {
100        let api_url = format!("{}/api/v2/notifications/send", self.base_url);
101
102        let client = AsyncClient::new();
103
104        let response = client
105            .post(api_url)
106            .header("Authorization", format!("Bearer {}", self.api_token))
107            .header("Content-Type", "application/json")
108            .header("User-Agent", "pushinator-rust/1.0")
109            .json(&json!({
110                "channel_id": channel_id,
111                "content": notification
112            }))
113            .send()
114            .await?;
115
116        if response.status().is_success() {
117            Ok(())
118        } else {
119            let status = response.status();
120            let error_body = response.text().await?;
121            Err(From::from(format!(
122                "Failed to send notification. Status: {}, Body: {}",
123                status, error_body
124            )))
125        }
126    }
127}