gevulot_rs/
task_client.rs

1use std::sync::Arc;
2use tokio::sync::RwLock;
3
4use crate::{
5    base_client::BaseClient,
6    error::{Error, Result},
7    proto::gevulot::gevulot::{
8        MsgAcceptTask, MsgAcceptTaskResponse, MsgCreateTask, MsgCreateTaskResponse, MsgDeclineTask,
9        MsgDeclineTaskResponse, MsgDeleteTask, MsgDeleteTaskResponse, MsgFinishTask,
10        MsgFinishTaskResponse, MsgRescheduleTask, MsgRescheduleTaskResponse,
11    },
12};
13
14/// Client for managing tasks in the Gevulot system.
15#[derive(Debug, Clone)]
16pub struct TaskClient {
17    base_client: Arc<RwLock<BaseClient>>,
18}
19
20impl TaskClient {
21    /// Creates a new instance of TaskClient.
22    ///
23    /// # Arguments
24    ///
25    /// * `base_client` - An Arc-wrapped RwLock of the BaseClient.
26    ///
27    /// # Returns
28    ///
29    /// A new instance of TaskClient.
30    pub fn new(base_client: Arc<RwLock<BaseClient>>) -> Self {
31        Self { base_client }
32    }
33
34    /// Lists all tasks.
35    ///
36    /// # Returns
37    ///
38    /// A Result containing a vector of tasks or an error.
39    ///
40    /// # Errors
41    ///
42    /// This function will return an error if the request to the Gevulot client fails.
43    pub async fn list(&mut self) -> Result<Vec<crate::proto::gevulot::gevulot::Task>> {
44        let request = crate::proto::gevulot::gevulot::QueryAllTaskRequest { pagination: None };
45        let response = self
46            .base_client
47            .write()
48            .await
49            .gevulot_client
50            .task_all(request)
51            .await?;
52        Ok(response.into_inner().task)
53    }
54
55    /// Gets a task by its ID.
56    ///
57    /// # Arguments
58    ///
59    /// * `id` - The ID of the task to retrieve.
60    ///
61    /// # Returns
62    ///
63    /// A Result containing the task or an error.
64    ///
65    /// # Errors
66    ///
67    /// This function will return an error if the task is not found or if the request to the Gevulot client fails.
68    pub async fn get(&mut self, id: &str) -> Result<crate::proto::gevulot::gevulot::Task> {
69        let request = crate::proto::gevulot::gevulot::QueryGetTaskRequest { id: id.to_owned() };
70        let response = self
71            .base_client
72            .write()
73            .await
74            .gevulot_client
75            .task(request)
76            .await?;
77        response.into_inner().task.ok_or(Error::NotFound)
78    }
79
80    /// Creates a new task.
81    ///
82    /// # Arguments
83    ///
84    /// * `msg` - The message containing the task details.
85    ///
86    /// # Returns
87    ///
88    /// A Result containing the response or an error.
89    ///
90    /// # Errors
91    ///
92    /// This function will return an error if the request to the Gevulot client fails.
93    pub async fn create(&mut self, msg: MsgCreateTask) -> Result<MsgCreateTaskResponse> {
94        let resp: MsgCreateTaskResponse = self
95            .base_client
96            .write()
97            .await
98            .send_msg_sync(msg, "")
99            .await?;
100        Ok(resp)
101    }
102
103    /// Deletes a task.
104    ///
105    /// # Arguments
106    ///
107    /// * `msg` - The message containing the task ID to delete.
108    ///
109    /// # Returns
110    ///
111    /// A Result containing the response or an error.
112    ///
113    /// # Errors
114    ///
115    /// This function will return an error if the request to the Gevulot client fails.
116    pub async fn delete(&mut self, msg: MsgDeleteTask) -> Result<MsgDeleteTaskResponse> {
117        let resp: MsgDeleteTaskResponse = self
118            .base_client
119            .write()
120            .await
121            .send_msg_sync(msg, "")
122            .await?;
123        Ok(resp)
124    }
125
126    /// Accepts a task.
127    ///
128    /// # Arguments
129    ///
130    /// * `msg` - The message containing the task ID to accept.
131    ///
132    /// # Returns
133    ///
134    /// A Result containing the response or an error.
135    ///
136    /// # Errors
137    ///
138    /// This function will return an error if the request to the Gevulot client fails.
139    pub async fn accept(&mut self, msg: MsgAcceptTask) -> Result<MsgAcceptTaskResponse> {
140        let resp: MsgAcceptTaskResponse = self
141            .base_client
142            .write()
143            .await
144            .send_msg_sync(msg, "")
145            .await?;
146        Ok(resp)
147    }
148
149    /// Declines a task.
150    ///
151    /// # Arguments
152    ///
153    /// * `msg` - The message containing the task ID to decline.
154    ///
155    /// # Returns
156    ///
157    /// A Result containing the response or an error.
158    ///
159    /// # Errors
160    ///
161    /// This function will return an error if the request to the Gevulot client fails.
162    pub async fn decline(&mut self, msg: MsgDeclineTask) -> Result<MsgDeclineTaskResponse> {
163        let resp: MsgDeclineTaskResponse = self
164            .base_client
165            .write()
166            .await
167            .send_msg_sync(msg, "")
168            .await?;
169        Ok(resp)
170    }
171
172    /// Finishes a task.
173    ///
174    /// # Arguments
175    ///
176    /// * `msg` - The message containing the task ID to finish.
177    ///
178    /// # Returns
179    ///
180    /// A Result containing the response or an error.
181    ///
182    /// # Errors
183    ///
184    /// This function will return an error if the request to the Gevulot client fails.
185    pub async fn finish(&mut self, msg: MsgFinishTask) -> Result<MsgFinishTaskResponse> {
186        let resp: MsgFinishTaskResponse = self
187            .base_client
188            .write()
189            .await
190            .send_msg_sync(msg, "")
191            .await?;
192        Ok(resp)
193    }
194
195    /// Reschedules a task.
196    ///
197    /// # Arguments
198    ///
199    /// * `msg` - The message containing the task ID to reschedule.
200    ///
201    /// # Returns
202    ///
203    /// A Result containing the response or an error.
204    pub async fn reschedule(
205        &mut self,
206        msg: MsgRescheduleTask,
207    ) -> Result<MsgRescheduleTaskResponse> {
208        let resp: MsgRescheduleTaskResponse = self
209            .base_client
210            .write()
211            .await
212            .send_msg_sync(msg, "")
213            .await?;
214        Ok(resp)
215    }
216}