linkmobility/
rest_api.rs

1use crate::address::Address;
2use crate::client::Client;
3use crate::error::Result;
4use crate::request::{RequestBuilder, Response};
5use crate::ty::{Boolean, Currency, Date, Integer, KeyValue, Long, Priority, String, ToN, DCS};
6use serde::Serialize;
7
8#[derive(Serialize)]
9pub struct PlatformID(String);
10
11impl PlatformID {
12    pub fn new(id: impl Into<String>) -> Self {
13        Self(id.into())
14    }
15}
16
17#[derive(Serialize)]
18pub struct PlatformPartnerID(String);
19
20impl PlatformPartnerID {
21    pub fn new(id: impl Into<String>) -> Self {
22        Self(id.into())
23    }
24}
25
26#[derive(Serialize)]
27pub struct SMS {
28    /// Required. This is the source number from where the
29    /// message should be sent. The format is depending on
30    /// the specified sourceTON.
31    pub source: Address,
32
33    /// This is the source type of number. See allowed TON
34    /// values below.
35    /// Default ALPHANUMERIC
36    #[serde(skip_serializing_if = "Option::is_none")]
37    #[serde(rename = "sourceTON")]
38    pub source_ton: Option<ToN>,
39
40    /// Required. This is the destination number. The format is
41    /// depending on the specified destinationTON.
42    /// Remember that MSISDNS include the country code and
43    /// a leading plus sign. (+)
44    pub destination: Address,
45
46    /// This is the destination type of number. See allowed
47    /// TON values below.
48    /// Default MSISDN.
49    #[serde(skip_serializing_if = "Option::is_none")]
50    #[serde(rename = "destinationTON")]
51    pub destination_ton: Option<ToN>,
52
53    /// Advanced.
54    /// This is the Data Coding Scheme that should be used
55    /// when sending the SMS. See allowed DCS values in a
56    /// separate table.
57    ///Default TEXT.
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub dcs: Option<DCS>,
60
61    /// Advanced.
62    /// This value may be specified when sending
63    /// concatenated SMS, WAP-push, etc. The format is hex
64    /// encoded 8-bit bytes. More information about valid
65    /// UDH for long SMS may be given by Support upon
66    /// request. Common will handle the splitting and
67    /// concatenation of messages if you do not have a
68    /// specific reason to do it yourself.
69    #[serde(skip_serializing_if = "Option::is_none")]
70    #[serde(rename = "userDataHeader")]
71    pub user_data_header: Option<String>,
72
73    //// This is the message content itself. The DCS specifies
74    /// the format (encoding) on this value.
75    /// Note that messages that messages of more than 140
76    /// bytes must be split into multiple messages. Common
77    /// will do that automatically by default.
78    #[serde(rename = "userData")]
79    pub user_data: String,
80
81    /// True indicates that a delivery report should be sent
82    /// back when the message has come to a final state.
83    /// (Delivered or failed)
84    /// TRUE is mandatory for premium messages.
85    /// Defaults to TRUE, and it is recommended to use
86    /// delivery reports.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    #[serde(rename = "useDeliveryReport")]
89    pub use_deliver_report: Option<Boolean>,
90
91    /// One or more gates that should be used for sending
92    /// delivery reports. If you do not specify any Gates to
93    /// deliver Delivery Reports to, make sure to set
94    /// useDeliveryReport to FALSE. See the chapter on
95    /// delivery reports for more information. Required for
96    /// premium messages.
97    #[serde(skip_serializing_if = "Option::is_none")]
98    #[serde(rename = "deliveryReportGates")]
99    pub deliver_report_gates: Option<Box<[String]>>,
100
101    /// This specifies how long the message is supposed to
102    /// live. If the message takes longer to deliver to the
103    /// handset than the validityTime, the message will be
104    /// discarded.
105    /// The value is specified in milliseconds.
106    /// Default is 48 hours (172800000).
107    #[serde(skip_serializing_if = "Option::is_none")]
108    #[serde(rename = "relativeValidityTime")]
109    pub relative_validity_time: Option<Long>,
110
111    /// The absolute time when a message should expire.
112    /// Minimum 15 minutes and maximum 48h in the future.
113    /// Formatted according to RFC3339, e.g. 2010-03-30T12:59:40+02:00.
114    /// Overrides relativeValidityTime if both are set.
115    #[serde(skip_serializing_if = "Option::is_none")]
116    #[serde(rename = "absoluteValidityTime")]
117    pub absolute_validity_time: Option<Date>,
118
119    /// Price, in local currency, in 1/100 of currency. For
120    /// example, to send a message costing 5 NOK, this should
121    /// be set to 500.
122    /// If you are splitting a long message into multiple
123    /// segments yourself, set price only on the first segment.
124    /// Default 0.
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub tariff: Option<Integer>,
127
128    /// The currency should be set if the default country
129    /// currency not to be used. Supported currencies are
130    /// NOK, SEK, DKK, EUR, LTL.
131    /// Ignored for non-premium messages
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub currency: Option<Currency>,
134
135    /// Allowed age for (adult) content.
136    /// Optional. Not supported by all operators
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub age: Option<Integer>,
139
140    /// See the Priority value table, Optional.
141    #[serde(skip_serializing_if = "Option::is_none")]
142    pub priority: Option<Priority>,
143
144    /// Your platformId. Provided to you by Support.
145    #[serde(rename = "platformId")]
146    pub platform_id: PlatformID,
147
148    /// Your platformPartnerId. Provided to you by Support.
149    #[serde(rename = "platformPartnerId")]
150    pub platform_partner_id: PlatformPartnerID,
151
152    /// Your own internal transaction ID. Not used for
153    /// anything except as a reference.
154    /// Optional.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    #[serde(rename = "refId")]
157    pub ref_id: Option<String>,
158
159    /// When sending premium messages, a description of the
160    /// service. This will be printed on the end-user’s phone
161    /// bill.
162    /// Ignored for non-premium messages.
163    #[serde(skip_serializing_if = "Option::is_none")]
164    #[serde(rename = "productDescription")]
165    pub product_description: Option<String>,
166
167    /// When sending premium messages, specify which
168    /// category the service is. This lets the operator know
169    /// which rates to apply to the message. Support or your
170    /// sales contact will help you determine the correct
171    /// productCategory to set.
172    /// Ignored for non-premium messages.
173    #[serde(skip_serializing_if = "Option::is_none")]
174    #[serde(rename = "productCategory")]
175    pub product_category: Option<Integer>,
176
177    /// A reference to the ID of the MO message which
178    /// triggered the MT message. Required for some
179    /// operators.
180    #[serde(skip_serializing_if = "Option::is_none")]
181    #[serde(rename = "moReferenceId")]
182    pub mo_reference_id: Option<String>,
183
184    /// Advanced. Additional parameters may be specified if
185    /// needed.
186    /// Support will advise you if you need to use custom
187    /// parameters.
188    #[serde(skip_serializing_if = "Option::is_none")]
189    #[serde(rename = "customParameters")]
190    pub custom_parameters: Option<KeyValue>,
191
192    /// Indicates whether you want a response in the body
193    /// when you submit the message. This is not a delivery
194    /// report, only a confirmation of message submission.
195    /// Default is false.
196    #[serde(skip_serializing_if = "Option::is_none")]
197    #[serde(rename = "ignoreResponse")]
198    pub ignore_response: Option<Boolean>,
199}
200
201impl SMS {
202    pub fn with_text(
203        source: Address,
204        destination: Address,
205        platform_id: PlatformID,
206        platform_partner_id: PlatformPartnerID,
207        text: impl Into<String>,
208    ) -> Self {
209        Self {
210            source,
211            source_ton: None,
212            destination,
213            destination_ton: None,
214            dcs: None,
215            user_data_header: None,
216            user_data: text.into(),
217            use_deliver_report: None,
218            deliver_report_gates: None,
219            relative_validity_time: None,
220            absolute_validity_time: None,
221            tariff: None,
222            currency: None,
223            age: None,
224            priority: None,
225            platform_id,
226            platform_partner_id,
227            ref_id: None,
228            product_description: None,
229            product_category: None,
230            mo_reference_id: None,
231            custom_parameters: None,
232            ignore_response: None,
233        }
234    }
235
236    pub async fn send(&self, client: &Client) -> Result<Response> {
237        Ok(RequestBuilder::post(client, "send")?
238            .body(serde_json::to_string(self)?)
239            .execute()
240            .await?)
241    }
242}