1use reqwest::blocking::Client as SyncClient;
2use reqwest::Client as AsyncClient;
3
4use serde_json::json;
5use std::error::Error;
6pub struct PushinatorClient {
9 api_token: String,
11 base_url: String,
13}
14
15impl PushinatorClient {
16 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 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 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 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}