use std::borrow::Cow;
use crate::error::Error;
use crate::generated::routes;
use crate::generated::types::WorkflowStage;
use crate::http::Method;
use crate::observability::OperationInfo;
use crate::services::write_info;
pub use crate::generated::services::workflows::*;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct WorkflowSummary {
pub id: i64,
pub name: String,
pub account_name: String,
}
impl<'a> Workflows<'a> {
pub async fn list(&self, account_id: i64) -> Result<Vec<WorkflowSummary>, Error> {
let mut operation = self.client().request(
Method::GET,
format!("/autocompletable/accounts/{account_id}/workflows"),
);
operation
.info(OperationInfo {
service: Cow::Borrowed("Workflows"),
operation: Cow::Borrowed("ListWorkflows"),
resource_type: Cow::Borrowed("workflow"),
is_mutation: false,
resource_id: Some(account_id),
})
.without_json_suffix();
let rows: Vec<Vec<String>> = self.client().send(operation).await?;
Ok(rows.iter().filter_map(|row| summary(row)).collect())
}
pub async fn stages(&self, workflow_id: i64) -> Result<Vec<WorkflowStage>, Error> {
Ok(self.get(workflow_id).await?.stages.unwrap_or_default())
}
pub async fn create(&self, name: &str, account_id: Option<i64>) -> Result<(), Error> {
let account = account_id
.filter(|account_id| *account_id != 0)
.map(|account_id| account_id.to_string());
let mut fields = vec![("workflow[name]", name)];
if let Some(account) = &account {
fields.push(("account_id", account.as_str()));
}
let mut operation = self.client().form(Method::POST, "/workflows")?;
operation.info(write_info("Workflows", "CreateWorkflow", "workflow", None));
operation.form(&fields);
self.client().send_unit(operation).await
}
pub async fn update(&self, workflow_id: i64, name: &str) -> Result<(), Error> {
let mut operation = self
.client()
.form(Method::PATCH, &format!("/workflows/{workflow_id}"))?;
operation.info(write_info(
"Workflows",
"UpdateWorkflow",
"workflow",
Some(workflow_id),
));
operation.form(&[("workflow[name]", name)]);
self.client().send_unit(operation).await
}
pub async fn delete(&self, workflow_id: i64) -> Result<(), Error> {
let mut operation = self
.client()
.form(Method::DELETE, &format!("/workflows/{workflow_id}"))?;
operation.info(write_info(
"Workflows",
"DeleteWorkflow",
"workflow",
Some(workflow_id),
));
self.client().send_unit(operation).await
}
pub async fn create_stage(&self, workflow_id: i64) -> Result<(), Error> {
let mut operation = self
.client()
.form(Method::POST, &format!("/workflows/{workflow_id}/stages"))?;
operation.info(write_info(
"Workflows",
"CreateWorkflowStage",
"workflow_stage",
Some(workflow_id),
));
operation.form(&[]);
self.client().send_unit(operation).await
}
pub async fn update_stage(
&self,
workflow_id: i64,
stage_id: i64,
name: &str,
) -> Result<(), Error> {
let mut operation = self.client().form(
Method::PATCH,
&format!("/workflows/{workflow_id}/stages/{stage_id}"),
)?;
operation.info(write_info(
"Workflows",
"UpdateWorkflowStage",
"workflow_stage",
Some(stage_id),
));
operation.form(&[("workflow_stage[name]", name)]);
self.client().send_unit(operation).await
}
pub async fn delete_stage(&self, workflow_id: i64, stage_id: i64) -> Result<(), Error> {
let mut operation = self.client().form(
Method::DELETE,
&format!("/workflows/{workflow_id}/stages/{stage_id}"),
)?;
operation.info(write_info(
"Workflows",
"DeleteWorkflowStage",
"workflow_stage",
Some(stage_id),
));
self.client().send_unit(operation).await
}
pub async fn stage_topic(
&self,
topic_id: i64,
workflow_id: i64,
stage_id: i64,
) -> Result<(), Error> {
let mut operation = self
.client()
.operation(&routes::CREATE_WORKFLOW_STAGING, &[&topic_id, &workflow_id]);
operation
.info(write_info(
"Workflows",
"CreateWorkflowStaging",
"workflow_staging",
Some(topic_id),
))
.form_representation();
self.client().send_unit(operation).await?;
self.move_to_stage(topic_id, workflow_id, stage_id, None)
.await
}
pub async fn move_topic_to_stage(
&self,
topic_id: i64,
workflow_id: i64,
stage_id: i64,
) -> Result<(), Error> {
let info = write_info(
"Workflows",
"MoveWorkflowStaging",
"workflow_staging",
Some(topic_id),
);
self.move_to_stage(topic_id, workflow_id, stage_id, Some(info))
.await
}
pub async fn unstage_topic(&self, topic_id: i64, workflow_id: i64) -> Result<(), Error> {
let mut operation = self.client().form(
Method::DELETE,
&format!("/topics/{topic_id}/workflows/{workflow_id}/stagings"),
)?;
operation.info(write_info(
"Workflows",
"DeleteWorkflowStaging",
"workflow_staging",
Some(topic_id),
));
self.client().send_unit(operation).await
}
async fn move_to_stage(
&self,
topic_id: i64,
workflow_id: i64,
stage_id: i64,
info: Option<OperationInfo>,
) -> Result<(), Error> {
let stage = stage_id.to_string();
let mut operation = self
.client()
.operation(&routes::MOVE_WORKFLOW_STAGING, &[&topic_id, &workflow_id]);
operation
.form_representation()
.form(&[("workflow_staging[workflow_stage_id]", stage.as_str())]);
match info {
Some(info) => operation.info(info),
None => operation.quiet(),
};
self.client().send_unit(operation).await
}
}
fn summary(row: &[String]) -> Option<WorkflowSummary> {
match row {
[id, name, rest @ ..] => Some(WorkflowSummary {
id: id.parse().ok()?,
name: name.clone(),
account_name: rest.first().cloned().unwrap_or_default(),
}),
_ => None,
}
}