io_gmail/v1/rest/threads/
list.rs1use 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::{is_false, to_query_pairs},
17 rest::threads::GmailThreadSummary,
18 send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
19 },
20};
21
22#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
24#[serde(rename_all = "camelCase")]
25pub struct GmailThreadsListParams<'a> {
26 pub q: Option<&'a str>,
28 pub label_ids: &'a [String],
30 pub max_results: Option<u32>,
32 pub page_token: Option<&'a str>,
34 #[serde(skip_serializing_if = "is_false")]
36 pub include_spam_trash: bool,
37}
38
39#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
41#[serde(rename_all = "camelCase")]
42pub struct GmailThreadsListResponse {
43 #[serde(default)]
45 pub threads: Vec<GmailThreadSummary>,
46 #[serde(default)]
48 pub next_page_token: Option<String>,
49 #[serde(default)]
51 pub result_size_estimate: Option<u64>,
52}
53
54pub struct GmailThreadsList {
56 send: GmailSend<GmailThreadsListResponse>,
57}
58
59impl GmailThreadsList {
60 pub fn new(
63 auth: &HttpAuthBearer,
64 user_id: &str,
65 params: &GmailThreadsListParams,
66 ) -> Result<Self, GmailSendError> {
67 debug!("prepare gmail threads listing");
68 trace!("params: {params:?}");
69
70 let mut url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/threads"))?;
71 url.query_pairs_mut().extend_pairs(to_query_pairs(params));
72
73 let send = GmailSend::get(auth, url);
74
75 Ok(Self { send })
76 }
77}
78
79impl GmailCoroutine for GmailThreadsList {
80 type Yield = GmailYield;
81 type Return = Result<GmailSendOutput<GmailThreadsListResponse>, GmailSendError>;
82
83 fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
84 let out = gmail_try!(&mut self.send, arg);
85 debug!("threads listed");
86 trace!("out: {out:?}");
87 GmailCoroutineState::Complete(Ok(out))
88 }
89}