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, string::String, vec::Vec};
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use serde::{Deserialize, Serialize};
11use url::Url;
12
13use crate::{
14    coroutine::*,
15    msgraph_try,
16    v1::{
17        query::to_query_pairs,
18        rest::users::contacts::MsgraphContact,
19        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
20    },
21};
22
23/// One page of contacts (`value` plus the OData paging link).
24#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
25pub struct MsgraphContactsListResponse {
26    /// The contacts of the page.
27    #[serde(default)]
28    pub value: Vec<MsgraphContact>,
29    /// The URL of the next page, when one exists.
30    #[serde(default, rename = "@odata.nextLink")]
31    pub next_link: Option<String>,
32}
33
34/// OData query parameters for listing contacts.
35#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
36pub struct MsgraphContactsListParams<'a> {
37    /// Maximum number of contacts per page (`$top`).
38    #[serde(rename = "$top")]
39    pub top: Option<u32>,
40    /// Number of contacts to skip (`$skip`).
41    #[serde(rename = "$skip")]
42    pub skip: Option<u32>,
43    /// Comma-separated properties to return (`$select`).
44    #[serde(rename = "$select")]
45    pub select: Option<&'a str>,
46    /// OData filter expression (`$filter`).
47    #[serde(rename = "$filter")]
48    pub filter: Option<&'a str>,
49    /// Comma-separated sort properties (`$orderby`).
50    #[serde(rename = "$orderby")]
51    pub orderby: Option<&'a str>,
52    /// Navigation clause to expand (`$expand`).
53    #[serde(rename = "$expand")]
54    pub expand: Option<&'a str>,
55    /// Whether the total count rides along the page (`$count`).
56    #[serde(rename = "$count")]
57    pub count: Option<bool>,
58}
59
60/// Lists the Microsoft Graph contacts of a contact folder.
61pub struct MsgraphContactsList {
62    send: MsgraphSend<MsgraphContactsListResponse>,
63}
64
65impl MsgraphContactsList {
66    /// Lists contacts in the default Contacts folder, or in `folder`
67    /// when given (a contact folder id).
68    pub fn new(
69        auth: &HttpAuthBearer,
70        user_id: &str,
71        folder: Option<&str>,
72        params: &MsgraphContactsListParams,
73    ) -> Result<Self, MsgraphSendError> {
74        debug!("prepare microsoft graph contacts listing");
75        trace!("folder: {folder:?}");
76        trace!("params: {params:?}");
77
78        let user = user_path(user_id);
79        let path = match folder {
80            Some(folder) => format!("{user}/contactFolders/{folder}/contacts"),
81            None => format!("{user}/contacts"),
82        };
83        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
84        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
85
86        let send = MsgraphSend::get(auth, url);
87
88        Ok(Self { send })
89    }
90}
91
92impl MsgraphCoroutine for MsgraphContactsList {
93    type Yield = MsgraphYield;
94    type Return = Result<MsgraphSendOutput<MsgraphContactsListResponse>, MsgraphSendError>;
95
96    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
97        let out = msgraph_try!(&mut self.send, arg);
98        debug!("contacts listed");
99        trace!("out: {out:?}");
100        MsgraphCoroutineState::Complete(Ok(out))
101    }
102}