Skip to main content

io_msgraph/v1/rest/users/messages/
send.rs

1//! Send a Microsoft Graph draft message
2//! (`POST /me/messages/{id}/send`); the draft is moved to Sent Items.
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/message-send>
5
6use alloc::{format, vec::Vec};
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    msgraph_try,
15    v1::send::{
16        MSGRAPH_API_BASE, MsgraphNoResponse, MsgraphSend, MsgraphSendError, MsgraphSendOutput,
17        user_path,
18    },
19};
20
21/// Sends an existing Microsoft Graph draft message.
22pub struct MsgraphMessageSend {
23    send: MsgraphSend<MsgraphNoResponse>,
24}
25
26impl MsgraphMessageSend {
27    /// Sends the draft message `id`.
28    pub fn new(auth: &HttpAuthBearer, user_id: &str, id: &str) -> Result<Self, MsgraphSendError> {
29        debug!("prepare microsoft graph draft send");
30        trace!("id: {id:?}");
31
32        let user = user_path(user_id);
33        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/messages/{id}/send"))?;
34        let send = MsgraphSend::with_method(auth, "POST", url, None, Vec::new());
35
36        Ok(Self { send })
37    }
38}
39
40impl MsgraphCoroutine for MsgraphMessageSend {
41    type Yield = MsgraphYield;
42    type Return = Result<MsgraphSendOutput<MsgraphNoResponse>, MsgraphSendError>;
43
44    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
45        let out = msgraph_try!(&mut self.send, arg);
46        debug!("draft sent");
47        trace!("out: {out:?}");
48        MsgraphCoroutineState::Complete(Ok(out))
49    }
50}