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        path_segment, 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)?
34            .join(&format!("{user}/messages/{}/send", path_segment(id)?))?;
35        let send = MsgraphSend::with_method(auth, "POST", url, None, Vec::new());
36
37        Ok(Self { send })
38    }
39}
40
41impl MsgraphCoroutine for MsgraphMessageSend {
42    type Yield = MsgraphYield;
43    type Return = Result<MsgraphSendOutput<MsgraphNoResponse>, MsgraphSendError>;
44
45    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
46        let out = msgraph_try!(&mut self.send, arg);
47        debug!("draft sent");
48        trace!("out: {out:?}");
49        MsgraphCoroutineState::Complete(Ok(out))
50    }
51}