Skip to main content

io_gmail/v1/rest/threads/
get.rs

1//! Get a Gmail thread (`users.threads.get`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.threads/get>
4
5use alloc::format;
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde_variant::to_variant_name;
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    gmail_try,
15    v1::rest::messages::GmailMessageFormat,
16    v1::rest::threads::GmailThread,
17    v1::send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
18};
19
20/// Gmail REST thread retrieval, wrapping a `GmailThread` response.
21pub struct GmailThreadGet {
22    send: GmailSend<GmailThread>,
23}
24
25impl GmailThreadGet {
26    /// Builds the `users.threads.get` request for the given thread id
27    /// and message format.
28    ///
29    /// The metadata headers only apply when the format is
30    /// [`GmailMessageFormat::Metadata`].
31    pub fn new(
32        auth: &HttpAuthBearer,
33        user_id: &str,
34        id: &str,
35        format: GmailMessageFormat,
36        metadata_headers: &[&str],
37    ) -> Result<Self, GmailSendError> {
38        debug!("prepare gmail thread retrieval");
39        trace!("id: {id:?}");
40
41        let mut url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/threads/{id}"))?;
42
43        {
44            let mut query = url.query_pairs_mut();
45            query.append_pair("format", to_variant_name(&format).unwrap_or_default());
46
47            if matches!(format, GmailMessageFormat::Metadata) {
48                for header in metadata_headers {
49                    query.append_pair("metadataHeaders", header);
50                }
51            }
52        }
53
54        let send = GmailSend::get(auth, url);
55
56        Ok(Self { send })
57    }
58}
59
60impl GmailCoroutine for GmailThreadGet {
61    type Yield = GmailYield;
62    type Return = Result<GmailSendOutput<GmailThread>, GmailSendError>;
63
64    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
65        let out = gmail_try!(&mut self.send, arg);
66        debug!("thread retrieved");
67        trace!("out: {out:?}");
68        GmailCoroutineState::Complete(Ok(out))
69    }
70}