Skip to main content

io_gmail/v1/rest/settings/send_as/
create.rs

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