todoist-api 0.3.1

A Rust wrapper for the Todoist REST API v2
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use reqwest::Client;
use serde_json::Value;

use crate::models::*;

const TODOIST_API_BASE: &str = "https://api.todoist.com/rest/v2";

/// A comprehensive wrapper around the Todoist REST API v2
#[derive(Clone)]
pub struct TodoistWrapper {
    client: Client,
    api_token: String,
}

impl TodoistWrapper {
    /// Create a new Todoist client
    #[must_use]
    pub fn new(api_token: String) -> Self {
        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(10))
            .build()
            .unwrap_or_else(|_| Client::new());
        Self { client, api_token }
    }

    /// Helper method for making GET requests
    async fn make_get_request<T>(&self, endpoint: &str) -> TodoistResult<T>
    where
        T: serde::de::DeserializeOwned,
    {
        self.make_get_request_with_params(endpoint, &[] as &[(&str, String)])
            .await
    }

    /// Helper method for making GET requests with query parameters
    async fn make_get_request_with_params<T>(&self, endpoint: &str, query_params: &[(&str, String)]) -> TodoistResult<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let url = format!("{TODOIST_API_BASE}{endpoint}");

        let response = self
            .client
            .get(&url)
            .query(query_params)
            .bearer_auth(&self.api_token)
            .send()
            .await
            .map_err(|e| TodoistError::NetworkError {
                message: format!("Failed to send request: {}", e),
            })?;

        self.handle_response("GET", endpoint, response).await
    }

    /// Helper method for making POST requests
    async fn make_post_request<T>(&self, endpoint: &str, body: Option<&Value>) -> TodoistResult<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let url = format!("{TODOIST_API_BASE}{endpoint}");
        let mut request = self
            .client
            .post(&url)
            .bearer_auth(&self.api_token)
            .header("Content-Type", "application/json");

        if let Some(body_value) = body {
            request = request.json(body_value);
        }

        let response = request.send().await.map_err(|e| TodoistError::NetworkError {
            message: format!("Failed to send request: {}", e),
        })?;

        self.handle_response("POST", endpoint, response).await
    }

    /// Helper method for making DELETE requests
    async fn make_delete_request<T>(&self, endpoint: &str) -> TodoistResult<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let url = format!("{TODOIST_API_BASE}{endpoint}");
        let response = self
            .client
            .delete(&url)
            .bearer_auth(&self.api_token)
            .send()
            .await
            .map_err(|e| TodoistError::NetworkError {
                message: format!("Failed to send request: {}", e),
            })?;

        self.handle_response("DELETE", endpoint, response).await
    }

    /// Helper method to handle HTTP responses and convert them to TodoistResult
    async fn handle_response<T>(
        &self,
        http_method: &str,
        endpoint: &str,
        response: reqwest::Response,
    ) -> TodoistResult<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let status = response.status();
        let headers = response.headers().clone();

        if status.is_success() {
            // Read response body
            let text = response.text().await.map_err(|e| TodoistError::NetworkError {
                message: format!("Failed to read response body: {}", e),
            })?;

            // For DELETE requests, empty responses are expected and valid
            if http_method == "DELETE" && text.trim().is_empty() {
                // Try to deserialize "null" for empty DELETE responses
                return serde_json::from_str::<T>("null").map_err(|e| TodoistError::ParseError {
                    message: format!("Failed to deserialize empty DELETE response: {}", e),
                });
            }

            // For POST requests to close/reopen tasks, empty responses or 204 are expected and valid
            if http_method == "POST" && (status.as_u16() == 204 || text.trim().is_empty()) {
                // Try to deserialize "null" for empty POST responses
                return serde_json::from_str::<T>("null").map_err(|e| TodoistError::ParseError {
                    message: format!("Failed to deserialize empty POST response: {}", e),
                });
            }

            // Handle empty responses for other methods
            if text.trim().is_empty() {
                return Err(empty_response_error(endpoint, "API returned empty response body"));
            }

            // Try to parse response
            serde_json::from_str::<T>(&text).map_err(|e| TodoistError::ParseError {
                message: format!("Failed to parse response: {}", e),
            })
        } else {
            // Handle different error status codes
            let error_text = response
                .text()
                .await
                .unwrap_or_else(|_| format!("Unknown error occurred (HTTP {})", status));

            let error = match status.as_u16() {
                401 => TodoistError::AuthenticationError { message: error_text },
                403 => TodoistError::AuthorizationError { message: error_text },
                404 => TodoistError::NotFound {
                    resource_type: "Resource".to_string(),
                    resource_id: None,
                    message: error_text,
                },
                429 => {
                    let retry_after = headers
                        .get("Retry-After")
                        .and_then(|v| v.to_str().ok())
                        .and_then(|s| s.parse::<u64>().ok());
                    TodoistError::RateLimited {
                        retry_after,
                        message: error_text,
                    }
                }
                400 => TodoistError::ValidationError {
                    field: None,
                    message: error_text,
                },
                500..=599 => TodoistError::ServerError {
                    status_code: status.as_u16(),
                    message: error_text,
                },
                _ => TodoistError::Generic {
                    status_code: Some(status.as_u16()),
                    message: error_text,
                },
            };

            Err(error)
        }
    }

    // ===== PROJECT OPERATIONS =====

    /// Get all projects
    pub async fn get_projects(&self) -> TodoistResult<Vec<Project>> {
        self.make_get_request("/projects").await
    }

    /// Get projects with filtering and pagination
    pub async fn get_projects_filtered(&self, args: &ProjectFilterArgs) -> TodoistResult<Vec<Project>> {
        let mut query_params = Vec::new();

        if let Some(limit) = args.limit {
            query_params.push(("limit", limit.to_string()));
        }
        if let Some(cursor) = &args.cursor {
            query_params.push(("cursor", cursor.clone()));
        }

        self.make_get_request_with_params("/projects", &query_params).await
    }

    /// Get a specific project by ID
    pub async fn get_project(&self, project_id: &str) -> TodoistResult<Project> {
        self.make_get_request(&format!("/projects/{project_id}")).await
    }

    /// Create a new project
    pub async fn create_project(&self, args: &CreateProjectArgs) -> TodoistResult<Project> {
        let body_value = serde_json::to_value(args)?;
        self.make_post_request("/projects", Some(&body_value)).await
    }

    /// Update an existing project
    pub async fn update_project(&self, project_id: &str, args: &UpdateProjectArgs) -> TodoistResult<Project> {
        if !args.has_updates() {
            return Err(TodoistError::ValidationError {
                field: None,
                message: "No fields specified for update".to_string(),
            });
        }
        let body_value = serde_json::to_value(args)?;
        self.make_post_request(&format!("/projects/{project_id}"), Some(&body_value))
            .await
    }

    /// Delete a project
    pub async fn delete_project(&self, project_id: &str) -> TodoistResult<()> {
        self.make_delete_request(&format!("/projects/{project_id}")).await
    }

    // ===== TASK OPERATIONS =====

    /// Get all tasks
    pub async fn get_tasks(&self) -> TodoistResult<Vec<Task>> {
        self.make_get_request("/tasks").await
    }

    /// Get tasks for a specific project
    pub async fn get_tasks_for_project(&self, project_id: &str) -> TodoistResult<Vec<Task>> {
        let query_params = vec![("project_id", project_id.to_string())];
        self.make_get_request_with_params("/tasks", &query_params).await
    }

    /// Get a specific task by ID
    pub async fn get_task(&self, task_id: &str) -> TodoistResult<Task> {
        self.make_get_request(&format!("/tasks/{task_id}")).await
    }

    /// Get tasks by filter query
    pub async fn get_tasks_by_filter(&self, args: &TaskFilterArgs) -> TodoistResult<Vec<Task>> {
        let mut query_params = vec![("query", args.query.clone())];

        if let Some(lang) = &args.lang {
            query_params.push(("lang", lang.clone()));
        }
        if let Some(limit) = args.limit {
            query_params.push(("limit", limit.to_string()));
        }
        if let Some(cursor) = &args.cursor {
            query_params.push(("cursor", cursor.clone()));
        }

        self.make_get_request_with_params("/tasks", &query_params).await
    }

    /// Create a new task
    pub async fn create_task(&self, args: &CreateTaskArgs) -> TodoistResult<Task> {
        let body_value = serde_json::to_value(args)?;
        self.make_post_request("/tasks", Some(&body_value)).await
    }

    /// Update an existing task
    pub async fn update_task(&self, task_id: &str, args: &UpdateTaskArgs) -> TodoistResult<Task> {
        if !args.has_updates() {
            return Err(TodoistError::ValidationError {
                field: None,
                message: "No fields specified for update".to_string(),
            });
        }
        let body_value = serde_json::to_value(args)?;
        self.make_post_request(&format!("/tasks/{task_id}"), Some(&body_value))
            .await
    }

    /// Complete a task
    pub async fn complete_task(&self, task_id: &str) -> TodoistResult<()> {
        self.make_post_request(&format!("/tasks/{task_id}/close"), None).await
    }

    /// Reopen a completed task
    pub async fn reopen_task(&self, task_id: &str) -> TodoistResult<()> {
        self.make_post_request(&format!("/tasks/{task_id}/reopen"), None).await
    }

    /// Delete a task
    pub async fn delete_task(&self, task_id: &str) -> TodoistResult<()> {
        self.make_delete_request(&format!("/tasks/{task_id}")).await
    }

    // ===== LABEL OPERATIONS =====

    /// Get all labels
    pub async fn get_labels(&self) -> TodoistResult<Vec<Label>> {
        self.make_get_request("/labels").await
    }

    /// Get labels with filtering and pagination
    pub async fn get_labels_filtered(&self, args: &LabelFilterArgs) -> TodoistResult<Vec<Label>> {
        let mut query_params = Vec::new();

        if let Some(limit) = args.limit {
            query_params.push(("limit", limit.to_string()));
        }
        if let Some(cursor) = &args.cursor {
            query_params.push(("cursor", cursor.clone()));
        }

        self.make_get_request_with_params("/labels", &query_params).await
    }

    /// Get a specific label by ID
    pub async fn get_label(&self, label_id: &str) -> TodoistResult<Label> {
        self.make_get_request(&format!("/labels/{label_id}")).await
    }

    /// Create a new label
    pub async fn create_label(&self, args: &CreateLabelArgs) -> TodoistResult<Label> {
        let body_value = serde_json::to_value(args)?;
        self.make_post_request("/labels", Some(&body_value)).await
    }

    /// Update an existing label
    pub async fn update_label(&self, label_id: &str, args: &UpdateLabelArgs) -> TodoistResult<Label> {
        if !args.has_updates() {
            return Err(TodoistError::ValidationError {
                field: None,
                message: "No fields specified for update".to_string(),
            });
        }
        let body_value = serde_json::to_value(args)?;
        self.make_post_request(&format!("/labels/{label_id}"), Some(&body_value))
            .await
    }

    /// Delete a label
    pub async fn delete_label(&self, label_id: &str) -> TodoistResult<()> {
        self.make_delete_request(&format!("/labels/{label_id}")).await
    }

    // ===== SECTION OPERATIONS =====

    /// Get all sections
    pub async fn get_sections(&self) -> TodoistResult<Vec<Section>> {
        self.make_get_request("/sections").await
    }

    /// Get sections with filtering and pagination
    pub async fn get_sections_filtered(&self, args: &SectionFilterArgs) -> TodoistResult<Vec<Section>> {
        let mut query_params = Vec::new();

        if let Some(project_id) = &args.project_id {
            query_params.push(("project_id", project_id.clone()));
        }
        if let Some(limit) = args.limit {
            query_params.push(("limit", limit.to_string()));
        }
        if let Some(cursor) = &args.cursor {
            query_params.push(("cursor", cursor.clone()));
        }

        self.make_get_request_with_params("/sections", &query_params).await
    }

    /// Get a specific section by ID
    pub async fn get_section(&self, section_id: &str) -> TodoistResult<Section> {
        self.make_get_request(&format!("/sections/{section_id}")).await
    }

    /// Create a new section
    pub async fn create_section(&self, args: &CreateSectionArgs) -> TodoistResult<Section> {
        let body_value = serde_json::to_value(args)?;
        self.make_post_request("/sections", Some(&body_value)).await
    }

    /// Update an existing section
    pub async fn update_section(&self, section_id: &str, args: &UpdateSectionArgs) -> TodoistResult<Section> {
        let body_value = serde_json::to_value(args)?;
        self.make_post_request(&format!("/sections/{section_id}"), Some(&body_value))
            .await
    }

    /// Delete a section
    pub async fn delete_section(&self, section_id: &str) -> TodoistResult<()> {
        self.make_delete_request(&format!("/sections/{section_id}")).await
    }

    // ===== COMMENT OPERATIONS =====

    /// Get all comments
    pub async fn get_comments(&self) -> TodoistResult<Vec<Comment>> {
        self.make_get_request("/comments").await
    }

    /// Get comments with filtering and pagination
    pub async fn get_comments_filtered(&self, args: &CommentFilterArgs) -> TodoistResult<Vec<Comment>> {
        let mut query_params = Vec::new();

        if let Some(task_id) = &args.task_id {
            query_params.push(("task_id", task_id.clone()));
        }
        if let Some(project_id) = &args.project_id {
            query_params.push(("project_id", project_id.clone()));
        }
        if let Some(limit) = args.limit {
            query_params.push(("limit", limit.to_string()));
        }
        if let Some(cursor) = &args.cursor {
            query_params.push(("cursor", cursor.clone()));
        }

        self.make_get_request_with_params("/comments", &query_params).await
    }

    /// Get a specific comment by ID
    pub async fn get_comment(&self, comment_id: &str) -> TodoistResult<Comment> {
        self.make_get_request(&format!("/comments/{comment_id}")).await
    }

    /// Create a new comment
    pub async fn create_comment(&self, args: &CreateCommentArgs) -> TodoistResult<Comment> {
        let body_value = serde_json::to_value(args)?;
        self.make_post_request("/comments", Some(&body_value)).await
    }

    /// Update an existing comment
    pub async fn update_comment(&self, comment_id: &str, args: &UpdateCommentArgs) -> TodoistResult<Comment> {
        if !args.has_updates() {
            return Err(TodoistError::ValidationError {
                field: None,
                message: "No fields specified for update".to_string(),
            });
        }
        let body_value = serde_json::to_value(args)?;
        self.make_post_request(&format!("/comments/{comment_id}"), Some(&body_value))
            .await
    }

    /// Delete a comment
    pub async fn delete_comment(&self, comment_id: &str) -> TodoistResult<()> {
        self.make_delete_request(&format!("/comments/{comment_id}")).await
    }
}