open_lark/service/cloud_docs/sheets/v3/protect_range/
get.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 service::sheets::v3::SpreadsheetService,
15};
16
17use super::create::ProtectRangeData;
18
19impl SpreadsheetService {
20 pub async fn get_protect_ranges(
22 &self,
23 request: GetProtectRangesRequest,
24 option: Option<RequestOption>,
25 ) -> SDKResult<BaseResponse<GetProtectRangesResponseData>> {
26 let mut api_req = request.api_request;
27 api_req.http_method = Method::GET;
28 api_req.api_path =
29 SHEETS_V3_SPREADSHEET_PROTECT_RANGE.replace("{}", &request.spreadsheet_token);
30 api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
31
32 if let Some(sheet_id) = &request.sheet_id {
34 api_req.query_params.insert("sheet_id", sheet_id.clone());
35 }
36
37 let api_resp = Transport::request(api_req, &self.config, option).await?;
38
39 Ok(api_resp)
40 }
41}
42
43#[derive(Default, Debug, Serialize, Deserialize)]
45pub struct GetProtectRangesRequest {
46 #[serde(skip)]
47 api_request: ApiRequest,
48 spreadsheet_token: String,
50 #[serde(skip_serializing_if = "Option::is_none")]
52 sheet_id: Option<String>,
53}
54
55impl GetProtectRangesRequest {
56 pub fn builder() -> GetProtectRangesRequestBuilder {
57 GetProtectRangesRequestBuilder::default()
58 }
59}
60
61#[derive(Default)]
62pub struct GetProtectRangesRequestBuilder {
63 request: GetProtectRangesRequest,
64}
65
66impl GetProtectRangesRequestBuilder {
67 pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
68 self.request.spreadsheet_token = spreadsheet_token.to_string();
69 self
70 }
71
72 pub fn sheet_id(mut self, sheet_id: impl ToString) -> Self {
73 self.request.sheet_id = Some(sheet_id.to_string());
74 self
75 }
76
77 pub fn build(mut self) -> GetProtectRangesRequest {
78 self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
79 self.request
80 }
81}
82
83#[derive(Deserialize, Debug)]
85pub struct ProtectRangeInfo {
86 pub protect_id: String,
88 #[serde(flatten)]
90 pub protect_range: ProtectRangeData,
91}
92
93#[derive(Deserialize, Debug)]
95pub struct GetProtectRangesResponseData {
96 pub items: Vec<ProtectRangeInfo>,
98 #[serde(default)]
100 pub has_more: bool,
101 #[serde(skip_serializing_if = "Option::is_none")]
103 pub page_token: Option<String>,
104}
105
106impl ApiResponseTrait for GetProtectRangesResponseData {
107 fn data_format() -> ResponseFormat {
108 ResponseFormat::Data
109 }
110}
111
112#[cfg(test)]
113#[allow(unused_variables, unused_unsafe)]
114mod test {
115 use super::*;
116 use serde_json::json;
117
118 #[test]
119 fn test_get_protect_ranges_response() {
120 let json = json!({
121 "items": [
122 {
123 "protect_id": "protect_001",
124 "dimension": "ROWS",
125 "sheet_id": "Sheet1",
126 "start_index": 1,
127 "end_index": 10,
128 "lock_info": "user1@example.com"
129 },
130 {
131 "protect_id": "protect_002",
132 "dimension": "COLUMNS",
133 "sheet_id": "Sheet1",
134 "start_index": 1,
135 "end_index": 5,
136 "lock_info": "user2@example.com"
137 }
138 ],
139 "has_more": false
140 });
141
142 let response: GetProtectRangesResponseData = serde_json::from_value(json).unwrap();
143 assert_eq!(response.items.len(), 2);
144 assert_eq!(response.items[0].protect_id, "protect_001");
145 assert_eq!(response.items[1].protect_range.dimension, "COLUMNS");
146 assert!(!response.has_more);
147 }
148}