Skip to main content

io_gmail/v1/rest/messages/
send.rs

1//! Send a Gmail message (`users.messages.send`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send>
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::{GmailMessage, GmailMessageId},
15    v1::send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
16};
17
18/// Gmail REST message send, wrapping the resulting `GmailMessageId`.
19pub struct GmailMessageSend {
20    send: GmailSend<GmailMessageId>,
21}
22
23impl GmailMessageSend {
24    /// Builds the `users.messages.send` request for the given message.
25    pub fn new(
26        auth: &HttpAuthBearer,
27        user_id: &str,
28        message: &GmailMessage,
29    ) -> Result<Self, GmailSendError> {
30        debug!("prepare gmail message send");
31        trace!("message: {message:?}");
32
33        let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/messages/send"))?;
34        let send = GmailSend::post_json(auth, url, message)?;
35
36        Ok(Self { send })
37    }
38}
39
40impl GmailCoroutine for GmailMessageSend {
41    type Yield = GmailYield;
42    type Return = Result<GmailSendOutput<GmailMessageId>, GmailSendError>;
43
44    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
45        let out = gmail_try!(&mut self.send, arg);
46        debug!("message sent");
47        trace!("out: {out:?}");
48        GmailCoroutineState::Complete(Ok(out))
49    }
50}