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/// The outgoing SMS message to be sent to a target number.
64#[derive(Serialize, PartialEq, Default, Debug, Clone)]
65pub struct HttpOutgoingSmsMessage {
66 /// The target phone number, this should be in international format.
67 pub to: String,
68
69 /// The full message content. This will be split into multiple messages
70 /// by the server if required. This also supports Unicode emojis etc.
71 pub content: String,
72
73 /// The relative validity period to use for message sending. This determines
74 /// how long the message should remain waiting while undelivered.
75 /// By default, this is determined by the server (24 hours).
76 #[serde(skip_serializing_if = "Option::is_none")]
77 pub validity_period: Option<u8>,
78
79 /// Should the SMS message be sent as a Silent class? This makes a popup
80 /// show on the users device with the message content if they're logged in.
81 #[serde(skip_serializing_if = "Option::is_none")]
82 pub flash: Option<bool>,
83
84 /// A timeout that should be applied to the entire request.
85 /// If one is not set, the default timeout is used.
86 #[serde(skip_serializing_if = "Option::is_none")]
87 pub timeout: Option<u32>,
88}
89impl HttpOutgoingSmsMessage {
90 /// Create a new outgoing message with a default validity period and no flash.
91 /// The default validity period is applied by SMS-API, so usually 24 hours.
92 pub fn simple_message(to: impl Into<String>, content: impl Into<String>) -> Self {
93 Self {
94 to: to.into(),
95 content: content.into(),
96 ..Default::default()
97 }
98 }
99
100 /// Set the message flash state. This will show a popup if the recipient is
101 /// logged-in to their phone, otherwise as a normal text message.
102 #[must_use]
103 pub fn with_flash(mut self, flash: bool) -> Self {
104 self.flash = Some(flash);
105 self
106 }
107
108 /// Set a relative validity period value.
109 #[must_use]
110 pub fn with_validity_period(mut self, period: u8) -> Self {
111 self.validity_period = Some(period);
112 self
113 }
114
115 /// Set a request timeout value.
116 #[must_use]
117 pub fn with_timeout(mut self, timeout: u32) -> Self {
118 self.timeout = Some(timeout);
119 self
120 }
121}
122
123/// Response returned after sending an SMS message.
124#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
125pub struct HttpSmsSendResponse {
126 /// The unique ID assigned to the already sent message.
127 pub message_id: i64,
128
129 /// Reference ID for tracking the message.
130 pub reference_id: u8,
131}
132
133/// Delivery report for an already sent SMS message.
134#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
135pub struct HttpSmsDeliveryReport {
136 /// Unique identifier for this delivery report.
137 pub report_id: Option<i64>,
138
139 /// Delivery status code from the network.
140 pub status: u8,
141
142 /// Whether this is the final delivery report for the message.
143 pub is_final: bool,
144
145 /// Unix timestamp when this report was created.
146 pub created_at: Option<u32>,
147}
148
149/// Network registration status of the modem.
150#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
151pub struct HttpModemNetworkStatusResponse {
152 /// Registration status code (0=not registered, 1=registered home, 5=registered roaming).
153 pub registration: u8,
154
155 /// Network technology in use (e.g., 2G, 3G, 4G).
156 pub technology: u8,
157}
158
159/// Signal strength information from the modem.
160#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
161pub struct HttpModemSignalStrengthResponse {
162 /// Received Signal Strength Indicator (0-31, 99=unknown).
163 pub rssi: u8,
164
165 /// Bit Error Rate (0-7, 99=unknown).
166 pub ber: u8,
167}
168
169/// Network operator information from the modem.
170#[derive(Deserialize, PartialEq, Debug, Clone)]
171pub struct HttpModemNetworkOperatorResponse {
172 /// Operator selection status (0=automatic, 1=manual).
173 pub status: u8,
174
175 /// Format of the operator name (0=long alphanumeric, 1=short alphanumeric, 2=numeric).
176 pub format: u8,
177
178 /// Name or code of the network operator.
179 pub operator: String,
180}
181
182/// Battery status information from the modem.
183#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
184pub struct HttpModemBatteryLevelResponse {
185 /// Battery status (0=not charging, 1=charging, 2=no battery).
186 pub status: u8,
187
188 /// Battery charge level percentage (0-100).
189 pub charge: u8,
190
191 /// Battery voltage in volts.
192 pub voltage: f32,
193}
194
195/// Combine an outgoing message and send response into a dummy `SmsStoredMessage`.
196impl From<(HttpOutgoingSmsMessage, HttpSmsSendResponse)> for crate::sms::SmsMessage {
197 fn from(value: (HttpOutgoingSmsMessage, HttpSmsSendResponse)) -> crate::sms::SmsMessage {
198 crate::sms::SmsMessage {
199 message_id: Some(value.1.message_id),
200 phone_number: value.0.to,
201 message_content: value.0.content,
202 message_reference: Some(value.1.reference_id),
203 is_outgoing: true,
204 status: None,
205 created_at: None,
206 completed_at: None,
207 }
208 }
209}
210
211/// The raw `DeviceInfoResponse` with raw values.
212#[derive(Deserialize, PartialEq, Debug, Clone)]
213pub struct HttpSmsDeviceInfoResponse {
214 /// SMS API version string, including features.
215 pub version: String,
216
217 /// The phone number associated with the SMS device
218 pub phone_number: Option<String>,
219
220 /// The name of the cellular service provider
221 pub service_provider: Option<String>,
222
223 /// Network operator information as (code1, code2, `operator_name`)
224 pub network_operator: Option<(u8, u8, String)>,
225
226 /// Current network connection status as (`status_code`, `strength_indicator`)
227 pub network_status: Option<(u8, u8)>,
228
229 /// Battery information as (`level_percentage`, `charging_status`, voltage)
230 pub battery: Option<(u8, u8, f32)>,
231
232 /// Signal strength information as (`strength_level`, `quality_indicator`)
233 pub signal: Option<(u8, u8)>,
234}
235
236/// Formatted device info response, with each value packed into a proper optional response.
237#[derive(Deserialize, PartialEq, Debug, Clone)]
238pub struct HttpSmsDeviceInfoData {
239 /// SMS API version string, including features.
240 pub version: String,
241
242 /// The phone number associated with the SMS device
243 pub phone_number: Option<String>,
244
245 /// The name of the cellular service provider
246 pub service_provider: Option<String>,
247
248 /// Detailed network operator information and capabilities
249 pub network_operator: Option<HttpModemNetworkOperatorResponse>,
250
251 /// Current network connection status and diagnostics
252 pub network_status: Option<HttpModemNetworkStatusResponse>,
253
254 /// Battery level, charging state, and power metrics
255 pub battery: Option<HttpModemBatteryLevelResponse>,
256
257 /// Signal strength measurements and quality indicators
258 pub signal: Option<HttpModemSignalStrengthResponse>,
259}
260impl From<HttpSmsDeviceInfoResponse> for HttpSmsDeviceInfoData {
261 fn from(value: HttpSmsDeviceInfoResponse) -> HttpSmsDeviceInfoData {
262 HttpSmsDeviceInfoData {
263 version: value.version,
264 phone_number: value.phone_number,
265 service_provider: value.service_provider,
266 network_operator: value
267 .network_operator
268 .map(|v| HttpModemNetworkOperatorResponse {
269 status: v.0,
270 format: v.1,
271 operator: v.2,
272 }),
273 network_status: value
274 .network_status
275 .map(|v| HttpModemNetworkStatusResponse {
276 registration: v.0,
277 technology: v.1,
278 }),
279 battery: value.battery.map(|v| HttpModemBatteryLevelResponse {
280 status: v.0,
281 charge: v.1,
282 voltage: v.2,
283 }),
284 signal: value.signal.map(|v| HttpModemSignalStrengthResponse {
285 rssi: v.0,
286 ber: v.1,
287 }),
288 }
289 }
290}
291
292/// Used in latest-numbers return value, as a number and friendly name.
293pub type LatestNumberFriendlyNamePair = (String, Option<String>);