termii_rust/async_impl/rest/insights/
history.rs

1//! Request organization's account messaging history.
2
3use std::{collections::HashMap, sync::Arc};
4
5use async_trait::async_trait;
6
7use crate::{
8    async_impl::http::client,
9    common::{
10        errors,
11        insights::history::{HistoryItem, HistoryResponse},
12        pagination,
13    },
14};
15
16#[derive(Debug)]
17#[allow(dead_code)]
18pub struct History<'a> {
19    api_key: &'a str,
20    client: Arc<client::HttpClient>,
21}
22
23impl<'a> History<'a> {
24    pub(crate) fn new(api_key: &'a str, client: Arc<client::HttpClient>) -> History<'a> {
25        History { api_key, client }
26    }
27
28    pub(crate) async fn _get(&self, page: &str) -> Result<Vec<HistoryItem>, errors::HttpError> {
29        let mut params = HashMap::new();
30        params.insert("api_key", self.api_key);
31        params.insert("page", page);
32
33        let response = self.client.get("sms/inbox", Some(params), None).await?;
34
35        let history_item = response_or_error_text_async!(response, HistoryResponse);
36
37        Ok(history_item.data.data)
38    }
39
40    /// Gets your messaging history.
41    ///
42    /// ## Examples
43    ///
44    /// ```rust
45    /// use termii_rust::{
46    ///     async_impl::rest::termii,
47    ///     common::insights::history::HistoryItem,
48    /// }
49    ///
50    /// let client = termii::Termii::new("Your API key");
51    ///
52    /// let history:HistoryItem = client.insights.history.get().await.unwrap();
53    ///
54    /// println!("{:?}", history);
55    /// ```
56    ///
57    /// ### The above code is limited by termii's pagination. You can get all your messaging history with the **all** function like such
58    /// ```rust
59    /// let history = client.insights.history.all().await.unwrap();
60    /// ```
61    pub async fn get(&self, page: Option<&str>) -> Result<Vec<HistoryItem>, errors::HttpError> {
62        let page = page.unwrap_or("1");
63        let history_items = self._get(page).await?;
64        Ok(history_items)
65    }
66}
67
68#[async_trait]
69impl pagination::PaginatedResourceAsync for History<'_> {
70    type Item = HistoryItem;
71
72    async fn _get(&self, page: &str) -> Result<Vec<Self::Item>, errors::HttpError> {
73        History::_get(self, page).await
74    }
75}