Skip to main content

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

1//! List a Microsoft Graph message's attachments
2//! (`GET /me/messages/{id}/attachments`).
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/message-list-attachments>
5
6use alloc::{format, string::String, vec::Vec};
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use serde::{Deserialize, Serialize};
11use url::Url;
12
13use crate::{
14    coroutine::*,
15    msgraph_try,
16    v1::{
17        rest::users::messages::attachments::MsgraphAttachment,
18        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
19    },
20};
21
22/// One page of attachments (`value` plus the OData paging link).
23#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
24pub struct MsgraphAttachmentsListResponse {
25    /// The attachments of the page.
26    #[serde(default)]
27    pub value: Vec<MsgraphAttachment>,
28    /// The URL of the next page, when one exists.
29    #[serde(default, rename = "@odata.nextLink")]
30    pub next_link: Option<String>,
31}
32
33/// Lists the attachments of a Microsoft Graph message.
34pub struct MsgraphAttachmentsList {
35    send: MsgraphSend<MsgraphAttachmentsListResponse>,
36}
37
38impl MsgraphAttachmentsList {
39    /// Lists the attachments of the message `message_id`.
40    pub fn new(
41        auth: &HttpAuthBearer,
42        user_id: &str,
43        message_id: &str,
44    ) -> Result<Self, MsgraphSendError> {
45        debug!("prepare microsoft graph attachments listing");
46        trace!("message_id: {message_id:?}");
47
48        let user = user_path(user_id);
49        let url = Url::parse(MSGRAPH_API_BASE)?
50            .join(&format!("{user}/messages/{message_id}/attachments"))?;
51        let send = MsgraphSend::get(auth, url);
52
53        Ok(Self { send })
54    }
55}
56
57impl MsgraphCoroutine for MsgraphAttachmentsList {
58    type Yield = MsgraphYield;
59    type Return = Result<MsgraphSendOutput<MsgraphAttachmentsListResponse>, MsgraphSendError>;
60
61    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
62        let out = msgraph_try!(&mut self.send, arg);
63        debug!("attachments listed");
64        trace!("out: {out:?}");
65        MsgraphCoroutineState::Complete(Ok(out))
66    }
67}