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
22pub struct Service {
24 http_client: HttpClient,
25}
26
27impl Service {
28 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 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 #[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 pub async fn list_tasklists(&self, opt: Option<TasklistsOptions>) -> Result<Tasklists> {
52 tasklists::list(&self.http_client, opt).await
53 }
54
55 pub async fn get_tasklist(&self, id: &str) -> Result<Tasklist> {
57 tasklists::get(&self.http_client, id).await
58 }
59
60 pub async fn insert_tasklist(&self, v: tasklists::Tasklist) -> Result<Tasklist> {
62 tasklists::insert(&self.http_client, v).await
63 }
64
65 pub async fn update_tasklist(&self, v: Tasklist) -> Result<Tasklist> {
67 tasklists::update(&self.http_client, v).await
68 }
69
70 pub async fn delete_tasklist(&self, id: &str) -> Result<()> {
72 tasklists::delete(&self.http_client, id).await
73 }
74
75 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 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 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 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 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 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 pub async fn clear_tasks(&self, tasklist_id: &str) -> Result<()> {
123 tasks::clear(&self.http_client, tasklist_id).await
124 }
125
126 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 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}