1#![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#[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 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 }
228}