Skip to main content

io_gmail/v1/rest/drafts/
update.rs

1//! Update a Gmail draft (`users.drafts.update`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/update>
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,
16        send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
17    },
18};
19
20/// Gmail REST draft update, wrapping the updated `GmailDraft`.
21pub struct GmailDraftUpdate {
22    send: GmailSend<GmailDraft>,
23}
24
25impl GmailDraftUpdate {
26    /// Builds the `users.drafts.update` request wrapping the given draft,
27    /// addressed by its id.
28    pub fn new(
29        auth: &HttpAuthBearer,
30        user_id: &str,
31        draft: &GmailDraft,
32    ) -> Result<Self, GmailSendError> {
33        debug!("prepare gmail draft update");
34        trace!("draft: {draft:?}");
35
36        let id = &draft.id;
37
38        let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/drafts/{id}"))?;
39        let send = GmailSend::put_json(auth, url, draft)?;
40
41        Ok(Self { send })
42    }
43}
44
45impl GmailCoroutine for GmailDraftUpdate {
46    type Yield = GmailYield;
47    type Return = Result<GmailSendOutput<GmailDraft>, 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!("draft updated");
52        trace!("out: {out:?}");
53        GmailCoroutineState::Complete(Ok(out))
54    }
55}