Skip to main content

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(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
65#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
66pub struct HttpSmsSendResponse {
67    /// The unique ID assigned to the already sent message.
68    pub message_id: i64,
69
70    /// Reference ID for tracking the message.
71    pub reference_id: u8,
72}
73
74/// Combine an outgoing message and send response into a dummy `SmsStoredMessage`.
75impl From<(crate::sms::SmsOutgoingMessage, HttpSmsSendResponse)> for crate::sms::SmsMessage {
76    fn from(
77        value: (crate::sms::SmsOutgoingMessage, HttpSmsSendResponse),
78    ) -> crate::sms::SmsMessage {
79        crate::sms::SmsMessage {
80            message_id: Some(value.1.message_id),
81            phone_number: value.0.to,
82            message_content: value.0.content,
83            message_reference: Some(value.1.reference_id),
84            is_outgoing: true,
85            status: None,
86            created_at: None,
87            completed_at: None,
88        }
89    }
90}
91
92/// Network registration status of the modem.
93#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
94#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
95pub struct HttpModemNetworkStatusResponse {
96    /// Registration status code (0=not registered, 1=registered home, 5=registered roaming).
97    pub registration: u8,
98
99    /// Network technology in use (e.g., 2G, 3G, 4G).
100    pub technology: u8,
101}
102
103/// Signal strength information from the modem.
104#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
105#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
106pub struct HttpModemSignalStrengthResponse {
107    /// Received Signal Strength Indicator (0-31, 99=unknown).
108    pub rssi: i32,
109
110    /// Bit Error Rate (0-7, 99=unknown).
111    pub ber: i32,
112}
113
114/// Network operator information from the modem.
115#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
116#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
117pub struct HttpModemNetworkOperatorResponse {
118    /// Operator selection status (0=automatic, 1=manual).
119    pub status: u8,
120
121    /// Format of the operator name (0=long alphanumeric, 1=short alphanumeric, 2=numeric).
122    pub format: u8,
123
124    /// Name or code of the network operator.
125    pub operator: String,
126}
127
128/// Battery status information from the modem.
129#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
130#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
131pub struct HttpModemBatteryLevelResponse {
132    /// Battery status (0=not charging, 1=charging, 2=no battery).
133    pub status: u8,
134
135    /// Battery charge level percentage (0-100).
136    pub charge: u8,
137
138    /// Battery voltage in volts.
139    pub voltage: f32,
140}
141
142/// Formatted device info response, with each value packed into a proper optional response.
143#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
144#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
145pub struct HttpSmsDeviceInfoResponse {
146    /// SMS API version string, including features.
147    pub version: String,
148
149    /// The phone number associated with the SMS device
150    pub phone_number: Option<String>,
151
152    /// The name of the cellular service provider
153    pub service_provider: Option<String>,
154
155    /// Detailed network operator information and capabilities
156    pub network_operator: Option<HttpModemNetworkOperatorResponse>,
157
158    /// Current network connection status and diagnostics
159    pub network_status: Option<HttpModemNetworkStatusResponse>,
160
161    /// Battery level, charging state, and power metrics
162    pub battery: Option<HttpModemBatteryLevelResponse>,
163
164    /// Signal strength measurements and quality indicators
165    pub signal: Option<HttpModemSignalStrengthResponse>,
166}
167
168/// Used in latest-numbers return value, as a number and friendly name.
169#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
170#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
171pub struct LatestNumberFriendlyNamePair {
172    /// Phone number in international format.
173    pub number: String,
174
175    /// Optional friendly name for display purposes.
176    pub friendly_name: Option<String>,
177}
178impl From<(String, Option<String>)> for LatestNumberFriendlyNamePair {
179    fn from(value: (String, Option<String>)) -> Self {
180        Self {
181            number: value.0,
182            friendly_name: value.1,
183        }
184    }
185}