Skip to main content

io_gmail/v1/rest/history/
list.rs

1//! List the Gmail history records (`users.history.list`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.history/list>
4
5use alloc::{format, string::String, vec::Vec};
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde::{Deserialize, Serialize};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    gmail_try,
15    v1::{
16        query::to_query_pairs,
17        rest::history::{GmailHistory, GmailHistoryType},
18        send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
19    },
20};
21
22/// Query parameters for listing history records (`users.history.list`).
23#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
24#[serde(rename_all = "camelCase")]
25pub struct GmailHistoryListParams<'a> {
26    /// The history id to start listing changes after.
27    pub start_history_id: &'a str,
28    /// The label id to restrict history records to.
29    pub label_id: Option<&'a str>,
30    /// The history types to return.
31    pub history_types: &'a [GmailHistoryType],
32    /// The maximum number of history records to return per page.
33    pub max_results: Option<u32>,
34    /// The page token to retrieve a specific page of results.
35    pub page_token: Option<&'a str>,
36}
37
38/// Response returned when listing history records (`users.history.list`).
39#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
40#[serde(rename_all = "camelCase")]
41pub struct GmailHistoryListResponse {
42    /// The list of history records.
43    #[serde(default)]
44    pub history: Vec<GmailHistory>,
45    /// The page token to retrieve the next page of results.
46    #[serde(default)]
47    pub next_page_token: Option<String>,
48    /// The id of the current history record of the mailbox.
49    #[serde(default)]
50    pub history_id: Option<String>,
51}
52
53/// I/O-free coroutine listing Gmail history records (`users.history.list`).
54pub struct GmailHistoryList {
55    send: GmailSend<GmailHistoryListResponse>,
56}
57
58impl GmailHistoryList {
59    /// Builds the `users.history.list` request from the given
60    /// [`GmailHistoryListParams`].
61    pub fn new(
62        auth: &HttpAuthBearer,
63        user_id: &str,
64        params: &GmailHistoryListParams,
65    ) -> Result<Self, GmailSendError> {
66        debug!("prepare gmail history listing");
67        trace!("params: {params:?}");
68
69        let mut url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/history"))?;
70        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
71
72        let send = GmailSend::get(auth, url);
73
74        Ok(Self { send })
75    }
76}
77
78impl GmailCoroutine for GmailHistoryList {
79    type Yield = GmailYield;
80    type Return = Result<GmailSendOutput<GmailHistoryListResponse>, GmailSendError>;
81
82    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
83        let out = gmail_try!(&mut self.send, arg);
84        debug!("history listed");
85        trace!("out: {out:?}");
86        GmailCoroutineState::Complete(Ok(out))
87    }
88}