reactive_graph_client/client/instances/flows/
api.rs1use std::sync::Arc;
2
3use crate::client::ReactiveGraphClient;
4use crate::client::ReactiveGraphClientExecutionError;
5use crate::client::instances::flows::mutations::create_from_type::mutations::create_flow_instance_from_type_mutation;
6use crate::client::instances::flows::mutations::delete::mutations::delete_flow_instance_mutation;
7use crate::client::instances::flows::queries::get_by_id::queries::get_flow_instance_by_id;
8use crate::client::instances::flows::queries::get_by_label::queries::get_flow_instance_by_label;
9use crate::client::instances::flows::queries::search::queries::search;
10use crate::client::instances::flows::variables::search::variables::SearchFlowInstancesVariables;
11use cynic::http::ReqwestExt;
12use reactive_graph_graph::FlowInstance;
13use reactive_graph_graph::FlowTypeId;
14use reactive_graph_graph::PropertyInstances;
15use uuid::Uuid;
16
17pub struct FlowInstances {
18 client: Arc<ReactiveGraphClient>,
19}
20
21impl FlowInstances {
22 pub fn new(client: Arc<ReactiveGraphClient>) -> Self {
23 Self { client }
24 }
25
26 pub async fn search(&self, search_query: SearchFlowInstancesVariables) -> Result<Option<Vec<FlowInstance>>, ReactiveGraphClientExecutionError> {
27 let flow_instances = self
28 .client
29 .client
30 .post(self.client.url_reactive_graph())
31 .run_graphql(search(search_query))
32 .await
33 .map_err(ReactiveGraphClientExecutionError::FailedToSendRequest)?
34 .data
35 .map(|data| crate::schema_graphql::instances::flow_instance::FlowInstances(data.instances.flows))
36 .map(From::from);
37 Ok(flow_instances)
38 }
39
40 pub async fn get_by_id<ID: Into<Uuid>>(&self, id: ID) -> Result<Option<FlowInstance>, ReactiveGraphClientExecutionError> {
41 let id = id.into();
42 let flow_instance = self
43 .client
44 .client
45 .post(self.client.url_reactive_graph())
46 .run_graphql(get_flow_instance_by_id(id))
47 .await
48 .map_err(ReactiveGraphClientExecutionError::FailedToSendRequest)?
49 .data
50 .and_then(|data| data.instances.flows.first().cloned())
51 .map(From::from);
52 Ok(flow_instance)
53 }
54
55 pub async fn get_by_label<L: Into<String>>(&self, label: L) -> Result<Option<FlowInstance>, ReactiveGraphClientExecutionError> {
56 let label = label.into();
57 let flow_instance = self
58 .client
59 .client
60 .post(self.client.url_reactive_graph())
61 .run_graphql(get_flow_instance_by_label(label))
62 .await
63 .map_err(ReactiveGraphClientExecutionError::FailedToSendRequest)?
64 .data
65 .and_then(|data| data.instances.flows.first().cloned())
66 .map(From::from);
67 Ok(flow_instance)
68 }
69
70 pub async fn create_from_type<TY: Into<FlowTypeId>, ID: Into<Uuid>>(
73 &self,
74 ty: TY,
75 id: Option<ID>,
76 variables: PropertyInstances,
77 properties: PropertyInstances,
78 ) -> Result<Option<FlowInstance>, ReactiveGraphClientExecutionError> {
79 let ty = ty.into();
80 let id = id.map(|id| id.into());
81 let flow_instance = self
82 .client
83 .client
84 .post(self.client.url_reactive_graph())
85 .run_graphql(create_flow_instance_from_type_mutation(ty, id, variables, properties))
86 .await
87 .map_err(ReactiveGraphClientExecutionError::FailedToSendRequest)?
88 .data
89 .map(|data| data.instances.flows.create_from_type)
90 .map(From::from);
91 Ok(flow_instance)
92 }
93
94 pub async fn delete<ID: Into<Uuid>>(&self, id: ID) -> Result<Option<bool>, ReactiveGraphClientExecutionError> {
95 let id = id.into();
96 let flow_instance = self
97 .client
98 .client
99 .post(self.client.url_reactive_graph())
100 .run_graphql(delete_flow_instance_mutation(id))
101 .await
102 .map_err(ReactiveGraphClientExecutionError::FailedToSendRequest)?
103 .data
104 .map(|data| data.instances.flows.delete);
105 Ok(flow_instance)
106 }
107}