termii_rust/async_impl/rest/switch/
templates.rs

1//! Request and Send template messageds across different messaging channels.
2
3use std::sync::Arc;
4
5use crate::{
6    async_impl::http::client,
7    common::{
8        errors,
9        switch::templates::{TemplateItem, TemplatesRequest},
10    },
11};
12
13#[derive(Debug)]
14pub struct Templates<'a> {
15    api_key: &'a str,
16    client: Arc<client::HttpClient>,
17}
18
19impl<'a> Templates<'a> {
20    pub(crate) fn new(api_key: &'a str, client: Arc<client::HttpClient>) -> Templates<'a> {
21        Templates { api_key, client }
22    }
23
24    /// Set a template for your org's one time pin.
25    ///
26    /// ## Examples
27    ///
28    /// ```rust
29    /// use termii_rust::{
30    ///     async_impl::rest::termii,
31    ///     common::switch::templates::{TemplatesData, TemplatesRequest},
32    /// };
33    ///
34    /// let client = termii::Termii::new("Your API key");
35    ///
36    /// let templates_data =
37    ///     TemplatesData::new("Termii", "325821".to_string(), "10 minutes".to_string());
38    ///
39    /// let templates_payload = TemplatesRequest::new(
40    ///     "+234XXXXXXXXXX".to_string(),
41    ///     "talert".to_string(),
42    ///     "1493-csdn3-ns34w-sd3434-dfdf".to_string(),
43    ///     templates_data,
44    /// );
45    ///
46    /// let templates_response = client
47    ///     .switch
48    ///     .templates
49    ///     .send(templates_payload)
50    ///     .await
51    ///     .unwrap();
52    ///
53    /// println!("{:?}", templates_response);
54    /// ```
55    pub async fn send<T>(
56        &self,
57        mut payload: TemplatesRequest,
58    ) -> Result<Vec<TemplateItem>, errors::HttpError> {
59        payload.set_api_key(self.api_key);
60
61        let response = self
62            .client
63            .post("send/templates", None, None, Some(payload))
64            .await?;
65
66        let template = response_or_error_text_async!(response, Vec<TemplateItem>);
67
68        Ok(template)
69    }
70}