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;
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    msgraph_try,
15    v1::{
16        page::MsgraphPage,
17        rest::users::messages::attachments::MsgraphAttachment,
18        send::{
19            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
20            user_path,
21        },
22    },
23};
24
25/// One page of attachments.
26pub type MsgraphAttachmentsListResponse = MsgraphPage<MsgraphAttachment>;
27
28/// Lists the attachments of a Microsoft Graph message.
29pub struct MsgraphAttachmentsList {
30    send: MsgraphSend<MsgraphAttachmentsListResponse>,
31}
32
33impl MsgraphAttachmentsList {
34    /// Lists the attachments of the message `message_id`.
35    pub fn new(
36        auth: &HttpAuthBearer,
37        user_id: &str,
38        message_id: &str,
39    ) -> Result<Self, MsgraphSendError> {
40        debug!("prepare microsoft graph attachments listing");
41        trace!("message_id: {message_id:?}");
42
43        let user = user_path(user_id)?;
44        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!(
45            "{user}/messages/{}/attachments",
46            path_segment(message_id)?
47        ))?;
48        let send = MsgraphSend::get(auth, url);
49
50        Ok(Self { send })
51    }
52}
53
54impl MsgraphCoroutine for MsgraphAttachmentsList {
55    type Yield = MsgraphYield;
56    type Return = Result<MsgraphSendOutput<MsgraphAttachmentsListResponse>, MsgraphSendError>;
57
58    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
59        let out = msgraph_try!(&mut self.send, arg);
60        debug!("attachments listed");
61        trace!("out: {out:?}");
62        MsgraphCoroutineState::Complete(Ok(out))
63    }
64}