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