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
//! SMS-API HTTP client.
//! This can be used to interface with the HTTP API standalone if required.

use crate::http::error::{HttpError, HttpResult};
use sms_types::gnss::{FixStatus, PositionReport};
use sms_types::http::{
    HttpModemBatteryLevelResponse, HttpModemNetworkOperatorResponse,
    HttpModemNetworkStatusResponse, HttpModemSignalStrengthResponse, HttpPaginationOptions,
    HttpSmsDeviceInfoResponse, HttpSmsSendResponse, LatestNumberFriendlyNamePair,
};
use sms_types::sms::{SmsDeliveryReport, SmsOutgoingMessage};

pub mod error;
pub mod paginator;

/// Take a response from the client, verify that the status code is 200,
/// then read JSON body and ensure success is true and finally return response value.
async fn read_http_response<T>(response: reqwest::Response) -> HttpResult<T>
where
    T: serde::de::DeserializeOwned,
{
    let is_json = response
        .headers()
        .get(reqwest::header::CONTENT_TYPE)
        .and_then(|ct| ct.to_str().ok())
        .is_some_and(|ct| ct.contains("application/json"));

    if is_json {
        // Verify JSON success status.
        let json: serde_json::Value = response.json().await?;
        let success = json
            .get("success")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);

        if !success {
            let message = json
                .get("error")
                .and_then(|v| v.as_str())
                .unwrap_or("Unknown API error!")
                .to_string();

            return Err(HttpError::ApiError(message));
        }

        // Read response field and make into expected value.
        let response_value = json
            .get("response")
            .ok_or(HttpError::MissingResponseField)?;

        return serde_json::from_value(response_value.clone()).map_err(HttpError::JsonError);
    }

    // Return a status error if there isn't any JSON error to use.
    let status = response.status();
    if !status.is_success() {
        let error_text = response
            .text()
            .await
            .unwrap_or_else(|_| "Unknown error!".to_string());

        return Err(HttpError::HttpStatus {
            status: status.as_u16(),
            message: error_text,
        });
    }

    Err(HttpError::MissingResponseField)
}

/// Create a reqwest client with optional TLS configuration.
fn client_builder(config: Option<&crate::config::TLSConfig>) -> HttpResult<reqwest::ClientBuilder> {
    let builder = reqwest::Client::builder();
    let Some(tls_config) = config.as_ref() else {
        return Ok(builder);
    };

    #[cfg(not(any(feature = "http-tls-rustls", feature = "http-tls-native")))]
    {
        let _ = tls_config; // Suppress unused variable warning
        Err(HttpError::TLSError(
            "TLS configuration provided but no TLS features enabled. Enable either 'http-tls-rustls' or 'http-tls-native' feature".to_string()
        ))
    }

    #[cfg(any(feature = "http-tls-rustls", feature = "http-tls-native"))]
    {
        let mut builder = builder;

        // Configure TLS backend
        #[cfg(feature = "http-tls-rustls")]
        {
            builder = builder.use_rustls_tls();
        }

        #[cfg(feature = "http-tls-native")]
        {
            builder = builder.use_native_tls();
        }

        // Load and add certificate
        let certificate = load_certificate(&tls_config.certificate)?;
        Ok(builder.add_root_certificate(certificate))
    }
}

/// Load a certificate filepath, returning the certificate set for builder.
#[cfg(any(feature = "http-tls-rustls", feature = "http-tls-native"))]
fn load_certificate(cert_path: &std::path::Path) -> HttpResult<reqwest::tls::Certificate> {
    let cert_data = std::fs::read(cert_path).map_err(HttpError::IOError)?;

    // Try to parse based on file extension first
    if let Some(ext) = cert_path.extension().and_then(|s| s.to_str()) {
        match ext {
            "pem" => return Ok(reqwest::tls::Certificate::from_pem(&cert_data)?),
            "der" => return Ok(reqwest::tls::Certificate::from_der(&cert_data)?),
            "crt" => {
                if cert_data.starts_with(b"-----BEGIN") {
                    return Ok(reqwest::tls::Certificate::from_pem(&cert_data)?);
                } else {
                    return Ok(reqwest::tls::Certificate::from_der(&cert_data)?);
                }
            }
            _ => {} // Fall through to auto-detection
        }
    }

    // Auto-detect format: try PEM first, then DER
    reqwest::tls::Certificate::from_pem(&cert_data)
        .or_else(|_| reqwest::tls::Certificate::from_der(&cert_data))
        .map_err(Into::into)
}

/// SMS-API HTTP interface client.
#[derive(Debug)]
pub struct HttpClient {
    base_url: reqwest::Url,
    authorization: Option<String>,
    modem_timeout: Option<std::time::Duration>,
    client: reqwest::Client,
}
impl HttpClient {
    /// Create a new HTTP client that uses the `base_url`.
    pub fn new(
        config: crate::config::HttpConfig,
        tls: Option<&crate::config::TLSConfig>,
    ) -> HttpResult<Self> {
        let client = client_builder(tls)?.timeout(config.base_timeout).build()?;

        Ok(Self {
            base_url: reqwest::Url::parse(config.url.as_str())?,
            authorization: config.authorization,
            modem_timeout: config.modem_timeout,
            client,
        })
    }

    /// Set/Remove the friendly name for a given phone number.
    pub async fn set_friendly_name(
        &self,
        phone_number: impl Into<String>,
        friendly_name: Option<impl Into<String>>,
    ) -> HttpResult<bool> {
        let body = serde_json::json!({
            "phone_number": phone_number.into(),
            "friendly_name": friendly_name.map(Into::into)
        });

        let url = self.base_url.join("/db/friendly-names/set")?;
        let response = self
            .setup_request(false, self.client.post(url))
            .json(&body)
            .send()
            .await?;

        read_http_response(response).await
    }

    /// Get the friendly name associated with a given phone number.
    pub async fn get_friendly_name(
        &self,
        phone_number: impl Into<String>,
    ) -> HttpResult<Option<String>> {
        let body = serde_json::json!({
            "phone_number": phone_number.into()
        });

        let url = self.base_url.join("/db/friendly-names/get")?;
        let response = self
            .setup_request(false, self.client.post(url))
            .json(&body)
            .send()
            .await?;

        read_http_response(response).await
    }

    /// Get messages sent to and from a given phone number.
    /// Pagination options are supported.
    pub async fn get_messages(
        &self,
        phone_number: impl Into<String>,
        pagination: Option<HttpPaginationOptions>,
    ) -> HttpResult<Vec<sms_types::sms::SmsMessage>> {
        let mut body = serde_json::json!({
            "phone_number": phone_number.into()
        });
        if let Some(pagination) = pagination {
            pagination.add_to_body(&mut body);
        }

        let url = self.base_url.join("/db/messages")?;
        let response = self
            .setup_request(false, self.client.post(url))
            .json(&body)
            .send()
            .await?;

        read_http_response(response).await
    }

    /// Get the latest phone numbers that have been in contact with the SMS-API.
    /// This includes both senders and receivers. Pagination options are supported.
    pub async fn get_latest_numbers(
        &self,
        pagination: Option<HttpPaginationOptions>,
    ) -> HttpResult<Vec<LatestNumberFriendlyNamePair>> {
        let url = self.base_url.join("/db/latest-numbers")?;
        let mut request = self.setup_request(false, self.client.post(url));

        // Only add a JSON body if there are pagination options.
        if let Some(pagination) = pagination {
            request = request.json(&pagination);
        }

        let response = request.send().await?;
        read_http_response(response).await
    }

    /// Get received delivery reports for a given `message_id` (comes from `send_sms` etc).
    /// Pagination options are supported.
    pub async fn get_delivery_reports(
        &self,
        message_id: i64,
        pagination: Option<HttpPaginationOptions>,
    ) -> HttpResult<Vec<SmsDeliveryReport>> {
        let mut body = serde_json::json!({
            "message_id": message_id
        });
        if let Some(pagination) = pagination {
            pagination.add_to_body(&mut body);
        }

        let url = self.base_url.join("/db/delivery-reports")?;
        let response = self
            .setup_request(false, self.client.post(url))
            .json(&body)
            .send()
            .await?;

        read_http_response(response).await
    }

    /// Send an SMS message to a target `phone_number`. The result will contain the
    /// message reference (provided from modem) and message id (used internally).
    /// This will use the message timeout for the request if one is set.
    pub async fn send_sms(&self, message: &SmsOutgoingMessage) -> HttpResult<HttpSmsSendResponse> {
        let url = self.base_url.join("/sms/send")?;

        // Create request, applying request timeout if one is set (+ 5).
        // The timeout is enforced by the server, so the additional buffer is to allow for slow networking.
        let mut request = self.setup_request(true, self.client.post(url));
        if let Some(timeout) = message.timeout {
            request = request.timeout(std::time::Duration::from_secs(u64::from(timeout) + 5));
        }

        let response = request.json(message).send().await?;
        read_http_response(response).await
    }

    /// Get the carrier network status.
    pub async fn get_network_status(&self) -> HttpResult<HttpModemNetworkStatusResponse> {
        self.modem_request("/sms/modem-status").await
    }

    /// Get the modem signal strength for the connected tower.
    pub async fn get_signal_strength(&self) -> HttpResult<HttpModemSignalStrengthResponse> {
        self.modem_request("/sms/signal-strength").await
    }

    /// Get the underlying network operator, this is often the same across
    /// multiple service providers for a given region. Eg: vodafone.
    pub async fn get_network_operator(&self) -> HttpResult<HttpModemNetworkOperatorResponse> {
        self.modem_request("/sms/network-operator").await
    }

    /// Get the SIM service provider, this is the brand that manages the contract.
    /// This matters less than the network operator, as they're just resellers. Eg: ASDA Mobile.
    pub async fn get_service_provider(&self) -> HttpResult<String> {
        self.modem_request("/sms/service-provider").await
    }

    /// Get the Modem Hat's battery level, which is used for GNSS warm starts.
    pub async fn get_battery_level(&self) -> HttpResult<HttpModemBatteryLevelResponse> {
        self.modem_request("/sms/battery-level").await
    }

    /// Get the GNSS module's fix status, indicating location data capabilities.
    /// If GNSS is disabled/unavailable this will likely be `FixStatus::Unknown`.
    pub async fn get_gnss_status(&self) -> HttpResult<FixStatus> {
        self.modem_request("/gnss/status").await
    }

    /// Get the GNSS module's current location (`PositionReport`).
    /// If GNSS is disabled/unavailable some values be None, others may be Some(0.00).
    /// This depends on the SIM chip being used.
    pub async fn get_gnss_location(&self) -> HttpResult<PositionReport> {
        self.modem_request("/gnss/location").await
    }

    /// Get device info summary result. This is a more efficient way to request all device info.
    pub async fn get_device_info(&self) -> HttpResult<HttpSmsDeviceInfoResponse> {
        let url = self.base_url.join("/sms/device-info")?;
        let response = self
            .setup_request(true, self.client.get(url))
            .send()
            .await?;

        read_http_response(response).await
    }

    /// Get the configured sender SMS number. This should be used primarily for client identification.
    /// This is optional, as the API could have left this un-configured without any value set.
    pub async fn get_phone_number(&self) -> HttpResult<Option<String>> {
        let url = self.base_url.join("/sys/phone-number")?;
        let response = self
            .setup_request(false, self.client.get(url))
            .send()
            .await?;

        read_http_response(response).await
    }

    /// Get the modem SMS-API version string. This will be a semver format,
    /// often with feature names added as a suffix, eg: "0.0.1+sentry".
    pub async fn get_version(&self) -> HttpResult<String> {
        let url = self.base_url.join("/sys/version")?;
        let response = self
            .setup_request(false, self.client.get(url))
            .send()
            .await?;

        read_http_response(response).await
    }

    /// Set the server log level during runtime. This is useful for live debugging.
    pub async fn set_log_level(&self, level: impl Into<String>) -> HttpResult<bool> {
        let body = serde_json::json!({
            "level": level.into()
        });

        let url = self.base_url.join("/sys/set-log-level")?;
        let response = self
            .setup_request(false, self.client.post(url))
            .json(&body)
            .send()
            .await?;

        read_http_response(response).await
    }

    /// Send an SMS modem request, returning inner response value.
    async fn modem_request<T>(&self, route: &str) -> HttpResult<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let url = self.base_url.join(route)?;
        let response = self
            .setup_request(true, self.client.get(url))
            .send()
            .await?;

        read_http_response::<T>(response).await
    }

    /// Allow for a different timeout to be used for modem requests,
    /// and apply optional authorization header to request builder.
    fn setup_request(
        &self,
        is_modem: bool,
        builder: reqwest::RequestBuilder,
    ) -> reqwest::RequestBuilder {
        let builder = if is_modem && let Some(timeout) = &self.modem_timeout {
            builder.timeout(*timeout)
        } else {
            builder
        };
        if let Some(auth) = &self.authorization {
            builder.header("authorization", auth)
        } else {
            builder
        }
    }
}