Skip to main content

io_gmail/v1/rest/messages/attachments/
get.rs

1//! Get a Gmail message attachment (`users.messages.attachments.get`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.messages.attachments/get>
4
5use alloc::format;
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use url::Url;
10
11use crate::{
12    coroutine::*,
13    gmail_try,
14    v1::rest::messages::GmailMessagePartBody,
15    v1::send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
16};
17
18/// Gmail REST attachment retrieval, wrapping a `GmailMessagePartBody`.
19pub struct GmailAttachmentGet {
20    send: GmailSend<GmailMessagePartBody>,
21}
22
23impl GmailAttachmentGet {
24    /// Builds the `users.messages.attachments.get` request for the
25    /// given message and attachment ids.
26    pub fn new(
27        auth: &HttpAuthBearer,
28        user_id: &str,
29        message_id: &str,
30        id: &str,
31    ) -> Result<Self, GmailSendError> {
32        debug!("prepare gmail attachment retrieval");
33        trace!("message_id: {message_id:?}");
34        trace!("id: {id:?}");
35
36        let url = Url::parse(GMAIL_API_BASE)?.join(&format!(
37            "users/{user_id}/messages/{message_id}/attachments/{id}"
38        ))?;
39        let send = GmailSend::get(auth, url);
40
41        Ok(Self { send })
42    }
43}
44
45impl GmailCoroutine for GmailAttachmentGet {
46    type Yield = GmailYield;
47    type Return = Result<GmailSendOutput<GmailMessagePartBody>, GmailSendError>;
48
49    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
50        let out = gmail_try!(&mut self.send, arg);
51        debug!("attachment retrieved");
52        trace!("out: {out:?}");
53        GmailCoroutineState::Complete(Ok(out))
54    }
55}