termii_rust/async_impl/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::sync::Arc;
5
6use crate::{
7    async_impl::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: Arc<client::HttpClient>,
18}
19
20impl<'a> InAppToken<'a> {
21    pub(crate) fn new(api_key: &'a str, client: Arc<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    ///     async_impl::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    ///     .await
45    ///     .unwrap();
46    ///
47    /// println!("{:?}", in_app_token_response);
48    /// ```
49    pub async fn send(
50        &self,
51        mut otp_payload: InAppTokenRequest,
52    ) -> Result<InAppTokenResponse, errors::HttpError> {
53        otp_payload.set_api_key(self.api_key);
54
55        let response = self
56            .client
57            .post("sms/otp/generate", None, None, Some(otp_payload))
58            .await?;
59
60        let otp_response = response_or_error_text_async!(response, InAppTokenResponse);
61
62        Ok(otp_response)
63    }
64}