sms-client 3.1.0

Rust client library for sms-server - send/receive SMS through your own GSM hardware with HTTP/WebSocket support and no API subscriptions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! SMS-Client connection configuration.

/// HTTP-specific configuration.
#[cfg(feature = "http")]
#[derive(Clone, Debug)]
pub struct HttpConfig {
    /// HTTP base URL. eg: <http://192.168.1.2:3000>
    pub url: String,

    /// Optional HTTP authorization header token.
    pub authorization: Option<String>,

    /// A default timeout to apply to all requests that do not have
    /// their own timeout (this applies to all if `modem_timeout` is None,
    /// otherwise only database and sys queries).
    pub base_timeout: std::time::Duration,

    /// An optional timeout to use specifically for modem requests
    /// (requests that must send and receive modem data). This should
    /// be higher than the default timeout as they can take longer.
    pub modem_timeout: Option<std::time::Duration>,
}
#[cfg(feature = "http")]
impl HttpConfig {
    /// The default amount of seconds before an HTTP request should time out.
    /// If there is no `modem_timeout`, this is applied to all requests.
    pub const HTTP_DEFAULT_BASE_TIMEOUT: u64 = 5;

    /// The default amount of seconds before an HTTP request that interacts directly
    /// with the modem should time out. This should be longer to allow for carrier response.
    pub const HTTP_DEFAULT_MODEM_TIMEOUT: u64 = 20;

    /// Create a new HTTP configuration with default settings.
    #[must_use]
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            authorization: None,
            base_timeout: std::time::Duration::from_secs(Self::HTTP_DEFAULT_BASE_TIMEOUT),
            modem_timeout: Some(std::time::Duration::from_secs(
                Self::HTTP_DEFAULT_MODEM_TIMEOUT,
            )),
        }
    }

    /// Set the authorization token.
    #[must_use]
    pub fn with_auth(mut self, token: impl Into<String>) -> Self {
        self.authorization = Some(token.into());
        self
    }

    /// Set the base request timeout.
    #[must_use]
    pub fn with_base_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.base_timeout = timeout;
        self
    }

    /// Set the modem request timeout.
    #[must_use]
    pub fn with_modem_timeout(mut self, timeout: Option<std::time::Duration>) -> Self {
        self.modem_timeout = timeout;
        self
    }
}
#[cfg(feature = "http")]
impl Default for HttpConfig {
    fn default() -> Self {
        Self {
            url: "http://localhost:3000".to_string(),
            authorization: None,
            base_timeout: std::time::Duration::from_secs(Self::HTTP_DEFAULT_BASE_TIMEOUT),
            modem_timeout: Some(std::time::Duration::from_secs(
                Self::HTTP_DEFAULT_MODEM_TIMEOUT,
            )),
        }
    }
}

/// WebSocket-specific configuration.
#[cfg(feature = "websocket")]
#[derive(Clone, Debug)]
pub struct WebSocketConfig {
    /// Websocket event channel URL. eg: ws://192.168.1.2:3000/ws
    pub url: String,

    /// Optional Websocket authorization header token.
    pub authorization: Option<String>,

    /// Should the websocket connection automatically reconnect if disconnected.
    pub auto_reconnect: bool,

    /// Interval to use between reconnection attempts.
    pub reconnect_interval: std::time::Duration,

    /// The interval between sending websocket pings.
    pub ping_interval: std::time::Duration,

    /// Timeout duration for missing pings.
    pub ping_timeout: std::time::Duration,

    /// Maximum reconnection attempts (None = unlimited).
    pub max_reconnect_attempts: Option<u32>,

    /// Optional set of events that should be listened to. This is added to
    /// the websocket connection URI, and the server filters out events before
    /// sending them. By default, all events are sent when none are selected.
    pub filtered_events: Option<Vec<String>>,
}
#[cfg(feature = "websocket")]
impl WebSocketConfig {
    /// The default interval to use between connection attempts.
    /// Sequential attempts use a backoff up to 60 seconds.
    pub const WS_DEFAULT_RECONNECT_INTERVAL: u64 = 5;

    /// The interval between sending ping messages.
    pub const WS_DEFAULT_PING_INTERVAL: u64 = 10;

    /// The duration between the last ping to count as a timeout.
    pub const WS_DEFAULT_PING_TIMEOUT: u64 = 30;

    /// Create a new WebSocket configuration with default settings.
    #[must_use]
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            authorization: None,
            auto_reconnect: true,
            reconnect_interval: std::time::Duration::from_secs(Self::WS_DEFAULT_RECONNECT_INTERVAL),
            ping_interval: std::time::Duration::from_secs(Self::WS_DEFAULT_PING_INTERVAL),
            ping_timeout: std::time::Duration::from_secs(Self::WS_DEFAULT_PING_TIMEOUT),
            max_reconnect_attempts: None,
            filtered_events: None,
        }
    }

    /// Set the authorization token.
    #[must_use]
    pub fn with_auth(mut self, token: impl Into<String>) -> Self {
        self.authorization = Some(token.into());
        self
    }

    /// Enable or disable auto-reconnection.
    #[must_use]
    pub fn with_auto_reconnect(mut self, enabled: bool) -> Self {
        self.auto_reconnect = enabled;
        self
    }

    /// Set the reconnection interval.
    #[must_use]
    pub fn with_reconnect_interval(mut self, interval: std::time::Duration) -> Self {
        self.reconnect_interval = interval;
        self
    }

    /// Set the ping interval.
    #[must_use]
    pub fn with_ping_interval(mut self, interval: std::time::Duration) -> Self {
        self.ping_interval = interval;
        self
    }

    /// Set the ping timeout.
    #[must_use]
    pub fn with_ping_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.ping_timeout = timeout;
        self
    }

    /// Set maximum reconnection attempts (None = unlimited).
    #[must_use]
    pub fn with_max_reconnect_attempts(mut self, max_attempts: Option<u32>) -> Self {
        self.max_reconnect_attempts = max_attempts;
        self
    }

    /// Set filtered listen events, this is included in the connection query-string.
    /// The provided Vec should contain every event name that should be sent by the server.
    /// If None, filtering is disabled so all events are sent.
    #[must_use]
    pub fn with_filtered_events(mut self, events: Option<Vec<impl Into<String>>>) -> Self {
        self.filtered_events = events.map(|events| events.into_iter().map(Into::into).collect());
        self
    }
}
#[cfg(feature = "websocket")]
impl Default for WebSocketConfig {
    fn default() -> Self {
        Self {
            url: "ws://localhost:3000/ws".to_string(),
            authorization: None,
            auto_reconnect: true,
            reconnect_interval: std::time::Duration::from_secs(Self::WS_DEFAULT_RECONNECT_INTERVAL),
            ping_interval: std::time::Duration::from_secs(Self::WS_DEFAULT_PING_INTERVAL),
            ping_timeout: std::time::Duration::from_secs(Self::WS_DEFAULT_PING_TIMEOUT),
            max_reconnect_attempts: None,
            filtered_events: None,
        }
    }
}

/// WebSocket and HTTP TLS configuration.
#[derive(Clone, Debug)]
pub struct TLSConfig {
    /// TLS certificate filepath.
    pub certificate: std::path::PathBuf,
}
impl TLSConfig {
    /// Set a certificate filepath to use for TLS connections.
    pub fn new(certificate: impl Into<std::path::PathBuf>) -> crate::error::ClientResult<Self> {
        Ok(Self {
            certificate: Self::verify_path(&certificate.into())?,
        })
    }

    /// Verify certificate filepath. Returning Path it's a valid filepath, and it has an appropriate extension.
    fn verify_path(path: &std::path::Path) -> crate::error::ClientResult<std::path::PathBuf> {
        if !path.exists() {
            return Err(crate::error::ClientError::ConfigError(
                "Certificate filepath does not exist",
            ));
        }
        if !path.is_file() {
            return Err(crate::error::ClientError::ConfigError(
                "Certificate filepath is not a file",
            ));
        }
        let canonical_path = path
            .canonicalize()
            .map_err(|_| crate::error::ClientError::ConfigError("Invalid certificate path"))?;

        // Check file extension.
        match path.extension().and_then(|s| s.to_str()) {
            Some("pem" | "crt" | "der") => Ok(canonical_path),
            _ => Err(crate::error::ClientError::ConfigError(
                "Invalid certificate file extension",
            )),
        }
    }
}

/// Complete client configuration.
#[derive(Clone, Debug)]
pub struct ClientConfig {
    /// TLS configuration, used for both HTTP and WebSocket connections.
    pub tls: Option<TLSConfig>,

    /// HTTP configuration.
    #[cfg(feature = "http")]
    pub http: Option<HttpConfig>,

    /// Optional WebSocket configuration.
    #[cfg(feature = "websocket")]
    pub websocket: Option<WebSocketConfig>,
}
impl ClientConfig {
    /// Create a new configuration with only HTTP support.
    ///
    /// # Example
    /// ```
    /// use sms_client::config::ClientConfig;
    ///
    /// let config = ClientConfig::http_only("http://192.168.1.2:3000");
    /// ```
    #[cfg(feature = "http")]
    #[must_use]
    pub fn http_only(url: impl Into<String>) -> Self {
        Self {
            tls: None,
            http: Some(HttpConfig::new(url)),

            #[cfg(feature = "websocket")]
            websocket: None,
        }
    }

    /// Create a new configuration with only WebSocket support.
    ///
    /// # Example
    /// ```
    /// use sms_client::config::ClientConfig;
    ///
    /// let config = ClientConfig::websocket_only("ws://192.168.1.2:3000/ws");
    /// ```
    #[cfg(feature = "websocket")]
    #[must_use]
    pub fn websocket_only(ws_url: impl Into<String>) -> Self {
        Self {
            tls: None,

            #[cfg(feature = "http")]
            http: None,

            websocket: Some(WebSocketConfig::new(ws_url)),
        }
    }

    /// Create a new configuration with both HTTP and WebSocket support.
    ///
    /// # Example
    /// ```
    /// use sms_client::config::ClientConfig;
    ///
    /// let config = ClientConfig::both(
    ///     "http://192.168.1.2:3000",
    ///     "ws://192.168.1.2:3000/ws"
    /// );
    /// ```
    #[cfg(feature = "http")]
    #[cfg(feature = "websocket")]
    #[must_use]
    pub fn both(http_url: impl Into<String>, ws_url: impl Into<String>) -> Self {
        Self {
            tls: None,
            http: Some(HttpConfig::new(http_url)),
            websocket: Some(WebSocketConfig::new(ws_url)),
        }
    }

    /// Create a configuration from individual HTTP and WebSocket configs.
    ///
    /// # Example
    /// ```
    /// use std::time::Duration;
    /// use sms_client::config::{ClientConfig, HttpConfig, WebSocketConfig};
    ///
    /// let http = HttpConfig::new("http://192.168.1.2:3000")
    ///     .with_auth("token123")
    ///     .with_base_timeout(Duration::from_secs(30));
    ///
    /// let ws = WebSocketConfig::new("ws://192.168.1.2:3000/ws")
    ///     .with_auth("token123")
    ///     .with_auto_reconnect(true)
    ///     .with_max_reconnect_attempts(Some(10));
    ///
    /// let config = ClientConfig::from_parts(Some(http), Some(ws));
    /// ```
    #[cfg(feature = "http")]
    #[cfg(feature = "websocket")]
    #[must_use]
    pub fn from_parts(http: Option<HttpConfig>, websocket: Option<WebSocketConfig>) -> Self {
        Self {
            tls: None,
            http,
            websocket,
        }
    }

    /// Add TLS configuration.
    #[must_use]
    pub fn add_tls(mut self, tls: TLSConfig) -> Self {
        self.tls = Some(tls);
        self
    }

    /// Set authorization for both HTTP and WebSocket.
    /// This only sets the authorization value for components that already exist.
    ///
    /// # Example
    /// ```
    /// use sms_client::config::ClientConfig;
    ///
    /// let config = ClientConfig::both(
    ///     "http://192.168.1.2:3000",
    ///     "ws://192.168.1.2:3000/ws"
    /// ).with_auth("my-token");
    /// ```
    #[must_use]
    pub fn with_auth(mut self, token: impl Into<String>) -> Self {
        let token = token.into();

        #[cfg(feature = "http")]
        if let Some(http) = &mut self.http {
            http.authorization = Some(token.clone());
        }

        #[cfg(feature = "websocket")]
        if let Some(ws) = &mut self.websocket {
            ws.authorization = Some(token);
        }
        self
    }

    /// Modify/Set a `TLSConfig` with certificate filepath.
    ///
    /// # Example
    /// ```
    /// use sms_client::config::ClientConfig;
    ///
    /// let config = ClientConfig::http_only("https://192.168.1.2:3000")
    ///     .with_certificate("./certificate.crt")?;
    pub fn with_certificate(
        mut self,
        certificate: impl Into<std::path::PathBuf>,
    ) -> crate::error::ClientResult<Self> {
        if let Some(tls) = &mut self.tls {
            tls.certificate = TLSConfig::verify_path(&certificate.into())?;
        } else {
            self.tls = Some(TLSConfig::new(certificate)?);
        }
        Ok(self)
    }

    /// Configure the HTTP component if present.
    ///
    /// # Example
    /// ```
    /// use sms_client::config::ClientConfig;
    /// use std::time::Duration;
    ///
    /// let config = ClientConfig::http_only("http://192.168.1.2:3000")
    ///     .configure_http(|http| {
    ///         http.with_base_timeout(Duration::from_secs(30))
    ///             .with_auth("token")
    ///     });
    /// ```
    #[cfg(feature = "http")]
    #[must_use]
    pub fn configure_http<F>(mut self, f: F) -> Self
    where
        F: FnOnce(HttpConfig) -> HttpConfig,
    {
        if let Some(http) = self.http {
            self.http = Some(f(http));
        }
        self
    }

    /// Configure the WebSocket component if present.
    ///
    /// # Example
    /// ```
    /// use sms_client::config::ClientConfig;
    /// use std::time::Duration;
    ///
    /// let config = ClientConfig::both(
    ///     "http://192.168.1.2:3000",
    ///     "ws://192.168.1.2:3000/ws"
    /// ).configure_websocket(|ws| {
    ///     ws.with_ping_interval(Duration::from_secs(60))
    ///       .with_max_reconnect_attempts(Some(5))
    /// });
    /// ```
    #[cfg(feature = "websocket")]
    #[must_use]
    pub fn configure_websocket<F>(mut self, f: F) -> Self
    where
        F: FnOnce(WebSocketConfig) -> WebSocketConfig,
    {
        if let Some(ws) = self.websocket {
            self.websocket = Some(f(ws));
        }
        self
    }

    /// Add WebSocket configuration.
    ///
    /// # Example
    /// ```
    /// use sms_client::config::{ClientConfig, WebSocketConfig};
    ///
    /// let config = ClientConfig::http_only("http://192.168.1.2:3000")
    ///     .add_websocket(WebSocketConfig::new("ws://192.168.1.2:3000/ws"));
    /// ```
    #[cfg(feature = "websocket")]
    #[must_use]
    pub fn add_websocket(mut self, websocket: WebSocketConfig) -> Self {
        self.websocket = Some(websocket);
        self
    }
}
impl Default for ClientConfig {
    fn default() -> Self {
        Self {
            tls: None,

            #[cfg(feature = "http")]
            http: Some(HttpConfig::default()),

            #[cfg(feature = "websocket")]
            websocket: Some(WebSocketConfig::default()),
        }
    }
}

#[cfg(feature = "http")]
#[allow(clippy::needless_update)]
impl From<HttpConfig> for ClientConfig {
    fn from(http: HttpConfig) -> Self {
        ClientConfig {
            tls: None,
            http: Some(http),
            ..Default::default()
        }
    }
}

#[cfg(feature = "websocket")]
#[allow(clippy::needless_update)]
impl From<WebSocketConfig> for ClientConfig {
    fn from(ws: WebSocketConfig) -> Self {
        ClientConfig {
            tls: None,
            websocket: Some(ws),
            ..Default::default()
        }
    }
}