termii_rust/blocking/rest/token/
request.rs

1//! Allow business trigger one-time-passwords
2//! across any available messaging channels.
3
4use std::rc::Rc;
5
6use crate::{
7    blocking::http::client,
8    common::{
9        errors,
10        token::request::{RequestTokenRequest, RequestTokenResponse},
11    },
12};
13
14#[derive(Debug)]
15pub struct RequestToken<'a> {
16    api_key: &'a str,
17    client: Rc<client::HttpClient>,
18}
19
20impl<'a> RequestToken<'a> {
21    pub(crate) fn new(api_key: &'a str, client: Rc<client::HttpClient>) -> RequestToken<'a> {
22        RequestToken { api_key, client }
23    }
24
25    /// Send a one time token request.
26    ///
27    /// ## Examples
28    ///
29    /// ```rust
30    /// use termii_rust::{
31    ///     blocking::rest::termii,
32    ///     common::token::{
33    ///         RequestTokenChannel, RequestTokenMessageType, RequestTokenPinType, RequestTokenRequest,
34    ///     },
35    /// };
36    ///
37    /// let client = termii::Termii::new("Your API key");
38    ///
39    /// let otp_request = RequestTokenRequest::new(
40    ///     RequestTokenMessageType::ALPHANUMERIC,
41    ///     String::from("234XXXXXXXXXX"),
42    ///     String::from("Your org sender ID"),
43    ///     RequestTokenChannel::Generic,
44    ///     3 as u8,
45    ///     50 as usize,
46    ///     6 as u8,
47    ///     String::from("< 1234 >"),
48    ///     String::from("Your pin is < 1234 >"),
49    ///     RequestTokenPinType::ALPHANUMERIC,
50    /// );
51    ///
52    /// let response = client.token.request.send(otp_request).unwrap();
53    ///
54    /// println!("{:#?}", response);
55    /// ```
56    pub fn send(
57        &self,
58        mut otp_payload: RequestTokenRequest,
59    ) -> Result<RequestTokenResponse, errors::HttpError> {
60        otp_payload.set_api_key(self.api_key);
61
62        let response = self
63            .client
64            .post("sms/otp/send", None, None, Some(otp_payload))?;
65
66        let otp_response = response_or_error_text_blocking!(response, RequestTokenResponse);
67
68        Ok(otp_response)
69    }
70}