Skip to main content

io_msgraph/v1/rest/users/messages/attachments/
delete.rs

1//! Delete a Microsoft Graph attachment
2//! (`DELETE /me/messages/{id}/attachments/{aid}`).
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/attachment-delete>
5
6use alloc::format;
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/// Deletes an attachment of a Microsoft Graph message.
22pub struct MsgraphAttachmentDelete {
23    send: MsgraphSend<MsgraphNoResponse>,
24}
25
26impl MsgraphAttachmentDelete {
27    /// Deletes the attachment `attachment_id` of the message
28    /// `message_id`.
29    pub fn new(
30        auth: &HttpAuthBearer,
31        user_id: &str,
32        message_id: &str,
33        attachment_id: &str,
34    ) -> Result<Self, MsgraphSendError> {
35        debug!("prepare microsoft graph attachment deletion");
36        trace!("message_id: {message_id:?}");
37        trace!("attachment_id: {attachment_id:?}");
38
39        let user = user_path(user_id);
40        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!(
41            "{user}/messages/{message_id}/attachments/{attachment_id}"
42        ))?;
43        let send = MsgraphSend::delete(auth, url);
44
45        Ok(Self { send })
46    }
47}
48
49impl MsgraphCoroutine for MsgraphAttachmentDelete {
50    type Yield = MsgraphYield;
51    type Return = Result<MsgraphSendOutput<MsgraphNoResponse>, MsgraphSendError>;
52
53    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
54        let out = msgraph_try!(&mut self.send, arg);
55        debug!("attachment deleted");
56        trace!("out: {out:?}");
57        MsgraphCoroutineState::Complete(Ok(out))
58    }
59}