termii_rust/blocking/rest/token/
in_app_token.rs

1//! In-App token are numeric or alpha-numeric codes generated to authenticate
2//! login requests and verify customer transactions.
3
4use std::rc::Rc;
5
6use crate::{
7    blocking::http::client,
8    common::{
9        errors,
10        token::in_app_token::{InAppTokenRequest, InAppTokenResponse},
11    },
12};
13
14#[derive(Debug)]
15pub struct InAppToken<'a> {
16    api_key: &'a str,
17    client: Rc<client::HttpClient>,
18}
19
20impl<'a> InAppToken<'a> {
21    pub(crate) fn new(api_key: &'a str, client: Rc<client::HttpClient>) -> InAppToken<'a> {
22        InAppToken { api_key, client }
23    }
24
25    /// Fetch JSON In-App otp's.
26    ///
27    /// ## Examples
28    ///
29    /// ```rust
30    /// use termii_rust::{
31    ///     blocking::rest::termii,
32    ///     common::token::{InAppTokenMessageType, InAppTokenRequest},
33    /// };
34    ///
35    /// let client = termii::Termii::new("Your API key");
36    ///
37    /// let in_app_token_request =
38    ///     InAppTokenRequest::new("+234XXXXXXXXXX", InAppTokenMessageType::NUMERIC, 3, 300, 6);
39    ///
40    /// let in_app_token_response = client
41    ///     .token
42    ///     .in_app_token
43    ///     .send(in_app_token_request)
44    ///     .unwrap();
45    ///
46    /// println!("{:?}", in_app_token_response);
47    /// ```
48    pub fn send(
49        &self,
50        mut otp_payload: InAppTokenRequest,
51    ) -> Result<InAppTokenResponse, errors::HttpError> {
52        otp_payload.set_api_key(self.api_key);
53
54        let response = self
55            .client
56            .post("sms/otp/generate", None, None, Some(otp_payload))?;
57
58        let otp_response = response_or_error_text_blocking!(response, InAppTokenResponse);
59
60        Ok(otp_response)
61    }
62}