termii_rust/blocking/rest/token/
verify.rs

1//! Verify one-time passwords and return responses
2//! based on the validity of the token.
3
4use std::rc::Rc;
5
6use crate::{
7    blocking::http::client,
8    common::{
9        errors,
10        token::verify::{VerifyTokenRequest, VerifyTokenResponse},
11    },
12};
13
14#[derive(Debug)]
15pub struct VerifyToken<'a> {
16    api_key: &'a str,
17    client: Rc<client::HttpClient>,
18}
19
20impl<'a> VerifyToken<'a> {
21    pub fn new(api_key: &'a str, client: Rc<client::HttpClient>) -> VerifyToken<'a> {
22        VerifyToken { api_key, client }
23    }
24
25    /// Verify one-time passwords and return responses.
26    ///
27    /// ## Examples
28    ///
29    /// ```rust
30    /// use termii_rust::{blocking::rest::termii, common::token::VerifyTokenRequest};
31    ///
32    /// let client = termii::Termii::new("Your API key");
33    ///
34    /// let verify_otp_request = VerifyTokenRequest::new(
35    ///     "c8dcd048-5e7f-4347-8c89-4470c3af0b".to_string(),
36    ///     "195558".to_string(),
37    /// );
38    ///
39    /// let response = client.token.verify.send(verify_otp_request).unwrap();
40    ///
41    /// println!("{:#?}", response);
42    /// ```
43    pub fn send(
44        &self,
45        mut otp_payload: VerifyTokenRequest,
46    ) -> Result<VerifyTokenResponse, errors::HttpError> {
47        otp_payload.set_api_key(self.api_key);
48
49        let response = self
50            .client
51            .post("sms/otp/verify", None, None, Some(otp_payload))?;
52
53        let otp_response = response_or_error_text_blocking!(response, VerifyTokenResponse);
54
55        Ok(otp_response)
56    }
57}