sms_client/
lib.rs

1//! A client library for SMS-API, via HTTP and an optional websocket connection.
2//! <https://github.com/morgverd/sms-server>
3
4#![deny(missing_docs)]
5#![deny(unsafe_code)]
6#![warn(clippy::all, clippy::pedantic)]
7#![allow(clippy::missing_errors_doc)]
8
9use crate::error::{ClientError, ClientResult};
10
11pub mod config;
12pub mod error;
13pub mod types;
14
15#[cfg(feature = "http")]
16pub mod http;
17
18#[cfg(feature = "websocket")]
19pub mod ws;
20
21/// SMS Client.
22#[derive(Clone, Debug)]
23pub struct Client {
24    #[cfg(feature = "http")]
25    http_client: Option<std::sync::Arc<http::HttpClient>>,
26
27    #[cfg(feature = "websocket")]
28    ws_client: Option<std::sync::Arc<tokio::sync::Mutex<ws::WebSocketClient>>>,
29}
30impl Client {
31    /// Create an SMS client with a connection config.
32    pub fn new(config: config::ClientConfig) -> ClientResult<Self> {
33        let tls = config.tls;
34
35        #[cfg(feature = "http")]
36        let http_client = if let Some(http_config) = config.http {
37            Some(std::sync::Arc::new(http::HttpClient::new(
38                http_config,
39                tls.as_ref(),
40            )?))
41        } else {
42            None
43        };
44
45        #[cfg(feature = "websocket")]
46        let ws_client = config.websocket.map(|ws_config| {
47            std::sync::Arc::new(tokio::sync::Mutex::new(ws::WebSocketClient::new(
48                ws_config, tls,
49            )))
50        });
51
52        Ok(Self {
53            #[cfg(feature = "http")]
54            http_client,
55
56            #[cfg(feature = "websocket")]
57            ws_client,
58        })
59    }
60
61    /// Borrow the optional inner HTTP client.
62    #[cfg(feature = "http")]
63    pub fn http(&self) -> ClientResult<&http::HttpClient> {
64        self.http_client
65            .as_ref()
66            .map(std::convert::AsRef::as_ref)
67            .ok_or(ClientError::ConfigError("HttpClient"))
68    }
69
70    /// Get a cloned Arc to the optional HTTP client for use in async contexts.
71    #[cfg(feature = "http")]
72    pub fn http_arc(&self) -> ClientResult<std::sync::Arc<http::HttpClient>> {
73        self.http_client
74            .clone()
75            .ok_or(ClientError::ConfigError("HttpClient"))
76    }
77
78    /// Set the callback for incoming WebSocket messages. The callback will include the WebSocket
79    /// message and an Arc to the current Client allowing for easy use within the callback!
80    /// This must be called before starting the WebSocket connection.
81    ///
82    /// # Example
83    /// ```
84    /// use sms_client::http::types::HttpOutgoingSmsMessage;
85    /// use sms_client::ws::types::WebsocketMessage;
86    /// use sms_client::Client;
87    /// use log::info;
88    ///
89    /// #[tokio::main]
90    /// async fn main() {
91    ///     let client: Client = unimplemented!("See other examples");
92    ///
93    ///     client.on_message(move |message, client| {
94    ///         match message {
95    ///             WebsocketMessage::IncomingMessage(sms) => {
96    ///                 // Can access client.http() here!
97    ///             },
98    ///             _ => { }
99    ///         }
100    ///     }).await?
101    /// }
102    /// ```
103    #[cfg(feature = "websocket")]
104    pub async fn on_message<F>(&self, callback: F) -> ClientResult<()>
105    where
106        F: Fn(ws::types::WebsocketMessage, std::sync::Arc<Self>) + Send + Sync + 'static,
107    {
108        let ws_client = self
109            .ws_client
110            .as_ref()
111            .ok_or(ClientError::ConfigError("WebSocketClient"))?;
112
113        let mut ws_guard = ws_client.lock().await;
114        let client_arc = std::sync::Arc::new(self.clone());
115
116        ws_guard.on_message(move |msg| callback(msg, std::sync::Arc::clone(&client_arc)));
117
118        Ok(())
119    }
120
121    /// Set the callback for incoming WebSocket messages (simple version without client copy).
122    /// This must be called before starting the WebSocket connection.
123    ///
124    /// # Example
125    /// ```
126    /// use sms_client::Client;
127    /// use sms_client::ws::types::WebsocketMessage;
128    /// use log::info;
129    ///
130    /// #[tokio::main]
131    /// async fn main() {
132    ///     let client: Client = unimplemented!("See other examples");
133    ///
134    ///     client.on_message_simple(move |message| {
135    ///         match message {
136    ///             WebsocketMessage::OutgoingMessage(sms) => info!("Outgoing message: {:?}", sms),
137    ///             _ => { }
138    ///         }
139    ///     }).await?
140    /// }
141    /// ```
142    #[cfg(feature = "websocket")]
143    pub async fn on_message_simple<F>(&self, callback: F) -> ClientResult<()>
144    where
145        F: Fn(ws::types::WebsocketMessage) + Send + Sync + 'static,
146    {
147        let ws_client = self
148            .ws_client
149            .as_ref()
150            .ok_or(ClientError::ConfigError("WebSocketClient"))?;
151
152        let mut ws_guard = ws_client.lock().await;
153        ws_guard.on_message(callback);
154
155        Ok(())
156    }
157
158    /// Start the WebSocket connection.
159    #[cfg(feature = "websocket")]
160    pub async fn start_background_websocket(&self) -> ClientResult<()> {
161        let ws_client = self
162            .ws_client
163            .as_ref()
164            .ok_or(ClientError::ConfigError("WebsocketConfig"))?;
165
166        let mut ws_guard = ws_client.lock().await;
167        ws_guard.start_background().await?;
168
169        Ok(())
170    }
171
172    /// Start the WebSocket connection and block until closed.
173    #[cfg(feature = "websocket")]
174    pub async fn start_blocking_websocket(&self) -> ClientResult<()> {
175        let ws_client = self
176            .ws_client
177            .as_ref()
178            .ok_or(ClientError::ConfigError("WebsocketConfig"))?;
179
180        let mut ws_guard = ws_client.lock().await;
181        ws_guard.start_blocking().await?;
182
183        Ok(())
184    }
185
186    /// Stop the WebSocket connection.
187    #[cfg(feature = "websocket")]
188    pub async fn stop_background_websocket(&self) -> ClientResult<()> {
189        let ws_client = self
190            .ws_client
191            .as_ref()
192            .ok_or(ClientError::ConfigError("WebsocketConfig"))?;
193
194        let mut ws_guard = ws_client.lock().await;
195        ws_guard.stop_background().await?;
196
197        Ok(())
198    }
199
200    /// Check if the WebSocket is currently connected.
201    #[cfg(feature = "websocket")]
202    pub async fn is_websocket_connected(&self) -> bool {
203        let Some(ws_client) = &self.ws_client else {
204            return false;
205        };
206
207        let ws_guard = ws_client.lock().await;
208        ws_guard.is_connected().await
209    }
210
211    /// Force a WebSocket reconnection.
212    #[cfg(feature = "websocket")]
213    pub async fn reconnect_websocket(&self) -> ClientResult<()> {
214        let ws_client = self
215            .ws_client
216            .as_ref()
217            .ok_or(ClientError::NoWebsocketClient)?;
218
219        let ws_guard = ws_client.lock().await;
220        ws_guard.reconnect().await.map_err(ClientError::from)
221    }
222}
223impl Drop for Client {
224    fn drop(&mut self) {
225        // The WebSocket client will handle its own cleanup in its Drop impl
226        // This is just here to ensure proper cleanup ordering.
227    }
228}