use alloc::format;
use io_http::rfc6750::bearer::HttpAuthBearer;
use log::{debug, trace};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::{
coroutine::*,
msgraph_try,
v1::{
page::{MsgraphPage, link_url},
rest::users::{contacts::delta::MsgraphRemoved, todo::lists::MsgraphTodoList},
send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
},
};
pub type MsgraphTodoListsDeltaResponse = MsgraphPage<MsgraphTodoListDelta>;
#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MsgraphTodoListDelta {
#[serde(flatten)]
pub list: MsgraphTodoList,
#[serde(default, rename = "@removed", skip_serializing_if = "Option::is_none")]
pub removed: Option<MsgraphRemoved>,
}
pub struct MsgraphTodoListsDelta {
send: MsgraphSend<MsgraphTodoListsDeltaResponse>,
}
impl MsgraphTodoListsDelta {
pub fn new(auth: &HttpAuthBearer, user_id: &str) -> Result<Self, MsgraphSendError> {
debug!("prepare microsoft graph to do lists delta");
let user = user_path(user_id)?;
let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/todo/lists/delta"))?;
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 delta from link");
trace!("link: {link}");
let url = link_url(link)?;
let send = MsgraphSend::get(auth, url);
Ok(Self { send })
}
pub fn max_page_size(mut self, size: u32) -> Self {
self.send = self.send.max_page_size(size);
self
}
}
impl MsgraphCoroutine for MsgraphTodoListsDelta {
type Yield = MsgraphYield;
type Return = Result<MsgraphSendOutput<MsgraphTodoListsDeltaResponse>, 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 delta page received");
trace!("out: {out:?}");
MsgraphCoroutineState::Complete(Ok(out))
}
}