Skip to main content

io_gmail/v1/rest/drafts/
send.rs

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