gevulot_rs/workflow_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 MsgCreateWorkflow, MsgCreateWorkflowResponse, MsgDeleteWorkflow, MsgDeleteWorkflowResponse,
9 },
10};
11
12/// Client for managing workflows in the Gevulot system.
13#[derive(Debug, Clone)]
14pub struct WorkflowClient {
15 base_client: Arc<RwLock<BaseClient>>,
16}
17
18impl WorkflowClient {
19 /// Creates a new instance of WorkflowClient.
20 ///
21 /// # Arguments
22 ///
23 /// * `base_client` - An Arc-wrapped RwLock of the BaseClient.
24 ///
25 /// # Returns
26 ///
27 /// A new instance of WorkflowClient.
28 pub fn new(base_client: Arc<RwLock<BaseClient>>) -> Self {
29 Self { base_client }
30 }
31
32 /// Lists all workflows.
33 ///
34 /// # Returns
35 ///
36 /// A Result containing a vector of workflows or an error.
37 ///
38 /// # Errors
39 ///
40 /// This function will return an error if the request to the Gevulot client fails.
41 pub async fn list(&mut self) -> Result<Vec<crate::proto::gevulot::gevulot::Workflow>> {
42 let request = crate::proto::gevulot::gevulot::QueryAllWorkflowRequest { pagination: None };
43 let response = self
44 .base_client
45 .write()
46 .await
47 .gevulot_client
48 .workflow_all(request)
49 .await?;
50 Ok(response.into_inner().workflow)
51 }
52
53 /// Gets a workflow by its ID.
54 ///
55 /// # Arguments
56 ///
57 /// * `id` - The ID of the workflow to retrieve.
58 ///
59 /// # Returns
60 ///
61 /// A Result containing the workflow or an error.
62 ///
63 /// # Errors
64 ///
65 /// This function will return an error if the workflow is not found or if the request to the Gevulot client fails.
66 pub async fn get(&mut self, id: &str) -> Result<crate::proto::gevulot::gevulot::Workflow> {
67 let request = crate::proto::gevulot::gevulot::QueryGetWorkflowRequest { id: id.to_owned() };
68 let response = self
69 .base_client
70 .write()
71 .await
72 .gevulot_client
73 .workflow(request)
74 .await?;
75 response.into_inner().workflow.ok_or(Error::NotFound)
76 }
77
78 /// Creates a new workflow.
79 ///
80 /// # Arguments
81 ///
82 /// * `msg` - The message containing the workflow details.
83 ///
84 /// # Returns
85 ///
86 /// A Result containing the response or an error.
87 ///
88 /// # Errors
89 ///
90 /// This function will return an error if the request to the Gevulot client fails.
91 pub async fn create(&mut self, msg: MsgCreateWorkflow) -> Result<MsgCreateWorkflowResponse> {
92 let resp: MsgCreateWorkflowResponse = self
93 .base_client
94 .write()
95 .await
96 .send_msg_sync(msg, "")
97 .await?;
98 Ok(resp)
99 }
100
101 /// Deletes a workflow.
102 ///
103 /// # Arguments
104 ///
105 /// * `msg` - The message containing the workflow ID to delete.
106 ///
107 /// # Returns
108 ///
109 /// A Result containing the response or an error.
110 ///
111 /// # Errors
112 ///
113 /// This function will return an error if the request to the Gevulot client fails.
114 pub async fn delete(&mut self, msg: MsgDeleteWorkflow) -> Result<MsgDeleteWorkflowResponse> {
115 let resp: MsgDeleteWorkflowResponse = self
116 .base_client
117 .write()
118 .await
119 .send_msg_sync(msg, "")
120 .await?;
121 Ok(resp)
122 }
123}