Skip to main content

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

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