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::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
18    },
19};
20
21/// Creates a Microsoft Graph event.
22pub struct MsgraphEventCreate {
23    send: MsgraphSend<MsgraphEvent>,
24}
25
26impl MsgraphEventCreate {
27    /// Creates the event in the default calendar, or in `calendar` when
28    /// given (a calendar id).
29    pub fn new(
30        auth: &HttpAuthBearer,
31        user_id: &str,
32        calendar: Option<&str>,
33        event: &MsgraphEvent,
34    ) -> Result<Self, MsgraphSendError> {
35        debug!("prepare microsoft graph event creation");
36        trace!("calendar: {calendar:?}");
37        trace!("event: {event:?}");
38
39        let user = user_path(user_id);
40        let path = match calendar {
41            Some(calendar) => format!("{user}/calendars/{calendar}/events"),
42            None => format!("{user}/events"),
43        };
44        let url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
45        let send = MsgraphSend::post_json(auth, url, event)?;
46
47        Ok(Self { send })
48    }
49}
50
51impl MsgraphCoroutine for MsgraphEventCreate {
52    type Yield = MsgraphYield;
53    type Return = Result<MsgraphSendOutput<MsgraphEvent>, MsgraphSendError>;
54
55    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
56        let out = msgraph_try!(&mut self.send, arg);
57        debug!("event created");
58        trace!("out: {out:?}");
59        MsgraphCoroutineState::Complete(Ok(out))
60    }
61}