use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required,
};
use serde::{Deserialize, Serialize};
use crate::common::api_endpoints::BitableApiV1;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "PascalCase")]
pub enum WorkflowStatus {
Enable,
Disable,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateWorkflowBody {
pub status: WorkflowStatus,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UpdateWorkflowResponse {}
impl ApiResponseTrait for UpdateWorkflowResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub struct UpdateWorkflowRequest {
config: Config,
app_token: String,
workflow_id: String,
status: WorkflowStatus,
}
impl UpdateWorkflowRequest {
pub fn new(config: Config) -> Self {
Self {
config,
app_token: String::new(),
workflow_id: String::new(),
status: WorkflowStatus::Enable,
}
}
pub fn app_token(mut self, app_token: impl Into<String>) -> Self {
self.app_token = app_token.into();
self
}
pub fn workflow_id(mut self, workflow_id: impl Into<String>) -> Self {
self.workflow_id = workflow_id.into();
self
}
pub fn status(mut self, status: WorkflowStatus) -> Self {
self.status = status;
self
}
pub async fn execute(self) -> SDKResult<UpdateWorkflowResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(
self,
option: RequestOption,
) -> SDKResult<UpdateWorkflowResponse> {
validate_required!(self.app_token, "app_token 不能为空");
validate_required!(self.workflow_id, "workflow_id 不能为空");
let api_endpoint = BitableApiV1::WorkflowUpdate(self.app_token, self.workflow_id);
let body = serde_json::to_value(&UpdateWorkflowBody {
status: self.status,
})
.map_err(|e| {
openlark_core::error::serialization_error("序列化更新自动化流程请求体失败", Some(e))
})?;
let api_request: ApiRequest<UpdateWorkflowResponse> = api_endpoint.to_request().body(body);
let response = Transport::request(api_request, &self.config, Some(option)).await?;
response
.data
.ok_or_else(|| openlark_core::error::validation_error("response", "响应数据为空"))
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_update_workflow_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("PUT"))
.and(path("/open-apis/bitable/v1/apps/app001/workflows/wf001"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success", "data": {}
})))
.mount(&server)
.await;
let config = Config::builder()
.app_id("ci_app_id")
.app_secret("ci_app_secret")
.base_url(server.uri())
.enable_token_cache(false)
.build();
UpdateWorkflowRequest::new(config)
.app_token("app001")
.workflow_id("wf001")
.execute()
.await
.expect("更新工作流应成功");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/bitable/v1/apps/app001/workflows/wf001"
);
}
}