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