Skip to main content

prismer_sdk/
tasks.rs

1use crate::{PrismerClient, types::*};
2use serde_json::json;
3
4pub struct TasksClient<'a> {
5    pub(crate) client: &'a PrismerClient,
6}
7
8impl<'a> TasksClient<'a> {
9    /// Create a new task.
10    pub async fn create(
11        &self,
12        title: &str,
13        description: Option<&str>,
14        capability: Option<&str>,
15        assignee_id: Option<&str>,
16        budget: Option<f64>,
17    ) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
18        let mut body = json!({ "title": title });
19        if let Some(d) = description { body["description"] = json!(d); }
20        if let Some(c) = capability { body["capability"] = json!(c); }
21        if let Some(a) = assignee_id { body["assigneeId"] = json!(a); }
22        if let Some(b) = budget { body["budget"] = json!(b); }
23        self.client.request(reqwest::Method::POST, "/api/im/tasks", Some(body)).await
24    }
25
26    /// List tasks with optional filters.
27    pub async fn list(
28        &self,
29        status: Option<&str>,
30        capability: Option<&str>,
31        limit: Option<u32>,
32    ) -> Result<ApiResponse<Vec<serde_json::Value>>, PrismerError> {
33        let mut params = vec![];
34        if let Some(s) = status { params.push(format!("status={}", s)); }
35        if let Some(c) = capability { params.push(format!("capability={}", c)); }
36        if let Some(l) = limit { params.push(format!("limit={}", l)); }
37        let qs = if params.is_empty() { String::new() } else { format!("?{}", params.join("&")) };
38        self.client.request(reqwest::Method::GET, &format!("/api/im/tasks{}", qs), None).await
39    }
40
41    /// Get a task by ID.
42    pub async fn get(&self, task_id: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
43        self.client.request(reqwest::Method::GET, &format!("/api/im/tasks/{}", task_id), None).await
44    }
45
46    /// Claim a task.
47    pub async fn claim(&self, task_id: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
48        self.client.request(reqwest::Method::POST, &format!("/api/im/tasks/{}/claim", task_id), None).await
49    }
50
51    /// Report task progress.
52    pub async fn progress(&self, task_id: &str, message: Option<&str>) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
53        let body = message.map(|m| json!({ "message": m }));
54        self.client.request(reqwest::Method::POST, &format!("/api/im/tasks/{}/progress", task_id), body).await
55    }
56
57    /// Complete a task.
58    pub async fn complete(&self, task_id: &str, result: Option<serde_json::Value>) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
59        let body = result.map(|r| json!({ "result": r }));
60        self.client.request(reqwest::Method::POST, &format!("/api/im/tasks/{}/complete", task_id), body).await
61    }
62
63    /// Fail a task.
64    pub async fn fail(&self, task_id: &str, error: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
65        self.client.request(
66            reqwest::Method::POST,
67            &format!("/api/im/tasks/{}/fail", task_id),
68            Some(json!({ "error": error })),
69        ).await
70    }
71
72    /// Approve a completed task.
73    pub async fn approve(&self, task_id: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
74        self.client.request(
75            reqwest::Method::POST,
76            &format!("/api/im/tasks/{}/approve", task_id),
77            None,
78        ).await
79    }
80
81    /// Reject a task with a reason.
82    pub async fn reject(&self, task_id: &str, reason: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
83        self.client.request(
84            reqwest::Method::POST,
85            &format!("/api/im/tasks/{}/reject", task_id),
86            Some(json!({ "reason": reason })),
87        ).await
88    }
89
90    /// Cancel a task.
91    pub async fn cancel(&self, task_id: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
92        self.client.request(
93            reqwest::Method::DELETE,
94            &format!("/api/im/tasks/{}", task_id),
95            None,
96        ).await
97    }
98}