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