Skip to main content

io_msgraph/v1/rest/users/contacts/
list.rs

1//! List Microsoft Graph contacts (`GET /me/contacts` or
2//! `GET /me/contactFolders/{id}/contacts`).
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/user-list-contacts>
5
6use 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        query::to_query_pairs,
18        rest::users::contacts::MsgraphContactsListResponse,
19        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
20    },
21};
22
23/// OData query parameters for listing contacts.
24#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
25pub struct MsgraphContactsListParams<'a> {
26    /// Maximum number of contacts per page (`$top`).
27    #[serde(rename = "$top")]
28    pub top: Option<u32>,
29    /// Number of contacts to skip (`$skip`).
30    #[serde(rename = "$skip")]
31    pub skip: Option<u32>,
32    /// Comma-separated properties to return (`$select`).
33    #[serde(rename = "$select")]
34    pub select: Option<&'a str>,
35    /// OData filter expression (`$filter`).
36    #[serde(rename = "$filter")]
37    pub filter: Option<&'a str>,
38    /// Comma-separated sort properties (`$orderby`).
39    #[serde(rename = "$orderby")]
40    pub orderby: Option<&'a str>,
41    /// Navigation clause to expand (`$expand`).
42    #[serde(rename = "$expand")]
43    pub expand: Option<&'a str>,
44    /// Whether the total count rides along the page (`$count`).
45    #[serde(rename = "$count")]
46    pub count: Option<bool>,
47}
48
49/// Lists the Microsoft Graph contacts of a contact folder.
50pub struct MsgraphContactsList {
51    send: MsgraphSend<MsgraphContactsListResponse>,
52}
53
54impl MsgraphContactsList {
55    /// Lists contacts in the default Contacts folder, or in `folder`
56    /// when given (a contact folder id).
57    pub fn new(
58        auth: &HttpAuthBearer,
59        user_id: &str,
60        folder: Option<&str>,
61        params: &MsgraphContactsListParams,
62    ) -> Result<Self, MsgraphSendError> {
63        debug!("prepare microsoft graph contacts listing");
64        trace!("folder: {folder:?}");
65        trace!("params: {params:?}");
66
67        let user = user_path(user_id);
68        let path = match folder {
69            Some(folder) => format!("{user}/contactFolders/{folder}/contacts"),
70            None => format!("{user}/contacts"),
71        };
72        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
73        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
74
75        let send = MsgraphSend::get(auth, url);
76
77        Ok(Self { send })
78    }
79}
80
81impl MsgraphCoroutine for MsgraphContactsList {
82    type Yield = MsgraphYield;
83    type Return = Result<MsgraphSendOutput<MsgraphContactsListResponse>, MsgraphSendError>;
84
85    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
86        let out = msgraph_try!(&mut self.send, arg);
87        debug!("contacts listed");
88        trace!("out: {out:?}");
89        MsgraphCoroutineState::Complete(Ok(out))
90    }
91}