io-msgraph 0.9.2

Microsoft Graph API client library for Rust
Documentation
//! List a Microsoft Graph message's attachments
//! (`GET /me/messages/{id}/attachments`).
//!
//! <https://learn.microsoft.com/en-us/graph/api/message-list-attachments>

use alloc::format;

use io_http::rfc6750::bearer::HttpAuthBearer;
use log::{debug, trace};
use url::Url;

use crate::{
    coroutine::*,
    msgraph_try,
    v1::{
        page::MsgraphPage,
        rest::users::messages::attachments::MsgraphAttachment,
        send::{
            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
            user_path,
        },
    },
};

/// One page of attachments.
pub type MsgraphAttachmentsListResponse = MsgraphPage<MsgraphAttachment>;

/// Lists the attachments of a Microsoft Graph message.
pub struct MsgraphAttachmentsList {
    send: MsgraphSend<MsgraphAttachmentsListResponse>,
}

impl MsgraphAttachmentsList {
    /// Lists the attachments of the message `message_id`.
    pub fn new(
        auth: &HttpAuthBearer,
        user_id: &str,
        message_id: &str,
    ) -> Result<Self, MsgraphSendError> {
        debug!("prepare microsoft graph attachments listing");
        trace!("message_id: {message_id:?}");

        let user = user_path(user_id)?;
        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!(
            "{user}/messages/{}/attachments",
            path_segment(message_id)?
        ))?;
        let send = MsgraphSend::get(auth, url);

        Ok(Self { send })
    }
}

impl MsgraphCoroutine for MsgraphAttachmentsList {
    type Yield = MsgraphYield;
    type Return = Result<MsgraphSendOutput<MsgraphAttachmentsListResponse>, MsgraphSendError>;

    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
        let out = msgraph_try!(&mut self.send, arg);
        debug!("attachments listed");
        trace!("out: {out:?}");
        MsgraphCoroutineState::Complete(Ok(out))
    }
}