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::{
query::to_query_pairs,
rest::users::contacts::MsgraphContact,
send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
},
};
#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MsgraphContactsListResponse {
#[serde(default)]
pub value: Vec<MsgraphContact>,
#[serde(default, rename = "@odata.nextLink")]
pub next_link: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
pub struct MsgraphContactsListParams<'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>,
#[serde(rename = "$orderby")]
pub orderby: Option<&'a str>,
#[serde(rename = "$expand")]
pub expand: Option<&'a str>,
#[serde(rename = "$count")]
pub count: Option<bool>,
}
pub struct MsgraphContactsList {
send: MsgraphSend<MsgraphContactsListResponse>,
}
impl MsgraphContactsList {
pub fn new(
auth: &HttpAuthBearer,
user_id: &str,
folder: Option<&str>,
params: &MsgraphContactsListParams,
) -> Result<Self, MsgraphSendError> {
debug!("prepare microsoft graph contacts listing");
trace!("folder: {folder:?}");
trace!("params: {params:?}");
let user = user_path(user_id);
let path = match folder {
Some(folder) => format!("{user}/contactFolders/{folder}/contacts"),
None => format!("{user}/contacts"),
};
let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
url.query_pairs_mut().extend_pairs(to_query_pairs(params));
let send = MsgraphSend::get(auth, url);
Ok(Self { send })
}
}
impl MsgraphCoroutine for MsgraphContactsList {
type Yield = MsgraphYield;
type Return = Result<MsgraphSendOutput<MsgraphContactsListResponse>, MsgraphSendError>;
fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
let out = msgraph_try!(&mut self.send, arg);
debug!("contacts listed");
trace!("out: {out:?}");
MsgraphCoroutineState::Complete(Ok(out))
}
}