sms_types/
http.rs

1//! HTTP interface related request/response types.
2
3use serde::{Deserialize, Serialize};
4
5/// HTTP pagination options allow for lazy reading of large sets of data,
6/// for example if thousands of messages have been sent and received from
7/// a phone number it would be impractical to request all of them at the
8/// same time, instead it can be read in shorter pages using limit+offset.
9/// This is applied at the server level when requesting data from database.
10#[derive(Serialize, PartialEq, Default, Debug, Clone, Copy)]
11pub struct HttpPaginationOptions {
12    /// The maximum amount of return values.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub limit: Option<u64>,
15
16    /// The offset in index to start getting values from.
17    /// Eg, if the limit was 5, and you want to view page 2,
18    /// the offset would be 5, then 10, 15, ...
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub offset: Option<u64>,
21
22    /// Should return values be reversed? This is useful for getting the
23    /// first results from a large set without having to know it's size.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub reverse: Option<bool>,
26}
27impl HttpPaginationOptions {
28    /// Set the limit/page size.
29    #[must_use]
30    pub fn with_limit(mut self, limit: u64) -> Self {
31        self.limit = Some(limit);
32        self
33    }
34
35    /// Set request position offset.
36    #[must_use]
37    pub fn with_offset(mut self, offset: u64) -> Self {
38        self.offset = Some(offset);
39        self
40    }
41
42    /// Set the reverse state for options.
43    #[must_use]
44    pub fn with_reverse(mut self, reverse: bool) -> Self {
45        self.reverse = Some(reverse);
46        self
47    }
48
49    /// Add pagination options to a json Value.
50    pub fn add_to_body(&self, body: &mut serde_json::Value) {
51        if let Some(limit) = self.limit {
52            body["limit"] = serde_json::json!(limit);
53        }
54        if let Some(offset) = self.offset {
55            body["offset"] = serde_json::json!(offset);
56        }
57        if let Some(reverse) = self.reverse {
58            body["reverse"] = serde_json::json!(reverse);
59        }
60    }
61}
62
63/// Response returned after sending an SMS message.
64#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
65pub struct HttpSmsSendResponse {
66    /// The unique ID assigned to the already sent message.
67    pub message_id: i64,
68
69    /// Reference ID for tracking the message.
70    pub reference_id: u8,
71}
72
73/// Combine an outgoing message and send response into a dummy `SmsStoredMessage`.
74impl From<(crate::sms::SmsOutgoingMessage, HttpSmsSendResponse)> for crate::sms::SmsMessage {
75    fn from(
76        value: (crate::sms::SmsOutgoingMessage, HttpSmsSendResponse),
77    ) -> crate::sms::SmsMessage {
78        crate::sms::SmsMessage {
79            message_id: Some(value.1.message_id),
80            phone_number: value.0.to,
81            message_content: value.0.content,
82            message_reference: Some(value.1.reference_id),
83            is_outgoing: true,
84            status: None,
85            created_at: None,
86            completed_at: None,
87        }
88    }
89}
90
91/// Network registration status of the modem.
92#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
93pub struct HttpModemNetworkStatusResponse {
94    /// Registration status code (0=not registered, 1=registered home, 5=registered roaming).
95    pub registration: u8,
96
97    /// Network technology in use (e.g., 2G, 3G, 4G).
98    pub technology: u8,
99}
100
101/// Signal strength information from the modem.
102#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
103pub struct HttpModemSignalStrengthResponse {
104    /// Received Signal Strength Indicator (0-31, 99=unknown).
105    pub rssi: u8,
106
107    /// Bit Error Rate (0-7, 99=unknown).
108    pub ber: u8,
109}
110
111/// Network operator information from the modem.
112#[derive(Deserialize, PartialEq, Debug, Clone)]
113pub struct HttpModemNetworkOperatorResponse {
114    /// Operator selection status (0=automatic, 1=manual).
115    pub status: u8,
116
117    /// Format of the operator name (0=long alphanumeric, 1=short alphanumeric, 2=numeric).
118    pub format: u8,
119
120    /// Name or code of the network operator.
121    pub operator: String,
122}
123
124/// Battery status information from the modem.
125#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
126pub struct HttpModemBatteryLevelResponse {
127    /// Battery status (0=not charging, 1=charging, 2=no battery).
128    pub status: u8,
129
130    /// Battery charge level percentage (0-100).
131    pub charge: u8,
132
133    /// Battery voltage in volts.
134    pub voltage: f32,
135}
136
137/// The raw `DeviceInfoResponse` with raw values.
138#[derive(Deserialize, PartialEq, Debug, Clone)]
139pub struct HttpSmsDeviceInfoResponse {
140    /// SMS API version string, including features.
141    pub version: String,
142
143    /// The phone number associated with the SMS device
144    pub phone_number: Option<String>,
145
146    /// The name of the cellular service provider
147    pub service_provider: Option<String>,
148
149    /// Network operator information as (code1, code2, `operator_name`)
150    pub network_operator: Option<(u8, u8, String)>,
151
152    /// Current network connection status as (`status_code`, `strength_indicator`)
153    pub network_status: Option<(u8, u8)>,
154
155    /// Battery information as (`level_percentage`, `charging_status`, voltage)
156    pub battery: Option<(u8, u8, f32)>,
157
158    /// Signal strength information as (`strength_level`, `quality_indicator`)
159    pub signal: Option<(u8, u8)>,
160}
161
162/// Formatted device info response, with each value packed into a proper optional response.
163#[derive(Deserialize, PartialEq, Debug, Clone)]
164pub struct HttpSmsDeviceInfoData {
165    /// SMS API version string, including features.
166    pub version: String,
167
168    /// The phone number associated with the SMS device
169    pub phone_number: Option<String>,
170
171    /// The name of the cellular service provider
172    pub service_provider: Option<String>,
173
174    /// Detailed network operator information and capabilities
175    pub network_operator: Option<HttpModemNetworkOperatorResponse>,
176
177    /// Current network connection status and diagnostics
178    pub network_status: Option<HttpModemNetworkStatusResponse>,
179
180    /// Battery level, charging state, and power metrics
181    pub battery: Option<HttpModemBatteryLevelResponse>,
182
183    /// Signal strength measurements and quality indicators
184    pub signal: Option<HttpModemSignalStrengthResponse>,
185}
186impl From<HttpSmsDeviceInfoResponse> for HttpSmsDeviceInfoData {
187    fn from(value: HttpSmsDeviceInfoResponse) -> HttpSmsDeviceInfoData {
188        HttpSmsDeviceInfoData {
189            version: value.version,
190            phone_number: value.phone_number,
191            service_provider: value.service_provider,
192            network_operator: value
193                .network_operator
194                .map(|v| HttpModemNetworkOperatorResponse {
195                    status: v.0,
196                    format: v.1,
197                    operator: v.2,
198                }),
199            network_status: value
200                .network_status
201                .map(|v| HttpModemNetworkStatusResponse {
202                    registration: v.0,
203                    technology: v.1,
204                }),
205            battery: value.battery.map(|v| HttpModemBatteryLevelResponse {
206                status: v.0,
207                charge: v.1,
208                voltage: v.2,
209            }),
210            signal: value.signal.map(|v| HttpModemSignalStrengthResponse {
211                rssi: v.0,
212                ber: v.1,
213            }),
214        }
215    }
216}
217
218/// Used in latest-numbers return value, as a number and friendly name.
219pub type LatestNumberFriendlyNamePair = (String, Option<String>);