Skip to main content

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

1//! List Microsoft Graph calendars (`GET /me/calendars`).
2//!
3//! <https://learn.microsoft.com/en-us/graph/api/user-list-calendars>
4
5use alloc::format;
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde::Serialize;
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    msgraph_try,
15    v1::{
16        page::MsgraphPage,
17        query::to_query_pairs,
18        rest::users::calendars::MsgraphCalendar,
19        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
20    },
21};
22
23/// One page of calendars.
24pub type MsgraphCalendarsListResponse = MsgraphPage<MsgraphCalendar>;
25
26/// OData query parameters for listing calendars.
27#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
28pub struct MsgraphCalendarsListParams<'a> {
29    /// Maximum number of calendars per page (`$top`).
30    #[serde(rename = "$top")]
31    pub top: Option<u32>,
32    /// Number of calendars to skip (`$skip`).
33    #[serde(rename = "$skip")]
34    pub skip: Option<u32>,
35    /// Comma-separated properties to return (`$select`).
36    #[serde(rename = "$select")]
37    pub select: Option<&'a str>,
38    /// OData filter expression (`$filter`).
39    #[serde(rename = "$filter")]
40    pub filter: Option<&'a str>,
41}
42
43/// Lists the Microsoft Graph calendars of the user.
44pub struct MsgraphCalendarsList {
45    send: MsgraphSend<MsgraphCalendarsListResponse>,
46}
47
48impl MsgraphCalendarsList {
49    /// Lists the user's calendars.
50    pub fn new(
51        auth: &HttpAuthBearer,
52        user_id: &str,
53        params: &MsgraphCalendarsListParams,
54    ) -> Result<Self, MsgraphSendError> {
55        debug!("prepare microsoft graph calendars listing");
56        trace!("params: {params:?}");
57
58        let user = user_path(user_id)?;
59        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/calendars"))?;
60        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
61
62        let send = MsgraphSend::get(auth, url);
63
64        Ok(Self { send })
65    }
66}
67
68impl MsgraphCoroutine for MsgraphCalendarsList {
69    type Yield = MsgraphYield;
70    type Return = Result<MsgraphSendOutput<MsgraphCalendarsListResponse>, MsgraphSendError>;
71
72    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
73        let out = msgraph_try!(&mut self.send, arg);
74        debug!("calendars listed");
75        trace!("out: {out:?}");
76        MsgraphCoroutineState::Complete(Ok(out))
77    }
78}