termii_rust/blocking/rest/insights/
history.rs

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