open_lark/service/cloud_docs/bitable/v1/app_table/
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 endpoints::cloud_docs::*,
10 http::Transport,
11 req_option::RequestOption,
12 SDKResult,
13 },
14 impl_executable_builder_owned,
15};
16
17use super::AppTableService;
18
19impl AppTableService {
20 pub async fn patch(
22 &self,
23 request: PatchTableRequest,
24 option: Option<RequestOption>,
25 ) -> SDKResult<BaseResponse<PatchTableResponse>> {
26 let mut api_req = request.api_request;
27 api_req.http_method = Method::PATCH;
28 api_req.api_path = BITABLE_V1_TABLE_PATCH
29 .replace("{app_token}", &request.app_token)
30 .replace("{table_id}", &request.table_id);
31 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
32 api_req.body = serde_json::to_vec(&PatchTableRequestBody { name: request.name })?;
33
34 let api_resp = Transport::request(api_req, &self.config, option).await?;
35 Ok(api_resp)
36 }
37}
38
39#[derive(Debug, Default)]
41pub struct PatchTableRequest {
42 api_request: ApiRequest,
43 app_token: String,
45 table_id: String,
47 name: Option<String>,
49}
50
51impl PatchTableRequest {
52 pub fn builder() -> PatchTableRequestBuilder {
53 PatchTableRequestBuilder::default()
54 }
55
56 pub fn new(app_token: impl ToString, table_id: impl ToString) -> Self {
58 Self {
59 api_request: ApiRequest::default(),
60 app_token: app_token.to_string(),
61 table_id: table_id.to_string(),
62 name: None,
63 }
64 }
65}
66
67#[derive(Default)]
68pub struct PatchTableRequestBuilder {
69 request: PatchTableRequest,
70}
71
72impl PatchTableRequestBuilder {
73 pub fn app_token(mut self, app_token: impl ToString) -> Self {
75 self.request.app_token = app_token.to_string();
76 self
77 }
78
79 pub fn table_id(mut self, table_id: impl ToString) -> Self {
81 self.request.table_id = table_id.to_string();
82 self
83 }
84
85 pub fn name(mut self, name: impl ToString) -> Self {
87 self.request.name = Some(name.to_string());
88 self
89 }
90
91 pub fn build(self) -> PatchTableRequest {
92 self.request
93 }
94}
95
96impl_executable_builder_owned!(
97 PatchTableRequestBuilder,
98 AppTableService,
99 PatchTableRequest,
100 BaseResponse<PatchTableResponse>,
101 patch
102);
103
104#[derive(Serialize)]
105struct PatchTableRequestBody {
106 #[serde(skip_serializing_if = "Option::is_none")]
107 name: Option<String>,
108}
109
110#[derive(Deserialize, Debug)]
111pub struct PatchTableResponse {
112 pub name: String,
114}
115
116impl ApiResponseTrait for PatchTableResponse {
117 fn data_format() -> ResponseFormat {
118 ResponseFormat::Data
119 }
120}
121
122#[cfg(test)]
123#[allow(unused_variables, unused_unsafe)]
124mod tests {
125 use super::*;
126 use serde_json::json;
127
128 #[test]
129 fn test_patch_table_request() {
130 let request = PatchTableRequest::builder()
131 .app_token("bascnmBA*****yGehy8")
132 .table_id("tblsRc9GRRXKqhvW")
133 .name("更新后的表名")
134 .build();
135
136 assert_eq!(request.app_token, "bascnmBA*****yGehy8");
137 assert_eq!(request.table_id, "tblsRc9GRRXKqhvW");
138 assert_eq!(request.name, Some("更新后的表名".to_string()));
139 }
140
141 #[test]
142 fn test_patch_table_request_new() {
143 let request = PatchTableRequest::new("bascnmBA*****yGehy8", "tblsRc9GRRXKqhvW");
144 assert_eq!(request.app_token, "bascnmBA*****yGehy8");
145 assert_eq!(request.table_id, "tblsRc9GRRXKqhvW");
146 assert_eq!(request.name, None);
147 }
148
149 #[test]
150 fn test_patch_table_request_body_serialization() {
151 let body = PatchTableRequestBody {
152 name: Some("新表名".to_string()),
153 };
154
155 let serialized = serde_json::to_value(&body).unwrap();
156 let expected = json!({
157 "name": "新表名"
158 });
159
160 assert_eq!(serialized, expected);
161 }
162}