io_msgraph/v1/rest/users/contacts/
list.rs1use alloc::format;
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use serde::Serialize;
11use url::Url;
12
13use crate::{
14 coroutine::*,
15 msgraph_try,
16 v1::{
17 page::MsgraphPage,
18 query::to_query_pairs,
19 rest::users::contacts::MsgraphContact,
20 send::{
21 MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
22 user_path,
23 },
24 },
25};
26
27pub type MsgraphContactsListResponse = MsgraphPage<MsgraphContact>;
29
30#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
32pub struct MsgraphContactsListParams<'a> {
33 #[serde(rename = "$top")]
35 pub top: Option<u32>,
36 #[serde(rename = "$skip")]
38 pub skip: Option<u32>,
39 #[serde(rename = "$select")]
41 pub select: Option<&'a str>,
42 #[serde(rename = "$filter")]
44 pub filter: Option<&'a str>,
45 #[serde(rename = "$orderby")]
47 pub orderby: Option<&'a str>,
48 #[serde(rename = "$expand")]
50 pub expand: Option<&'a str>,
51 #[serde(rename = "$count")]
53 pub count: Option<bool>,
54}
55
56pub struct MsgraphContactsList {
58 send: MsgraphSend<MsgraphContactsListResponse>,
59}
60
61impl MsgraphContactsList {
62 pub fn new(
65 auth: &HttpAuthBearer,
66 user_id: &str,
67 folder: Option<&str>,
68 params: &MsgraphContactsListParams,
69 ) -> Result<Self, MsgraphSendError> {
70 debug!("prepare microsoft graph contacts listing");
71 trace!("folder: {folder:?}");
72 trace!("params: {params:?}");
73
74 let user = user_path(user_id)?;
75 let path = match folder {
76 Some(folder) => format!("{user}/contactFolders/{}/contacts", path_segment(folder)?),
77 None => format!("{user}/contacts"),
78 };
79 let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
80 url.query_pairs_mut().extend_pairs(to_query_pairs(params));
81
82 let send = MsgraphSend::get(auth, url);
83
84 Ok(Self { send })
85 }
86
87 pub fn max_page_size(mut self, size: u32) -> Self {
93 self.send = self.send.max_page_size(size);
94 self
95 }
96}
97
98impl MsgraphCoroutine for MsgraphContactsList {
99 type Yield = MsgraphYield;
100 type Return = Result<MsgraphSendOutput<MsgraphContactsListResponse>, MsgraphSendError>;
101
102 fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
103 let out = msgraph_try!(&mut self.send, arg);
104 debug!("contacts listed");
105 trace!("out: {out:?}");
106 MsgraphCoroutineState::Complete(Ok(out))
107 }
108}