termii_rust/blocking/rest/insights/
history.rs1use 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 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}