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