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::contacts::MsgraphContact,
send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
},
};
#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MsgraphContactsDeltaResponse {
#[serde(default)]
pub value: Vec<MsgraphContactDelta>,
#[serde(default, rename = "@odata.nextLink")]
pub next_link: Option<String>,
#[serde(default, rename = "@odata.deltaLink")]
pub delta_link: Option<String>,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MsgraphContactDelta {
#[serde(flatten)]
pub contact: MsgraphContact,
#[serde(default, rename = "@removed", skip_serializing_if = "Option::is_none")]
pub removed: Option<MsgraphRemoved>,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MsgraphRemoved {
#[serde(default)]
pub reason: String,
}
pub struct MsgraphContactsDelta {
send: MsgraphSend<MsgraphContactsDeltaResponse>,
}
impl MsgraphContactsDelta {
pub fn new(
auth: &HttpAuthBearer,
user_id: &str,
folder: Option<&str>,
select: Option<&str>,
) -> Result<Self, MsgraphSendError> {
debug!("prepare microsoft graph contacts delta");
trace!("folder: {folder:?}");
let user = user_path(user_id);
let path = match folder {
Some(folder) => format!("{user}/contactFolders/{folder}/contacts/delta"),
None => format!("{user}/contacts/delta"),
};
let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
if let Some(select) = select {
url.query_pairs_mut().append_pair("$select", select);
}
let send = MsgraphSend::get(auth, url);
Ok(Self { send })
}
}
impl MsgraphCoroutine for MsgraphContactsDelta {
type Yield = MsgraphYield;
type Return = Result<MsgraphSendOutput<MsgraphContactsDeltaResponse>, MsgraphSendError>;
fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
let out = msgraph_try!(&mut self.send, arg);
debug!("contacts delta page received");
trace!("out: {out:?}");
MsgraphCoroutineState::Complete(Ok(out))
}
}