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 endpoints::cloud_docs::*,
10 http::Transport,
11 req_option::RequestOption,
12 SDKResult,
13 },
14 impl_executable_builder_owned,
15 service::sheets::v3::SpreadsheetService,
16};
17
18impl SpreadsheetService {
19 pub async fn add_protect_range(
21 &self,
22 request: AddProtectRangeRequest,
23 option: Option<RequestOption>,
24 ) -> SDKResult<BaseResponse<AddProtectRangeResponseData>> {
25 let mut api_req = request.api_request;
26 api_req.http_method = Method::POST;
27 api_req.api_path =
28 SHEETS_V3_SPREADSHEET_PROTECT_RANGE.replace("{}", &request.spreadsheet_token);
29 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
30
31 let api_resp = Transport::request(api_req, &self.config, option).await?;
32
33 Ok(api_resp)
34 }
35}
36
37#[derive(Default, Debug, Serialize, Deserialize)]
39pub struct AddProtectRangeRequest {
40 #[serde(skip)]
41 api_request: ApiRequest,
42 spreadsheet_token: String,
44 protect_range: ProtectRangeData,
46}
47
48impl AddProtectRangeRequest {
49 pub fn builder() -> AddProtectRangeRequestBuilder {
50 AddProtectRangeRequestBuilder::default()
51 }
52}
53
54#[derive(Default)]
55pub struct AddProtectRangeRequestBuilder {
56 request: AddProtectRangeRequest,
57}
58
59impl AddProtectRangeRequestBuilder {
60 pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
61 self.request.spreadsheet_token = spreadsheet_token.to_string();
62 self
63 }
64
65 pub fn protect_range(mut self, protect_range: ProtectRangeData) -> Self {
66 self.request.protect_range = protect_range;
67 self
68 }
69
70 pub fn build(mut self) -> AddProtectRangeRequest {
71 self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
72 self.request
73 }
74}
75
76impl_executable_builder_owned!(
77 AddProtectRangeRequestBuilder,
78 SpreadsheetService,
79 AddProtectRangeRequest,
80 BaseResponse<AddProtectRangeResponseData>,
81 add_protect_range
82);
83
84#[derive(Default, Debug, Serialize, Deserialize)]
86pub struct ProtectRangeData {
87 pub dimension: String,
89 pub sheet_id: String,
91 pub start_index: i32,
93 pub end_index: i32,
95 #[serde(skip_serializing_if = "Option::is_none")]
97 pub protect_id: Option<String>,
98 #[serde(skip_serializing_if = "Option::is_none")]
100 pub lock_info: Option<String>,
101}
102
103impl ProtectRangeData {
104 pub fn new(
105 dimension: impl ToString,
106 sheet_id: impl ToString,
107 start_index: i32,
108 end_index: i32,
109 ) -> Self {
110 Self {
111 dimension: dimension.to_string(),
112 sheet_id: sheet_id.to_string(),
113 start_index,
114 end_index,
115 protect_id: None,
116 lock_info: None,
117 }
118 }
119
120 pub fn row_range(sheet_id: impl ToString, start_row: i32, end_row: i32) -> Self {
122 Self::new("ROWS", sheet_id, start_row, end_row)
123 }
124
125 pub fn column_range(sheet_id: impl ToString, start_col: i32, end_col: i32) -> Self {
127 Self::new("COLUMNS", sheet_id, start_col, end_col)
128 }
129}
130
131#[derive(Deserialize, Debug)]
133pub struct AddProtectRangeResponseData {
134 pub protect_id: String,
136 #[serde(flatten)]
138 pub protect_range: ProtectRangeData,
139}
140
141impl ApiResponseTrait for AddProtectRangeResponseData {
142 fn data_format() -> ResponseFormat {
143 ResponseFormat::Data
144 }
145}
146
147#[cfg(test)]
148#[allow(unused_variables, unused_unsafe)]
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}