Skip to main content

io_gmail/v1/rest/users/
watch.rs

1//! Set up Gmail push notifications (`users.watch`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users/watch>
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::users::{GmailWatchRequest, GmailWatchResponse},
16        send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
17    },
18};
19
20/// I/O-free coroutine setting up Gmail push notifications (`users.watch`).
21pub struct GmailWatch {
22    send: GmailSend<GmailWatchResponse>,
23}
24
25impl GmailWatch {
26    /// Builds the `users.watch` request from the given [`GmailWatchRequest`].
27    pub fn new(
28        auth: &HttpAuthBearer,
29        user_id: &str,
30        request: &GmailWatchRequest,
31    ) -> Result<Self, GmailSendError> {
32        debug!("prepare gmail watch");
33        trace!("request: {request:?}");
34
35        let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/watch"))?;
36        let send = GmailSend::post_json(auth, url, request)?;
37
38        Ok(Self { send })
39    }
40}
41
42impl GmailCoroutine for GmailWatch {
43    type Yield = GmailYield;
44    type Return = Result<GmailSendOutput<GmailWatchResponse>, 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!("watch established");
49        trace!("out: {out:?}");
50        GmailCoroutineState::Complete(Ok(out))
51    }
52}