Skip to main content

io_msgraph/v1/rest/users/todo/tasks/
create.rs

1//! Create a Microsoft To Do task (`POST /me/todo/lists/{id}/tasks`).
2//!
3//! <https://learn.microsoft.com/en-us/graph/api/todotasklist-post-tasks>
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::todo::tasks::MsgraphTodoTask,
16        send::{
17            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
18            user_path,
19        },
20    },
21};
22
23/// Creates a Microsoft To Do task.
24pub struct MsgraphTodoTaskCreate {
25    send: MsgraphSend<MsgraphTodoTask>,
26}
27
28impl MsgraphTodoTaskCreate {
29    /// Creates the task in the list `list_id`, its linked resources and
30    /// extended properties included.
31    pub fn new(
32        auth: &HttpAuthBearer,
33        user_id: &str,
34        list_id: &str,
35        task: &MsgraphTodoTask,
36    ) -> Result<Self, MsgraphSendError> {
37        debug!("prepare microsoft graph to do task creation");
38        trace!("list: {list_id:?}");
39        trace!("task: {task:?}");
40
41        let user = user_path(user_id)?;
42        let path = format!("{user}/todo/lists/{}/tasks", path_segment(list_id)?);
43        let url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
44        let send = MsgraphSend::post_json(auth, url, task)?;
45
46        Ok(Self { send })
47    }
48}
49
50impl MsgraphCoroutine for MsgraphTodoTaskCreate {
51    type Yield = MsgraphYield;
52    type Return = Result<MsgraphSendOutput<MsgraphTodoTask>, MsgraphSendError>;
53
54    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
55        let out = msgraph_try!(&mut self.send, arg);
56        debug!("to do task created");
57        trace!("out: {out:?}");
58        MsgraphCoroutineState::Complete(Ok(out))
59    }
60}