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