io-gmail 0.1.0

Gmail REST API client library for Rust
Documentation
//! Set up Gmail push notifications (`users.watch`).
//!
//! <https://developers.google.com/gmail/api/reference/rest/v1/users/watch>

use alloc::format;

use io_http::rfc6750::bearer::HttpAuthBearer;
use log::{debug, trace};
use url::Url;

use crate::{
    coroutine::*,
    gmail_try,
    v1::{
        rest::users::{GmailWatchRequest, GmailWatchResponse},
        send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
    },
};

/// I/O-free coroutine setting up Gmail push notifications (`users.watch`).
pub struct GmailWatch {
    send: GmailSend<GmailWatchResponse>,
}

impl GmailWatch {
    /// Builds the `users.watch` request from the given [`GmailWatchRequest`].
    pub fn new(
        auth: &HttpAuthBearer,
        user_id: &str,
        request: &GmailWatchRequest,
    ) -> Result<Self, GmailSendError> {
        debug!("prepare gmail watch");
        trace!("request: {request:?}");

        let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/watch"))?;
        let send = GmailSend::post_json(auth, url, request)?;

        Ok(Self { send })
    }
}

impl GmailCoroutine for GmailWatch {
    type Yield = GmailYield;
    type Return = Result<GmailSendOutput<GmailWatchResponse>, GmailSendError>;

    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
        let out = gmail_try!(&mut self.send, arg);
        debug!("watch established");
        trace!("out: {out:?}");
        GmailCoroutineState::Complete(Ok(out))
    }
}