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