Skip to main content

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

1//! Update a Microsoft Graph calendar (`PATCH /me/calendars/{id}`).
2//!
3//! <https://learn.microsoft.com/en-us/graph/api/calendar-update>
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/// Updates a Microsoft Graph calendar.
21pub struct MsgraphCalendarUpdate {
22    send: MsgraphSend<MsgraphCalendar>,
23}
24
25impl MsgraphCalendarUpdate {
26    /// Patches the calendar `id` with the set and null fields of `calendar`.
27    pub fn new(
28        auth: &HttpAuthBearer,
29        user_id: &str,
30        id: &str,
31        calendar: &MsgraphCalendar,
32    ) -> Result<Self, MsgraphSendError> {
33        debug!("prepare microsoft graph calendar update");
34        trace!("id: {id:?}");
35        trace!("calendar: {calendar:?}");
36
37        let user = user_path(user_id);
38        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/calendars/{id}"))?;
39        let send = MsgraphSend::patch_json(auth, url, calendar)?;
40
41        Ok(Self { send })
42    }
43}
44
45impl MsgraphCoroutine for MsgraphCalendarUpdate {
46    type Yield = MsgraphYield;
47    type Return = Result<MsgraphSendOutput<MsgraphCalendar>, MsgraphSendError>;
48
49    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
50        let out = msgraph_try!(&mut self.send, arg);
51        debug!("calendar updated");
52        trace!("out: {out:?}");
53        MsgraphCoroutineState::Complete(Ok(out))
54    }
55}