use crate::common::api_endpoints::BitableApiV1;
use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
error::SDKResult,
http::Transport,
req_option::RequestOption,
validate_required,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct View {
pub view_id: String,
pub view_name: String,
pub view_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub view_public_level: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub view_private_owner_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub property: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_time: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub modified_time: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct PatchViewData {
#[serde(skip_serializing_if = "Option::is_none")]
pub view_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub property: Option<Value>,
}
impl PatchViewData {
pub fn new(view_name: impl Into<String>) -> Self {
Self {
view_name: Some(view_name.into()),
property: None,
}
}
pub fn with_property(mut self, property: Value) -> Self {
self.property = Some(property);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PatchViewResponse {
pub view: View,
}
impl ApiResponseTrait for PatchViewResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone)]
pub struct PatchViewRequest {
config: Config,
app_token: String,
table_id: String,
view_id: String,
payload: PatchViewData,
}
impl PatchViewRequest {
pub fn new(config: Config) -> Self {
Self {
config,
app_token: String::new(),
table_id: String::new(),
view_id: String::new(),
payload: PatchViewData::default(),
}
}
pub fn app_token(mut self, app_token: String) -> Self {
self.app_token = app_token;
self
}
pub fn table_id(mut self, table_id: String) -> Self {
self.table_id = table_id;
self
}
pub fn view_id(mut self, view_id: String) -> Self {
self.view_id = view_id;
self
}
pub fn payload(mut self, payload: PatchViewData) -> Self {
self.payload = payload;
self
}
pub async fn execute(self) -> SDKResult<PatchViewResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(self, option: RequestOption) -> SDKResult<PatchViewResponse> {
validate_required!(self.app_token.trim(), "app_token");
validate_required!(self.table_id.trim(), "table_id");
validate_required!(self.view_id.trim(), "view_id");
if self.payload.view_name.is_none() && self.payload.property.is_none() {
return Err(openlark_core::error::validation_error(
"payload",
"至少需要提供一个更新字段(view_name 或 property)",
));
}
if let Some(ref view_name) = self.payload.view_name {
if view_name.trim().is_empty() {
return Err(openlark_core::error::validation_error(
"view_name",
"视图名称不能为空",
));
}
if view_name.len() > 100 {
return Err(openlark_core::error::validation_error(
"view_name",
"视图名称长度不能超过100个字符",
));
}
if view_name.contains('[') || view_name.contains(']') {
return Err(openlark_core::error::validation_error(
"view_name",
"视图名称不能包含 '[' 或 ']'",
));
}
}
let api_endpoint = BitableApiV1::ViewPatch(self.app_token, self.table_id, self.view_id);
let api_request: ApiRequest<PatchViewResponse> =
ApiRequest::patch(&api_endpoint.to_url()).body(serde_json::to_vec(&self.payload)?);
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 serde_json;
#[test]
fn test_serialization_roundtrip() {
let json = r#"{"test": "value"}"#;
assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
}
#[test]
fn test_deserialization_from_json() {
let json = r#"{"field": "data"}"#;
let value: serde_json::Value = serde_json::from_str(json).expect("JSON 反序列化失败");
assert_eq!(value["field"], "data");
}
}