open_lark/service/cloud_docs/sheets/v3/spreadsheet/
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 create(
23 &self,
24 request: CreateSpreedSheetRequest,
25 option: Option<RequestOption>,
26 ) -> SDKResult<BaseResponse<CreateSpreedSheetResponseData>> {
27 let mut api_req = request.api_request;
28 api_req.http_method = Method::POST;
29 api_req.api_path = SHEETS_V3_SPREADSHEETS.to_string();
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 CreateSpreedSheetRequest {
41 #[serde(skip)]
42 api_request: ApiRequest,
43 title: Option<String>,
51 folder_token: Option<String>,
53}
54
55impl CreateSpreedSheetRequest {
56 pub fn builder() -> CreateSpreedSheetRequestBuilder {
57 CreateSpreedSheetRequestBuilder::default()
58 }
59}
60
61#[derive(Default)]
62pub struct CreateSpreedSheetRequestBuilder {
63 request: CreateSpreedSheetRequest,
64}
65
66impl CreateSpreedSheetRequestBuilder {
67 pub fn title(mut self, title: impl ToString) -> Self {
75 self.request.title = Some(title.to_string());
76 self
77 }
78
79 pub fn folder_token(mut self, folder_token: impl ToString) -> Self {
81 self.request.folder_token = Some(folder_token.to_string());
82 self
83 }
84
85 pub fn build(mut self) -> CreateSpreedSheetRequest {
86 self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
87 self.request
88 }
89}
90
91impl_executable_builder_owned!(
93 CreateSpreedSheetRequestBuilder,
94 SpreadsheetService,
95 CreateSpreedSheetRequest,
96 BaseResponse<CreateSpreedSheetResponseData>,
97 create
98);
99
100#[derive(Deserialize, Debug)]
102pub struct CreateSpreedSheetResponseData {
103 pub spreadsheet: CreateSpreedSheetResponse,
104}
105
106impl ApiResponseTrait for CreateSpreedSheetResponseData {
107 fn data_format() -> ResponseFormat {
108 ResponseFormat::Data
109 }
110}
111
112#[derive(Deserialize, Debug)]
114pub struct CreateSpreedSheetResponse {
115 pub title: String,
117 pub folder_token: String,
119 pub url: String,
121 pub spreadsheet_token: String,
123}
124
125#[cfg(test)]
126#[allow(unused_variables, unused_unsafe)]
127mod test {
128 use serde_json::json;
129
130 use crate::service::sheets::v3::spreadsheet::CreateSpreedSheetResponseData;
131
132 #[test]
133 fn test_create_spreadsheet_response() {
134 let json = json!({
135 "spreadsheet": {
136 "title": "title",
137 "folder_token": "fldcnMsNb*****hIW9IjG1LVswg",
138 "url": "https://example.feishu.cn/sheets/shtcnmBA*****yGehy8",
139 "spreadsheet_token": "shtcnmBA*****yGehy8"
140 }
141
142 });
143
144 let response: CreateSpreedSheetResponseData = serde_json::from_value(json).unwrap();
145 let response = response.spreadsheet;
146
147 assert_eq!(response.title, "title");
148 assert_eq!(response.folder_token, "fldcnMsNb*****hIW9IjG1LVswg");
149 assert_eq!(
150 response.url,
151 "https://example.feishu.cn/sheets/shtcnmBA*****yGehy8"
152 );
153 assert_eq!(response.spreadsheet_token, "shtcnmBA*****yGehy8");
154 }
155}