use alloc::{format, string::String, vec::Vec};
use io_http::rfc6750::bearer::HttpAuthBearer;
use log::{debug, trace};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::{
coroutine::*,
msgraph_try,
v1::{
rest::users::messages::attachments::MsgraphAttachment,
send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
},
};
#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MsgraphAttachmentsListResponse {
#[serde(default)]
pub value: Vec<MsgraphAttachment>,
#[serde(default, rename = "@odata.nextLink")]
pub next_link: Option<String>,
}
pub struct MsgraphAttachmentsList {
send: MsgraphSend<MsgraphAttachmentsListResponse>,
}
impl MsgraphAttachmentsList {
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/{message_id}/attachments"))?;
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))
}
}