Skip to main content

coto/
lib.rs

1use reqwest::{header, Client, Url};
2use uuid::Uuid;
3
4// ########## Projects ##########
5
6pub async fn get_all_projects(token: &String) -> Result<std::string::String, Box<dyn std::error::Error>> {
7    let mut headers = header::HeaderMap::new();
8    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
9
10    let res = Client::new()
11        .get("https://api.todoist.com/rest/v1/projects")
12        .headers(headers)
13        .send()
14        .await?
15        .text()
16        .await?;
17
18    Ok(res)
19}
20
21pub async fn get_project(token: &String, id: &String) -> Result<std::string::String, Box<dyn std::error::Error>> {
22    let mut headers = header::HeaderMap::new();
23    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
24
25    let res = Client::new()
26        .get(Url::parse(&format!("https://api.todoist.com/rest/v1/projects/{}", id)).unwrap())
27        .headers(headers)
28        .send()
29        .await?
30        .text()
31        .await?;
32
33    Ok(res)
34}
35
36
37pub async fn new_project(token: &String, json_name: String) -> Result<(), Box<dyn std::error::Error>> {
38    let uuid = Uuid::new_v4();
39    let mut headers = header::HeaderMap::new();
40    headers.insert("Content-Type", "application/json".parse().unwrap());
41    headers.insert("X-Request-Id", uuid.to_string().parse().unwrap());
42    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
43
44    let _res = Client::new()
45        .post("https://api.todoist.com/rest/v1/projects")
46        .headers(headers)
47        .body(json_name)
48        .send()
49        .await?
50        .text()
51        .await?;
52
53    Ok(())
54}
55
56pub async fn update_project(token: &String, id: &String, json_data: String) -> Result<(), Box<dyn std::error::Error>> {
57    let uuid = Uuid::new_v4();
58    let mut headers = header::HeaderMap::new();
59    headers.insert("Content-Type", "application/json".parse().unwrap());
60    headers.insert("X-Request-Id", uuid.to_string().parse().unwrap());
61    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
62
63    let _res = Client::new()
64        .post(Url::parse(&format!("https://api.todoist.com/rest/v1/projects/{}", id)).unwrap())
65        .headers(headers)
66        .body(json_data)
67        .send()
68        .await?
69        .text()
70        .await?;
71
72    Ok(())
73}
74
75pub async fn delete_project(token: &String, id: &String) -> Result<(), Box<dyn std::error::Error>> {
76    let mut headers = header::HeaderMap::new();
77    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
78
79    let _res = Client::new()
80        .delete(Url::parse(&format!("https://api.todoist.com/rest/v1/projects/{}", id)).unwrap())
81        .headers(headers)
82        .send()
83        .await?
84        .text()
85        .await?;
86
87    Ok(())
88}
89
90// ########## Collaborators ##########
91
92pub async fn get_collaborators(token: &String, id: &String) -> Result<std::string::String, Box<dyn std::error::Error>> {
93    let mut headers = header::HeaderMap::new();
94    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
95
96    let res = Client::new()
97        .get(Url::parse(&format!("https://api.todoist.com/rest/v1/projects/{}/collaborators", id)).unwrap())
98        .headers(headers)
99        .send()
100        .await?
101        .text()
102        .await?;
103
104    Ok(res)
105}
106
107// ########## Sections ##########
108
109pub async fn get_all_sections(token: &String) -> Result<std::string::String, Box<dyn std::error::Error>> {
110    let mut headers = header::HeaderMap::new();
111    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
112
113    let res = Client::new()
114        .get("https://api.todoist.com/rest/v1/sections")
115        .headers(headers)
116        .send()
117        .await?
118        .text()
119        .await?;
120
121    Ok(res)
122}
123
124pub async fn get_project_sections(token: &String, id: &String) -> Result<std::string::String, Box<dyn std::error::Error>> {
125    let mut headers = header::HeaderMap::new();
126    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
127
128    let res = Client::new()
129        .get(Url::parse(&format!("https://api.todoist.com/rest/v1/sections?project_id={}", id)).unwrap())
130        .headers(headers)
131        .send()
132        .await?
133        .text()
134        .await?;
135
136    Ok(res)
137}
138
139pub async fn get_section(token: &String, id: &String) -> Result<std::string::String, Box<dyn std::error::Error>> {
140    let mut headers = header::HeaderMap::new();
141    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
142
143    let res = Client::new()
144        .get(Url::parse(&format!("https://api.todoist.com/rest/v1/sections/{}", id)).unwrap())
145        .headers(headers)
146        .send()
147        .await?
148        .text()
149        .await?;
150
151    Ok(res)
152}
153
154pub async fn new_section(token: &String, json_name_and_id: String) -> Result<(), Box<dyn std::error::Error>> {
155    let mut headers = header::HeaderMap::new();
156    headers.insert("Content-Type", "application/json".parse().unwrap());
157    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
158
159    let _res = Client::new()
160        .post("https://api.todoist.com/rest/v1/sections")
161        .headers(headers)
162        .body(json_name_and_id)
163        .send()
164        .await?
165        .text()
166        .await?;
167
168    Ok(())
169}
170
171
172pub async fn update_section(token: &String, id: &String, json_name: String) -> Result<(), Box<dyn std::error::Error>> {
173    let mut headers = header::HeaderMap::new();
174    headers.insert("Content-Type", "application/json".parse().unwrap());
175    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
176
177    let _res = Client::new()
178        .post(Url::parse(&format!("https://api.todoist.com/rest/v1/sections/{}", id)).unwrap())
179        .headers(headers)
180        .body(json_name)
181        .send()
182        .await?
183        .text()
184        .await?;
185
186    Ok(())
187}
188
189pub async fn delete_section(token: &String, id: &String) -> Result<(), Box<dyn std::error::Error>> {
190    let mut headers = header::HeaderMap::new();
191    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
192
193    let _res = Client::new()
194        .delete(Url::parse(&format!("https://api.todoist.com/rest/v1/sections/{}", id)).unwrap())
195        .headers(headers)
196        .send()
197        .await?
198        .text()
199        .await?;
200
201    Ok(())
202}
203
204// ########## Tasks ##########
205
206pub async fn get_all_tasks(token: &String) -> Result<std::string::String, Box<dyn std::error::Error>> {
207    let mut headers = header::HeaderMap::new();
208    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
209
210    let res = Client::new()
211        .get("https://api.todoist.com/rest/v1/tasks")
212        .headers(headers)
213        .send()
214        .await?
215        .text()
216        .await?;
217
218    Ok(res)
219}
220
221pub async fn get_task(token: &String, id: &String) -> Result<std::string::String, Box<dyn std::error::Error>> {
222    let mut headers = header::HeaderMap::new();
223    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
224
225    let res = Client::new()
226        .get(Url::parse(&format!("https://api.todoist.com/rest/v1/tasks/{}", id)).unwrap())
227        .headers(headers)
228        .send()
229        .await?
230        .text()
231        .await?;
232
233    Ok(res)
234}
235
236pub async fn new_task(token: &String, json_data: String) -> Result<(), Box<dyn std::error::Error>> {
237    let uuid = Uuid::new_v4();
238    let mut headers = header::HeaderMap::new();
239    headers.insert("Content-Type", "application/json".parse().unwrap());
240    headers.insert("X-Request-Id", uuid.to_string().parse().unwrap());
241    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
242
243    let _res = Client::new()
244        .post("https://api.todoist.com/rest/v1/tasks")
245        .headers(headers)
246        .body(json_data)
247        .send()
248        .await?
249        .text()
250        .await?;
251
252    Ok(())
253}
254
255pub async fn update_task(token: &String, id: &String, json_data: String) -> Result<std::string::String, Box<dyn std::error::Error>> {
256    let uuid = Uuid::new_v4();
257    let mut headers = header::HeaderMap::new();
258    headers.insert("Content-Type", "application/json".parse().unwrap());
259    headers.insert("X-Request-Id", uuid.to_string().parse().unwrap());
260    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
261
262    let res = Client::new()
263        .post(Url::parse(&format!("https://api.todoist.com/rest/v1/tasks/{}", id)).unwrap())
264        .headers(headers)
265        .body(json_data)
266        .send()
267        .await?
268        .text()
269        .await?;
270
271    Ok(res)
272}
273
274pub async fn close_task(token: &String, id: &String) -> Result<(), Box<dyn std::error::Error>> {
275    let mut headers = header::HeaderMap::new();
276    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
277
278    let _res = Client::new()
279        .post(Url::parse(&format!("https://api.todoist.com/rest/v1/tasks/{}/close", id)).unwrap())
280        .headers(headers)
281        .send()
282        .await?
283        .text()
284        .await?;
285
286    Ok(())
287}
288
289pub async fn reopen_task(token: &String, id: &String) -> Result<(), Box<dyn std::error::Error>> {
290    let mut headers = header::HeaderMap::new();
291    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
292
293    let _res = Client::new()
294        .post(Url::parse(&format!("https://api.todoist.com/rest/v1/tasks/{}/reopen", id)).unwrap())
295        .headers(headers)
296        .send()
297        .await?
298        .text()
299        .await?;
300
301    Ok(())
302}
303
304pub async fn delete_task(token: &String, id: &String) -> Result<(), Box<dyn std::error::Error>> {
305    let mut headers = header::HeaderMap::new();
306    headers.insert("Authorization", format!("Bearer {}", token).parse().unwrap());
307
308    let _res = Client::new()
309        .delete(Url::parse(&format!("https://api.todoist.com/rest/v1/tasks/{}", id)).unwrap())
310        .headers(headers)
311        .send()
312        .await?
313        .text()
314        .await?;
315
316    Ok(())
317}
318
319// ########## Comments ##########
320
321// ToDo
322
323// ########## Labels ##########
324
325// ToDo
326