Skip to main content

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

1//! List the Microsoft Graph contact folders
2//! (`GET /me/contactFolders`).
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/user-list-contactfolders>
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::contact_folders::MsgraphContactFoldersListResponse,
19        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
20    },
21};
22
23/// OData query parameters for listing contact folders.
24#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
25pub struct MsgraphContactFoldersListParams<'a> {
26    /// Maximum number of folders per page (`$top`).
27    #[serde(rename = "$top")]
28    pub top: Option<u32>,
29    /// Number of folders 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}
36
37/// Lists the Microsoft Graph contact folders of a mailbox.
38pub struct MsgraphContactFoldersList {
39    send: MsgraphSend<MsgraphContactFoldersListResponse>,
40}
41
42impl MsgraphContactFoldersList {
43    /// Lists the top-level contact folders, filtered by the OData
44    /// `params`.
45    pub fn new(
46        auth: &HttpAuthBearer,
47        user_id: &str,
48        params: &MsgraphContactFoldersListParams,
49    ) -> Result<Self, MsgraphSendError> {
50        debug!("prepare microsoft graph contact folders listing");
51        trace!("params: {params:?}");
52
53        let user = user_path(user_id);
54        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/contactFolders"))?;
55        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
56
57        let send = MsgraphSend::get(auth, url);
58
59        Ok(Self { send })
60    }
61}
62
63impl MsgraphCoroutine for MsgraphContactFoldersList {
64    type Yield = MsgraphYield;
65    type Return = Result<MsgraphSendOutput<MsgraphContactFoldersListResponse>, MsgraphSendError>;
66
67    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
68        let out = msgraph_try!(&mut self.send, arg);
69        debug!("contact folders listed");
70        trace!("out: {out:?}");
71        MsgraphCoroutineState::Complete(Ok(out))
72    }
73}