Skip to main content

io_gmail/v1/rest/messages/
batch_modify.rs

1//! Batch-modify Gmail message labels (`users.messages.batchModify`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.messages/batchModify>
4
5use alloc::{format, string::String};
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde::Serialize;
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    gmail_try,
15    v1::send::{GMAIL_API_BASE, GmailNoResponse, GmailSend, GmailSendError, GmailSendOutput},
16};
17
18#[derive(Debug, Serialize)]
19#[serde(rename_all = "camelCase")]
20struct GmailMessageBatchModifyRequest<'a> {
21    ids: &'a [String],
22    add_label_ids: &'a [String],
23    remove_label_ids: &'a [String],
24}
25
26/// Gmail REST batch message label modification, yielding no response body.
27pub struct GmailMessagesBatchModify {
28    send: GmailSend<GmailNoResponse>,
29}
30
31impl GmailMessagesBatchModify {
32    /// Builds the `users.messages.batchModify` request adding and
33    /// removing the given label ids on the given message ids.
34    pub fn new(
35        auth: &HttpAuthBearer,
36        user_id: &str,
37        ids: &[String],
38        add_label_ids: &[String],
39        remove_label_ids: &[String],
40    ) -> Result<Self, GmailSendError> {
41        debug!("prepare gmail messages batch modification");
42        trace!("ids: {ids:?}");
43        trace!("add_label_ids: {add_label_ids:?}");
44        trace!("remove_label_ids: {remove_label_ids:?}");
45
46        let url =
47            Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/messages/batchModify"))?;
48        let body = GmailMessageBatchModifyRequest {
49            ids,
50            add_label_ids,
51            remove_label_ids,
52        };
53        let send = GmailSend::post_json(auth, url, &body)?;
54
55        Ok(Self { send })
56    }
57}
58
59impl GmailCoroutine for GmailMessagesBatchModify {
60    type Yield = GmailYield;
61    type Return = Result<GmailSendOutput<GmailNoResponse>, GmailSendError>;
62
63    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
64        let out = gmail_try!(&mut self.send, arg);
65        debug!("messages batch modified");
66        trace!("out: {out:?}");
67        GmailCoroutineState::Complete(Ok(out))
68    }
69}