Skip to main content

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

1//! List Microsoft Graph events (`GET /me/events` or
2//! `GET /me/calendars/{id}/events`).
3//!
4//! The listing returns lone events and series masters, never the
5//! occurrences a series expands into; the calendar view and the instances
6//! listing do.
7//!
8//! <https://learn.microsoft.com/en-us/graph/api/user-list-events>
9
10use alloc::{format, string::String, vec::Vec};
11
12use io_http::rfc6750::bearer::HttpAuthBearer;
13use log::{debug, trace};
14use serde::{Deserialize, Serialize};
15use url::Url;
16
17use crate::{
18    coroutine::*,
19    msgraph_try,
20    v1::{
21        query::to_query_pairs,
22        rest::users::events::MsgraphEvent,
23        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
24    },
25};
26
27/// One page of events (`value` plus the OData paging link).
28#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
29pub struct MsgraphEventsListResponse {
30    /// The events of the page.
31    #[serde(default)]
32    pub value: Vec<MsgraphEvent>,
33    /// The URL of the next page, when one exists.
34    #[serde(default, rename = "@odata.nextLink")]
35    pub next_link: Option<String>,
36}
37
38/// OData query parameters for listing events.
39#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
40pub struct MsgraphEventsListParams<'a> {
41    /// Maximum number of events per page (`$top`).
42    #[serde(rename = "$top")]
43    pub top: Option<u32>,
44    /// Number of events to skip (`$skip`).
45    #[serde(rename = "$skip")]
46    pub skip: Option<u32>,
47    /// Comma-separated properties to return (`$select`).
48    #[serde(rename = "$select")]
49    pub select: Option<&'a str>,
50    /// OData filter expression (`$filter`).
51    #[serde(rename = "$filter")]
52    pub filter: Option<&'a str>,
53    /// Comma-separated sort properties (`$orderby`).
54    #[serde(rename = "$orderby")]
55    pub orderby: Option<&'a str>,
56    /// Navigation clause to expand (`$expand`).
57    #[serde(rename = "$expand")]
58    pub expand: Option<&'a str>,
59}
60
61/// Lists the Microsoft Graph events of a calendar.
62pub struct MsgraphEventsList {
63    send: MsgraphSend<MsgraphEventsListResponse>,
64}
65
66impl MsgraphEventsList {
67    /// Lists the events of the default calendar, or of `calendar` when
68    /// given (a calendar id).
69    pub fn new(
70        auth: &HttpAuthBearer,
71        user_id: &str,
72        calendar: Option<&str>,
73        params: &MsgraphEventsListParams,
74    ) -> Result<Self, MsgraphSendError> {
75        debug!("prepare microsoft graph events listing");
76        trace!("calendar: {calendar:?}");
77        trace!("params: {params:?}");
78
79        let user = user_path(user_id);
80        let path = match calendar {
81            Some(calendar) => format!("{user}/calendars/{calendar}/events"),
82            None => format!("{user}/events"),
83        };
84        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
85        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
86
87        let send = MsgraphSend::get(auth, url);
88
89        Ok(Self { send })
90    }
91
92    /// Continues a listing from an `@odata.nextLink`, sent as-is.
93    pub fn from_link(auth: &HttpAuthBearer, link: &str) -> Result<Self, MsgraphSendError> {
94        debug!("prepare microsoft graph events listing from link");
95        trace!("link: {link}");
96
97        let url = Url::parse(link)?;
98        let send = MsgraphSend::get(auth, url);
99
100        Ok(Self { send })
101    }
102}
103
104impl MsgraphCoroutine for MsgraphEventsList {
105    type Yield = MsgraphYield;
106    type Return = Result<MsgraphSendOutput<MsgraphEventsListResponse>, MsgraphSendError>;
107
108    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
109        let out = msgraph_try!(&mut self.send, arg);
110        debug!("events listed");
111        trace!("out: {out:?}");
112        MsgraphCoroutineState::Complete(Ok(out))
113    }
114}