open_lark/service/cloud_docs/bitable/v1/app_table_view/
patch.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    core::{
6        api_req::ApiRequest,
7        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
8        constants::AccessTokenType,
9        endpoints::cloud_docs::*,
10        http::Transport,
11        req_option::RequestOption,
12        SDKResult,
13    },
14    impl_executable_builder_owned,
15};
16
17use super::AppTableViewService;
18
19impl AppTableViewService {
20    /// 更新视图
21    pub async fn patch(
22        &self,
23        request: PatchViewRequest,
24        option: Option<RequestOption>,
25    ) -> SDKResult<BaseResponse<PatchViewResponse>> {
26        let mut api_req = request.api_request;
27        api_req.http_method = Method::PATCH;
28        api_req.api_path = BITABLE_V1_VIEW_PATCH
29            .replace("{app_token}", &request.app_token)
30            .replace("{table_id}", &request.table_id)
31            .replace("{view_id}", &request.view_id);
32        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
33        api_req.body = serde_json::to_vec(&PatchViewRequestBody {
34            view_name: request.view_name,
35            property: request.property,
36        })?;
37
38        let api_resp = Transport::request(api_req, &self.config, option).await?;
39        Ok(api_resp)
40    }
41}
42
43/// 更新视图请求
44#[derive(Debug, Default)]
45pub struct PatchViewRequest {
46    api_request: ApiRequest,
47    /// 多维表格的 app_token
48    app_token: String,
49    /// 数据表的 table_id
50    table_id: String,
51    /// 视图的 view_id
52    view_id: String,
53    /// 视图名称
54    view_name: Option<String>,
55    /// 视图的自定义属性
56    property: Option<serde_json::Value>,
57}
58
59impl PatchViewRequest {
60    pub fn builder() -> PatchViewRequestBuilder {
61        PatchViewRequestBuilder::default()
62    }
63
64    /// 创建更新视图请求
65    pub fn new(app_token: impl ToString, table_id: impl ToString, view_id: impl ToString) -> Self {
66        Self {
67            api_request: ApiRequest::default(),
68            app_token: app_token.to_string(),
69            table_id: table_id.to_string(),
70            view_id: view_id.to_string(),
71            view_name: None,
72            property: None,
73        }
74    }
75}
76
77#[derive(Default)]
78pub struct PatchViewRequestBuilder {
79    request: PatchViewRequest,
80}
81
82impl PatchViewRequestBuilder {
83    /// 多维表格的 app_token
84    pub fn app_token(mut self, app_token: impl ToString) -> Self {
85        self.request.app_token = app_token.to_string();
86        self
87    }
88
89    /// 数据表的 table_id
90    pub fn table_id(mut self, table_id: impl ToString) -> Self {
91        self.request.table_id = table_id.to_string();
92        self
93    }
94
95    /// 视图的 view_id
96    pub fn view_id(mut self, view_id: impl ToString) -> Self {
97        self.request.view_id = view_id.to_string();
98        self
99    }
100
101    /// 视图名称
102    pub fn view_name(mut self, view_name: impl ToString) -> Self {
103        self.request.view_name = Some(view_name.to_string());
104        self
105    }
106
107    /// 视图的自定义属性
108    pub fn property(mut self, property: serde_json::Value) -> Self {
109        self.request.property = Some(property);
110        self
111    }
112
113    pub fn build(self) -> PatchViewRequest {
114        self.request
115    }
116}
117
118impl_executable_builder_owned!(
119    PatchViewRequestBuilder,
120    AppTableViewService,
121    PatchViewRequest,
122    BaseResponse<PatchViewResponse>,
123    patch
124);
125
126#[derive(Serialize)]
127struct PatchViewRequestBody {
128    #[serde(skip_serializing_if = "Option::is_none")]
129    view_name: Option<String>,
130    #[serde(skip_serializing_if = "Option::is_none")]
131    property: Option<serde_json::Value>,
132}
133
134#[derive(Deserialize, Debug)]
135pub struct PatchViewResponse {
136    /// 视图名称
137    pub view_name: String,
138    /// 视图 ID
139    pub view_id: String,
140    /// 视图类型
141    pub view_type: String,
142}
143
144impl ApiResponseTrait for PatchViewResponse {
145    fn data_format() -> ResponseFormat {
146        ResponseFormat::Data
147    }
148}
149
150#[cfg(test)]
151#[allow(unused_variables, unused_unsafe)]
152mod tests {
153    use super::*;
154    use serde_json::json;
155
156    #[test]
157    fn test_patch_view_request() {
158        let request = PatchViewRequest::builder()
159            .app_token("bascnmBA*****yGehy8")
160            .table_id("tblsRc9GRRXKqhvW")
161            .view_id("vewTpR1urY")
162            .view_name("更新后的视图名称")
163            .property(json!({
164                "filter_info": {
165                    "conditions": [
166                        {
167                            "field_id": "fldxxxxxx",
168                            "operator": "is",
169                            "value": "完成"
170                        }
171                    ]
172                }
173            }))
174            .build();
175
176        assert_eq!(request.app_token, "bascnmBA*****yGehy8");
177        assert_eq!(request.table_id, "tblsRc9GRRXKqhvW");
178        assert_eq!(request.view_id, "vewTpR1urY");
179        assert_eq!(request.view_name, Some("更新后的视图名称".to_string()));
180        assert!(request.property.is_some());
181    }
182
183    #[test]
184    fn test_patch_view_request_new() {
185        let request =
186            PatchViewRequest::new("bascnmBA*****yGehy8", "tblsRc9GRRXKqhvW", "vewTpR1urY");
187
188        assert_eq!(request.app_token, "bascnmBA*****yGehy8");
189        assert_eq!(request.table_id, "tblsRc9GRRXKqhvW");
190        assert_eq!(request.view_id, "vewTpR1urY");
191        assert_eq!(request.view_name, None);
192        assert_eq!(request.property, None);
193    }
194
195    #[test]
196    fn test_patch_view_request_body_serialization() {
197        let body = PatchViewRequestBody {
198            view_name: Some("新视图名称".to_string()),
199            property: Some(json!({"key": "value"})),
200        };
201
202        let serialized = serde_json::to_value(&body).unwrap();
203        let expected = json!({
204            "view_name": "新视图名称",
205            "property": {"key": "value"}
206        });
207
208        assert_eq!(serialized, expected);
209    }
210}