use crate::clients::{api::api_client::ApiClient, error::EngineError};
use super::process_definition::{
PersistProcessDefinitionPayload, ProcessDefinition, ProcessDefinitionList,
};
const PROCESS_DEFINITIONS_ENDPOINT: &str = "/process_definitions";
#[derive(Clone)]
pub struct ProcessDefinitionClient {
api_client: ApiClient,
pub process_definitions_url: String,
}
impl ProcessDefinitionClient {
pub fn new(api_client: ApiClient) -> ProcessDefinitionClient {
let process_definitions_url = format!(
"{}{}{}",
api_client.get_engine_url(),
api_client.get_engine_api_endpoint(),
PROCESS_DEFINITIONS_ENDPOINT
);
ProcessDefinitionClient {
api_client,
process_definitions_url,
}
}
pub async fn get_process_definitions(
&self,
offset: Option<u32>,
limit: Option<u32>,
) -> Result<ProcessDefinitionList, EngineError> {
let mut query_params = Vec::new();
if let Some(offset) = offset {
query_params.push(format!("offset={}", offset));
}
if let Some(limit) = limit {
query_params.push(format!("limit={}", limit));
}
let url = match query_params.is_empty() {
true => self.process_definitions_url.clone(),
false => format!(
"{}?{}",
self.process_definitions_url,
query_params.join("&")
),
};
self.api_client.get::<ProcessDefinitionList>(&url).await
}
pub async fn get_process_definition_by_id(
&self,
process_definition_id: &str,
) -> Result<ProcessDefinition, EngineError> {
let url = format!("{}/{}", self.process_definitions_url, process_definition_id);
self.api_client.get::<ProcessDefinition>(&url).await
}
pub async fn upload_process_definition(
&self,
request: PersistProcessDefinitionPayload,
) -> Result<(), EngineError> {
let request_json = serde_json::to_value(request).expect("Failed to serialize request");
self.api_client
.post::<()>(&self.process_definitions_url, Some(&request_json))
.await
}
pub async fn delete_process_definition_by_id(
&self,
process_definition_id: &str,
delete_all_related_data: Option<bool>,
) -> Result<(), EngineError> {
let url = format!(
"{}/{}?deleteAllRelatedData={}",
self.process_definitions_url,
process_definition_id,
delete_all_related_data.unwrap_or(false)
);
self.api_client.delete::<()>(&url).await
}
}