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