gtasks/
lib.rs

1use reqwest::Response;
2
3mod errors;
4mod http;
5mod tasklists;
6mod tasks;
7
8use errors::{Result, TasksError::ResponseError};
9use http::{AuthMiddleware, HttpClient};
10
11pub use tasklists::{
12    ListOptions as TasklistsOptions, {Tasklist, Tasklists},
13};
14
15pub use tasks::{
16    InsertOptions as TaskInsertOptions, ListOptions as TaskOptions,
17    {Task, TaskLink, TaskStatus, Tasks},
18};
19
20const BASE_URL: &str = "https://www.googleapis.com/tasks/v1";
21
22/// Service is an abstraction over google tasks.
23pub struct Service {
24    http_client: HttpClient,
25}
26
27impl Service {
28    /// Creates a new service with the given token provider.
29    pub fn with_auth<P>(token_provider: P) -> Result<Self>
30    where
31        P: http::TokenProvider,
32    {
33        let http_client = AuthMiddleware::new(token_provider).init_http_client()?;
34
35        Ok(Service { http_client })
36    }
37
38    /// Creates a new service with the given access token.
39    pub fn with_token(access_token: &str) -> Result<Self> {
40        let access_token = access_token.to_owned();
41        Self::with_auth(move || Ok(access_token.clone()))
42    }
43
44    /// Creates a new service with the given access token.
45    #[deprecated(since = "0.5.0", note = "Please use `Service::with_token` instead")]
46    pub fn new(access_token: &str) -> Result<Self> {
47        Self::with_token(access_token)
48    }
49
50    /// Returns all the authenticated user's task lists.
51    pub async fn list_tasklists(&self, opt: Option<TasklistsOptions>) -> Result<Tasklists> {
52        tasklists::list(&self.http_client, opt).await
53    }
54
55    /// Returns the authenticated user's specified task list.
56    pub async fn get_tasklist(&self, id: &str) -> Result<Tasklist> {
57        tasklists::get(&self.http_client, id).await
58    }
59
60    /// Creates a new task list and adds it to the authenticated user's task lists.
61    pub async fn insert_tasklist(&self, v: tasklists::Tasklist) -> Result<Tasklist> {
62        tasklists::insert(&self.http_client, v).await
63    }
64
65    /// Updates the authenticated user's specified task list.
66    pub async fn update_tasklist(&self, v: Tasklist) -> Result<Tasklist> {
67        tasklists::update(&self.http_client, v).await
68    }
69
70    /// Deletes the authenticated user's specified task list.
71    pub async fn delete_tasklist(&self, id: &str) -> Result<()> {
72        tasklists::delete(&self.http_client, id).await
73    }
74
75    /// Updates the authenticated user's specified task list. This method supports patch semantics.
76    pub async fn patch_tasklist(&self, tasklist_id: &str, v: Tasklist) -> Result<Tasklist> {
77        tasklists::patch(&self.http_client, tasklist_id, v).await
78    }
79
80    /// Returns all tasks in the specified task list.
81    pub async fn list_tasks(
82        &self,
83        tasklist_id: &str,
84        opt: Option<TaskOptions>,
85        etag: Option<String>,
86    ) -> Result<Option<Tasks>> {
87        tasks::list(&self.http_client, tasklist_id, opt, etag).await
88    }
89
90    /// Returns the specified task.
91    pub async fn get_task(
92        &self,
93        tasklist_id: &str,
94        task_id: &str,
95        etag: Option<String>,
96    ) -> Result<Option<Task>> {
97        tasks::get(&self.http_client, tasklist_id, task_id, etag).await
98    }
99
100    /// Creates a new task on the specified task list.
101    pub async fn insert_task(
102        &self,
103        tasklist_id: &str,
104        v: Task,
105        opts: Option<TaskInsertOptions>,
106    ) -> Result<Task> {
107        tasks::insert(&self.http_client, tasklist_id, v, opts).await
108    }
109
110    /// Updates the specified task.
111    pub async fn update_task(&self, tasklist_id: &str, v: Task) -> Result<Task> {
112        tasks::update(&self.http_client, tasklist_id, v).await
113    }
114
115    /// Deletes the specified task from the task list.
116    pub async fn delete_task(&self, tasklist_id: &str, task_id: &str) -> Result<()> {
117        tasks::delete(&self.http_client, tasklist_id, task_id).await
118    }
119
120    /// Clears all completed tasks from the specified task list.
121    /// The affected tasks will be marked as 'hidden' and no longer be returned by default when retrieving all tasks for a task list.
122    pub async fn clear_tasks(&self, tasklist_id: &str) -> Result<()> {
123        tasks::clear(&self.http_client, tasklist_id).await
124    }
125
126    /// Moves the specified task to another position in the task list.
127    /// This can include putting it as a child task under a new parent and/or move it to a different position among its sibling tasks.
128    pub async fn move_task(
129        &self,
130        tasklist_id: &str,
131        task_id: &str,
132        opts: TaskInsertOptions,
133    ) -> Result<Task> {
134        tasks::move_task(&self.http_client, tasklist_id, task_id, opts).await
135    }
136
137    /// Updates the specified task. This method supports patch semantics.
138    pub async fn patch_task(&self, tasklist_id: &str, task_id: &str, v: Task) -> Result<Task> {
139        tasks::patch(&self.http_client, tasklist_id, task_id, v).await
140    }
141}
142
143async fn ensure_status_success(resp: Response) -> Result<Response> {
144    if !resp.status().is_success() {
145        return Err(ResponseError(resp.text().await?));
146    }
147
148    Ok(resp)
149}
150
151#[cfg(test)]
152mod tests {
153    #[test]
154    fn it_works() {
155        assert_eq!(2 + 2, 4);
156    }
157}