Skip to main content

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

1//! Add a file attachment to a Microsoft Graph message
2//! (`POST /me/messages/{id}/attachments`); returns the created
3//! attachment.
4//!
5//! Only the `fileAttachment` subtype is created here: the raw content is
6//! base64-encoded into `contentBytes`, as Graph requires.
7//!
8//! <https://learn.microsoft.com/en-us/graph/api/message-post-attachments>
9
10use alloc::{format, string::String};
11
12use base64::{Engine, engine::general_purpose::STANDARD};
13use io_http::rfc6750::bearer::HttpAuthBearer;
14use log::{debug, trace};
15use serde::Serialize;
16use url::Url;
17
18use crate::{
19    coroutine::*,
20    msgraph_try,
21    v1::{
22        rest::users::messages::attachments::MsgraphAttachment,
23        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
24    },
25};
26
27/// Body of the file attachment create request.
28#[derive(Debug, Serialize)]
29#[serde(rename_all = "camelCase")]
30struct MsgraphAttachmentCreateRequest<'a> {
31    #[serde(rename = "@odata.type")]
32    odata_type: &'a str,
33    name: &'a str,
34    content_bytes: String,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    content_type: Option<&'a str>,
37}
38
39/// Adds a file attachment to a Microsoft Graph message.
40pub struct MsgraphAttachmentCreate {
41    send: MsgraphSend<MsgraphAttachment>,
42}
43
44impl MsgraphAttachmentCreate {
45    /// Adds a `fileAttachment` named `name` carrying `content`, with
46    /// an optional MIME `content_type`.
47    ///
48    /// The raw content bytes are base64-encoded into `contentBytes`,
49    /// as Graph requires.
50    pub fn new(
51        auth: &HttpAuthBearer,
52        user_id: &str,
53        message_id: &str,
54        name: &str,
55        content: &[u8],
56        content_type: Option<&str>,
57    ) -> Result<Self, MsgraphSendError> {
58        debug!("prepare microsoft graph attachment for creation");
59        trace!("message_id: {message_id:?}");
60        trace!("name: {name:?}");
61        trace!("content_type: {content_type:?}");
62
63        let user = user_path(user_id);
64        let url = Url::parse(MSGRAPH_API_BASE)?
65            .join(&format!("{user}/messages/{message_id}/attachments"))?;
66        let body = MsgraphAttachmentCreateRequest {
67            odata_type: "#microsoft.graph.fileAttachment",
68            name,
69            content_bytes: STANDARD.encode(content),
70            content_type,
71        };
72        let send = MsgraphSend::post_json(auth, url, &body)?;
73
74        Ok(Self { send })
75    }
76}
77
78impl MsgraphCoroutine for MsgraphAttachmentCreate {
79    type Yield = MsgraphYield;
80    type Return = Result<MsgraphSendOutput<MsgraphAttachment>, MsgraphSendError>;
81
82    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
83        let out = msgraph_try!(&mut self.send, arg);
84        debug!("attachment created");
85        trace!("out: {out:?}");
86        MsgraphCoroutineState::Complete(Ok(out))
87    }
88}