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