use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::cloud_docs::*,
http::Transport,
req_option::RequestOption,
SDKResult,
},
impl_executable_builder_config,
};
#[derive(Debug, Serialize, Default)]
pub struct UpdateWorkflowRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
app_token: String,
#[serde(skip)]
workflow_id: String,
is_enabled: bool,
}
impl UpdateWorkflowRequest {
pub fn builder() -> UpdateWorkflowRequestBuilder {
UpdateWorkflowRequestBuilder::default()
}
pub fn new(app_token: impl ToString, workflow_id: impl ToString, is_enabled: bool) -> Self {
Self {
app_token: app_token.to_string(),
workflow_id: workflow_id.to_string(),
is_enabled,
..Default::default()
}
}
}
#[derive(Default)]
pub struct UpdateWorkflowRequestBuilder {
request: UpdateWorkflowRequest,
}
impl UpdateWorkflowRequestBuilder {
pub fn app_token(mut self, app_token: impl ToString) -> Self {
self.request.app_token = app_token.to_string();
self
}
pub fn workflow_id(mut self, workflow_id: impl ToString) -> Self {
self.request.workflow_id = workflow_id.to_string();
self
}
pub fn set_enabled(mut self, is_enabled: bool) -> Self {
self.request.is_enabled = is_enabled;
self
}
pub fn enable(mut self) -> Self {
self.request.is_enabled = true;
self
}
pub fn disable(mut self) -> Self {
self.request.is_enabled = false;
self
}
pub fn build(mut self) -> UpdateWorkflowRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
impl_executable_builder_config!(
UpdateWorkflowRequestBuilder,
UpdateWorkflowRequest,
BaseResponse<UpdateWorkflowResponse>,
update_workflow
);
#[derive(Debug, Deserialize)]
pub struct UpdateWorkflowResponse {
pub workflow_id: String,
pub is_enabled: i32,
pub updated_time: i64,
}
impl ApiResponseTrait for UpdateWorkflowResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn update_workflow(
request: UpdateWorkflowRequest,
config: &Config,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<UpdateWorkflowResponse>> {
let mut api_req = request.api_request;
api_req.http_method = Method::PATCH;
api_req.api_path = BITABLE_V1_WORKFLOW_UPDATE
.replace("{app_token}", &request.app_token)
.replace("{workflow_id}", &request.workflow_id);
api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
let api_resp = Transport::request(api_req, config, option).await?;
Ok(api_resp)
}
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
use super::*;
#[test]
fn test_update_workflow_request_builder() {
let request = UpdateWorkflowRequest::builder()
.app_token("bascnmBA*****yGehy8")
.workflow_id("wkfxxxxxx")
.enable()
.build();
assert_eq!(request.app_token, "bascnmBA*****yGehy8");
assert_eq!(request.workflow_id, "wkfxxxxxx");
assert!(request.is_enabled);
}
#[test]
fn test_update_workflow_request_disable() {
let request = UpdateWorkflowRequest::builder()
.app_token("bascnmBA*****yGehy8")
.workflow_id("wkfxxxxxx")
.disable()
.build();
assert!(!request.is_enabled);
}
}