io_gmail/v1/rest/messages/
batch_modify.rs1use 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
26pub struct GmailMessagesBatchModify {
28 send: GmailSend<GmailNoResponse>,
29}
30
31impl GmailMessagesBatchModify {
32 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}