open_lark/service/cloud_docs/sheets/v3/protect_range/
create.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 service::sheets::v3::SpreadsheetService,
15};
16
17impl SpreadsheetService {
18 pub async fn add_protect_range(
20 &self,
21 request: AddProtectRangeRequest,
22 option: Option<RequestOption>,
23 ) -> SDKResult<BaseResponse<AddProtectRangeResponseData>> {
24 let mut api_req = request.api_request;
25 api_req.http_method = Method::POST;
26 api_req.api_path = format!(
27 "/open-apis/sheets/v3/spreadsheets/{}/protect_range",
28 request.spreadsheet_token
29 );
30 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
31
32 let api_resp = Transport::request(api_req, &self.config, option).await?;
33
34 Ok(api_resp)
35 }
36}
37
38#[derive(Default, Debug, Serialize, Deserialize)]
40pub struct AddProtectRangeRequest {
41 #[serde(skip)]
42 api_request: ApiRequest,
43 spreadsheet_token: String,
45 protect_range: ProtectRangeData,
47}
48
49impl AddProtectRangeRequest {
50 pub fn builder() -> AddProtectRangeRequestBuilder {
51 AddProtectRangeRequestBuilder::default()
52 }
53}
54
55#[derive(Default)]
56pub struct AddProtectRangeRequestBuilder {
57 request: AddProtectRangeRequest,
58}
59
60impl AddProtectRangeRequestBuilder {
61 pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
62 self.request.spreadsheet_token = spreadsheet_token.to_string();
63 self
64 }
65
66 pub fn protect_range(mut self, protect_range: ProtectRangeData) -> Self {
67 self.request.protect_range = protect_range;
68 self
69 }
70
71 pub fn build(mut self) -> AddProtectRangeRequest {
72 self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
73 self.request
74 }
75}
76
77impl_executable_builder_owned!(
78 AddProtectRangeRequestBuilder,
79 SpreadsheetService,
80 AddProtectRangeRequest,
81 BaseResponse<AddProtectRangeResponseData>,
82 add_protect_range
83);
84
85#[derive(Default, Debug, Serialize, Deserialize)]
87pub struct ProtectRangeData {
88 pub dimension: String,
90 pub sheet_id: String,
92 pub start_index: i32,
94 pub end_index: i32,
96 #[serde(skip_serializing_if = "Option::is_none")]
98 pub protect_id: Option<String>,
99 #[serde(skip_serializing_if = "Option::is_none")]
101 pub lock_info: Option<String>,
102}
103
104impl ProtectRangeData {
105 pub fn new(
106 dimension: impl ToString,
107 sheet_id: impl ToString,
108 start_index: i32,
109 end_index: i32,
110 ) -> Self {
111 Self {
112 dimension: dimension.to_string(),
113 sheet_id: sheet_id.to_string(),
114 start_index,
115 end_index,
116 protect_id: None,
117 lock_info: None,
118 }
119 }
120
121 pub fn row_range(sheet_id: impl ToString, start_row: i32, end_row: i32) -> Self {
123 Self::new("ROWS", sheet_id, start_row, end_row)
124 }
125
126 pub fn column_range(sheet_id: impl ToString, start_col: i32, end_col: i32) -> Self {
128 Self::new("COLUMNS", sheet_id, start_col, end_col)
129 }
130}
131
132#[derive(Deserialize, Debug)]
134pub struct AddProtectRangeResponseData {
135 pub protect_id: String,
137 #[serde(flatten)]
139 pub protect_range: ProtectRangeData,
140}
141
142impl ApiResponseTrait for AddProtectRangeResponseData {
143 fn data_format() -> ResponseFormat {
144 ResponseFormat::Data
145 }
146}
147
148#[cfg(test)]
149mod test {
150 use super::*;
151 use serde_json::json;
152
153 #[test]
154 fn test_protect_range_data_creation() {
155 let protect_range = ProtectRangeData::row_range("Sheet1", 1, 10);
156 assert_eq!(protect_range.dimension, "ROWS");
157 assert_eq!(protect_range.sheet_id, "Sheet1");
158 assert_eq!(protect_range.start_index, 1);
159 assert_eq!(protect_range.end_index, 10);
160 }
161
162 #[test]
163 fn test_add_protect_range_response() {
164 let json = json!({
165 "protect_id": "protect_001",
166 "dimension": "COLUMNS",
167 "sheet_id": "Sheet1",
168 "start_index": 1,
169 "end_index": 5,
170 "lock_info": "user@example.com"
171 });
172
173 let response: AddProtectRangeResponseData = serde_json::from_value(json).unwrap();
174 assert_eq!(response.protect_id, "protect_001");
175 assert_eq!(response.protect_range.dimension, "COLUMNS");
176 }
177}