Skip to main content

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

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