Skip to main content

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

1//! Create a Microsoft Graph calendar (`POST /me/calendars`).
2//!
3//! <https://learn.microsoft.com/en-us/graph/api/user-post-calendars>
4
5use alloc::format;
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use url::Url;
10
11use crate::{
12    coroutine::*,
13    msgraph_try,
14    v1::{
15        rest::users::calendars::MsgraphCalendar,
16        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
17    },
18};
19
20/// Creates a Microsoft Graph calendar.
21pub struct MsgraphCalendarCreate {
22    send: MsgraphSend<MsgraphCalendar>,
23}
24
25impl MsgraphCalendarCreate {
26    /// Creates the calendar.
27    pub fn new(
28        auth: &HttpAuthBearer,
29        user_id: &str,
30        calendar: &MsgraphCalendar,
31    ) -> Result<Self, MsgraphSendError> {
32        debug!("prepare microsoft graph calendar creation");
33        trace!("calendar: {calendar:?}");
34
35        let user = user_path(user_id);
36        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/calendars"))?;
37        let send = MsgraphSend::post_json(auth, url, calendar)?;
38
39        Ok(Self { send })
40    }
41}
42
43impl MsgraphCoroutine for MsgraphCalendarCreate {
44    type Yield = MsgraphYield;
45    type Return = Result<MsgraphSendOutput<MsgraphCalendar>, MsgraphSendError>;
46
47    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
48        let out = msgraph_try!(&mut self.send, arg);
49        debug!("calendar created");
50        trace!("out: {out:?}");
51        MsgraphCoroutineState::Complete(Ok(out))
52    }
53}