io_gmail/v1/rest/drafts/
create.rs1use 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
20pub struct GmailDraftCreate {
22 send: GmailSend<GmailDraft>,
23}
24
25impl GmailDraftCreate {
26 pub fn new(
28 auth: &HttpAuthBearer,
29 user_id: &str,
30 draft: &GmailDraft,
31 ) -> Result<Self, GmailSendError> {
32 debug!("prepare gmail draft creation");
33 trace!("draft: {draft:?}");
34
35 let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/drafts"))?;
36 let send = GmailSend::post_json(auth, url, draft)?;
37
38 Ok(Self { send })
39 }
40}
41
42impl GmailCoroutine for GmailDraftCreate {
43 type Yield = GmailYield;
44 type Return = Result<GmailSendOutput<GmailDraft>, 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 created");
49 trace!("out: {out:?}");
50 GmailCoroutineState::Complete(Ok(out))
51 }
52}