use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
error::SDKResult,
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 = "snake_case")]
pub enum FormDisplayMode {
Traditional,
OneQuestionPerPage,
}
#[derive(Debug, Clone, Serialize)]
struct UpgradeFormRequestBody {
form_name: String,
display_mode: FormDisplayMode,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UpgradedForm {
pub id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UpgradeFormResponse {
pub form: UpgradedForm,
}
impl ApiResponseTrait for UpgradeFormResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone)]
pub struct UpgradeFormRequest {
config: Config,
app_token: String,
table_id: String,
form_id: String,
form_name: String,
display_mode: Option<FormDisplayMode>,
}
impl UpgradeFormRequest {
pub fn new(config: Config) -> Self {
Self {
config,
app_token: String::new(),
table_id: String::new(),
form_id: String::new(),
form_name: String::new(),
display_mode: None,
}
}
pub fn app_token(mut self, app_token: impl Into<String>) -> Self {
self.app_token = app_token.into();
self
}
pub fn table_id(mut self, table_id: impl Into<String>) -> Self {
self.table_id = table_id.into();
self
}
pub fn form_id(mut self, form_id: impl Into<String>) -> Self {
self.form_id = form_id.into();
self
}
pub fn form_name(mut self, form_name: impl Into<String>) -> Self {
self.form_name = form_name.into();
self
}
pub fn display_mode(mut self, display_mode: FormDisplayMode) -> Self {
self.display_mode = Some(display_mode);
self
}
pub async fn execute(self) -> SDKResult<UpgradeFormResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(
self,
option: RequestOption,
) -> SDKResult<UpgradeFormResponse> {
validate_required!(self.app_token.trim(), "app_token");
validate_required!(self.table_id.trim(), "table_id");
validate_required!(self.form_id.trim(), "form_id");
validate_required!(self.form_name.trim(), "form_name");
let display_mode = self.display_mode.ok_or_else(|| {
openlark_core::error::validation_error("display_mode", "display_mode 不能为空")
})?;
let api_endpoint = BitableApiV1::FormUpgrade(self.app_token, self.table_id, self.form_id);
let body = UpgradeFormRequestBody {
form_name: self.form_name,
display_mode,
};
let api_request: ApiRequest<UpgradeFormResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serde_json::to_vec(&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 openlark_core::testing::prelude::test_runtime;
#[test]
fn test_missing_display_mode() {
let config = Config::default();
let request = UpgradeFormRequest::new(config)
.app_token("app_token")
.table_id("table_id")
.form_id("form_id")
.form_name("表单");
let rt = test_runtime();
let result = rt.block_on(request.execute());
assert!(result.is_err());
}
#[test]
fn test_response_trait() {
assert_eq!(UpgradeFormResponse::data_format(), ResponseFormat::Data);
}
}