use alloc::format;
use io_http::rfc6750::bearer::HttpAuthBearer;
use log::{debug, trace};
use serde::Serialize;
use url::Url;
use crate::{
coroutine::*,
msgraph_try,
v1::{
page::{MsgraphPage, link_url},
query::to_query_pairs,
rest::users::todo::lists::MsgraphTodoList,
send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
},
};
pub type MsgraphTodoListsListResponse = MsgraphPage<MsgraphTodoList>;
#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
pub struct MsgraphTodoListsListParams<'a> {
#[serde(rename = "$top")]
pub top: Option<u32>,
#[serde(rename = "$skip")]
pub skip: Option<u32>,
#[serde(rename = "$select")]
pub select: Option<&'a str>,
#[serde(rename = "$filter")]
pub filter: Option<&'a str>,
}
pub struct MsgraphTodoListsList {
send: MsgraphSend<MsgraphTodoListsListResponse>,
}
impl MsgraphTodoListsList {
pub fn new(
auth: &HttpAuthBearer,
user_id: &str,
params: &MsgraphTodoListsListParams,
) -> Result<Self, MsgraphSendError> {
debug!("prepare microsoft graph to do lists listing");
trace!("params: {params:?}");
let user = user_path(user_id)?;
let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/todo/lists"))?;
url.query_pairs_mut().extend_pairs(to_query_pairs(params));
let send = MsgraphSend::get(auth, url);
Ok(Self { send })
}
pub fn from_link(auth: &HttpAuthBearer, link: &str) -> Result<Self, MsgraphSendError> {
debug!("prepare microsoft graph to do lists listing from link");
trace!("link: {link}");
let url = link_url(link)?;
let send = MsgraphSend::get(auth, url);
Ok(Self { send })
}
}
impl MsgraphCoroutine for MsgraphTodoListsList {
type Yield = MsgraphYield;
type Return = Result<MsgraphSendOutput<MsgraphTodoListsListResponse>, MsgraphSendError>;
fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
let out = msgraph_try!(&mut self.send, arg);
debug!("to do lists listed");
trace!("out: {out:?}");
MsgraphCoroutineState::Complete(Ok(out))
}
}