Skip to main content

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

1//! List the tasks of a Microsoft To Do list
2//! (`GET /me/todo/lists/{id}/tasks`).
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/todotasklist-list-tasks>
5
6use alloc::format;
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use serde::Serialize;
11use url::Url;
12
13use crate::{
14    coroutine::*,
15    msgraph_try,
16    v1::{
17        page::{MsgraphPage, link_url},
18        query::to_query_pairs,
19        rest::users::todo::tasks::MsgraphTodoTask,
20        send::{
21            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
22            user_path,
23        },
24    },
25};
26
27/// One page of tasks.
28pub type MsgraphTodoTasksListResponse = MsgraphPage<MsgraphTodoTask>;
29
30/// OData query parameters for listing tasks.
31#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
32pub struct MsgraphTodoTasksListParams<'a> {
33    /// Maximum number of tasks per page (`$top`).
34    #[serde(rename = "$top")]
35    pub top: Option<u32>,
36    /// Number of tasks to skip (`$skip`).
37    #[serde(rename = "$skip")]
38    pub skip: Option<u32>,
39    /// Comma-separated properties to return (`$select`).
40    #[serde(rename = "$select")]
41    pub select: Option<&'a str>,
42    /// OData filter expression (`$filter`).
43    #[serde(rename = "$filter")]
44    pub filter: Option<&'a str>,
45    /// Comma-separated sort properties (`$orderby`).
46    #[serde(rename = "$orderby")]
47    pub orderby: Option<&'a str>,
48    /// Navigation clause to expand (`$expand`).
49    #[serde(rename = "$expand")]
50    pub expand: Option<&'a str>,
51}
52
53/// Lists the tasks of a Microsoft To Do list.
54pub struct MsgraphTodoTasksList {
55    send: MsgraphSend<MsgraphTodoTasksListResponse>,
56}
57
58impl MsgraphTodoTasksList {
59    /// Lists the tasks of the list `list_id`.
60    pub fn new(
61        auth: &HttpAuthBearer,
62        user_id: &str,
63        list_id: &str,
64        params: &MsgraphTodoTasksListParams,
65    ) -> Result<Self, MsgraphSendError> {
66        debug!("prepare microsoft graph to do tasks listing");
67        trace!("list: {list_id:?}");
68        trace!("params: {params:?}");
69
70        let user = user_path(user_id)?;
71        let path = format!("{user}/todo/lists/{}/tasks", path_segment(list_id)?);
72        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
73        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
74
75        let send = MsgraphSend::get(auth, url);
76
77        Ok(Self { send })
78    }
79
80    /// Continues a listing from an `@odata.nextLink`, sent as-is. A link
81    /// off the Microsoft Graph origin is refused.
82    pub fn from_link(auth: &HttpAuthBearer, link: &str) -> Result<Self, MsgraphSendError> {
83        debug!("prepare microsoft graph to do tasks listing from link");
84        trace!("link: {link}");
85
86        let url = link_url(link)?;
87        let send = MsgraphSend::get(auth, url);
88
89        Ok(Self { send })
90    }
91
92    /// Asks Graph for at most `size` items per page
93    /// (`Prefer: odata.maxpagesize={size}`), see
94    /// [`MsgraphSend::max_page_size`]. Graph keeps no memory of it
95    /// across pages: chain it on every page request, the ones built
96    /// from an `@odata.nextLink` included.
97    pub fn max_page_size(mut self, size: u32) -> Self {
98        self.send = self.send.max_page_size(size);
99        self
100    }
101}
102
103impl MsgraphCoroutine for MsgraphTodoTasksList {
104    type Yield = MsgraphYield;
105    type Return = Result<MsgraphSendOutput<MsgraphTodoTasksListResponse>, MsgraphSendError>;
106
107    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
108        let out = msgraph_try!(&mut self.send, arg);
109        debug!("to do tasks listed");
110        trace!("out: {out:?}");
111        MsgraphCoroutineState::Complete(Ok(out))
112    }
113}