1#![deny(missing_docs)]
5#![deny(unsafe_code)]
6#![warn(clippy::all, clippy::pedantic)]
7
8use crate::error::*;
9
10pub mod config;
11pub mod error;
12pub mod types;
13
14#[cfg(feature = "http")]
15pub mod http;
16
17#[cfg(feature = "websocket")]
18pub mod ws;
19
20#[derive(Clone, Debug)]
22pub struct Client {
23
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
32 pub fn new(config: config::ClientConfig) -> ClientResult<Self> {
34 let tls = config.tls;
35
36 #[cfg(feature = "http")]
37 let http_client = if let Some(http_config) = config.http {
38 Some(std::sync::Arc::new(
39 http::HttpClient::new(http_config, &tls)?
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(
48 ws::WebSocketClient::new(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(|arc| arc.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.ws_client
109 .as_ref()
110 .ok_or(ClientError::ConfigError("WebSocketClient"))?;
111
112 let mut ws_guard = ws_client.lock().await;
113 let client_arc = std::sync::Arc::new(self.clone());
114
115 ws_guard.on_message(move |msg| {
116 callback(msg, std::sync::Arc::clone(&client_arc))
117 });
118
119 Ok(())
120 }
121
122 #[cfg(feature = "websocket")]
144 pub async fn on_message_simple<F>(&self, callback: F) -> ClientResult<()>
145 where
146 F: Fn(ws::types::WebsocketMessage) + Send + Sync + 'static,
147 {
148 let ws_client = self.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.ws_client
162 .as_ref()
163 .ok_or(ClientError::ConfigError("WebsocketConfig"))?;
164
165 let mut ws_guard = ws_client.lock().await;
166 ws_guard.start_background().await?;
167
168 Ok(())
169 }
170
171 #[cfg(feature = "websocket")]
173 pub async fn start_blocking_websocket(&self) -> ClientResult<()> {
174 let ws_client = self.ws_client
175 .as_ref()
176 .ok_or(ClientError::ConfigError("WebsocketConfig"))?;
177
178 let mut ws_guard = ws_client.lock().await;
179 ws_guard.start_blocking().await?;
180
181 Ok(())
182 }
183
184 #[cfg(feature = "websocket")]
186 pub async fn stop_background_websocket(&self) -> ClientResult<()> {
187 let ws_client = self.ws_client
188 .as_ref()
189 .ok_or(ClientError::ConfigError("WebsocketConfig"))?;
190
191 let mut ws_guard = ws_client.lock().await;
192 ws_guard.stop_background().await?;
193
194 Ok(())
195 }
196
197 #[cfg(feature = "websocket")]
199 pub async fn is_websocket_connected(&self) -> bool {
200 let Some(ws_client) = &self.ws_client else {
201 return false;
202 };
203
204 let ws_guard = ws_client.lock().await;
205 ws_guard.is_connected().await
206 }
207
208 #[cfg(feature = "websocket")]
210 pub async fn reconnect_websocket(&self) -> ClientResult<()> {
211 let ws_client = self.ws_client
212 .as_ref()
213 .ok_or(ClientError::NoWebsocketClient)?;
214
215 let ws_guard = ws_client.lock().await;
216 ws_guard.reconnect().await.map_err(ClientError::from)
217 }
218}
219impl Drop for Client {
220 fn drop(&mut self) {
221 }
224}