open_lark/service/cloud_docs/sheets/v3/float_image/
get.rs

1use 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::SpreadsheetSheetService,
15};
16
17use super::create::FloatImageData;
18
19impl SpreadsheetSheetService {
20    /// 获取浮动图片
21    pub async fn get_float_image(
22        &self,
23        request: GetFloatImageRequest,
24        option: Option<RequestOption>,
25    ) -> SDKResult<BaseResponse<GetFloatImageResponseData>> {
26        let mut api_req = request.api_request;
27        api_req.http_method = Method::GET;
28        api_req.api_path = format!(
29            "/open-apis/sheets/v3/spreadsheets/{}/sheets/{}/float_images/{}",
30            request.spreadsheet_token, request.sheet_id, request.float_image_id
31        );
32        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
33
34        let api_resp = Transport::request(api_req, &self.config, option).await?;
35
36        Ok(api_resp)
37    }
38}
39
40/// 获取浮动图片请求
41#[derive(Default, Debug, Serialize, Deserialize)]
42pub struct GetFloatImageRequest {
43    #[serde(skip)]
44    api_request: ApiRequest,
45    /// spreadsheet 的 token
46    spreadsheet_token: String,
47    /// sheet 的 id
48    sheet_id: String,
49    /// 浮动图片 ID
50    float_image_id: String,
51}
52
53impl GetFloatImageRequest {
54    pub fn builder() -> GetFloatImageRequestBuilder {
55        GetFloatImageRequestBuilder::default()
56    }
57}
58
59#[derive(Default)]
60pub struct GetFloatImageRequestBuilder {
61    request: GetFloatImageRequest,
62}
63
64impl GetFloatImageRequestBuilder {
65    pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
66        self.request.spreadsheet_token = spreadsheet_token.to_string();
67        self
68    }
69
70    pub fn sheet_id(mut self, sheet_id: impl ToString) -> Self {
71        self.request.sheet_id = sheet_id.to_string();
72        self
73    }
74
75    pub fn float_image_id(mut self, float_image_id: impl ToString) -> Self {
76        self.request.float_image_id = float_image_id.to_string();
77        self
78    }
79
80    pub fn build(mut self) -> GetFloatImageRequest {
81        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
82        self.request
83    }
84}
85
86impl_executable_builder_owned!(
87    GetFloatImageRequestBuilder,
88    SpreadsheetSheetService,
89    GetFloatImageRequest,
90    BaseResponse<GetFloatImageResponseData>,
91    get_float_image
92);
93
94/// 获取浮动图片响应体最外层
95#[derive(Deserialize, Debug)]
96pub struct GetFloatImageResponseData {
97    /// 浮动图片信息
98    #[serde(flatten)]
99    pub float_image: FloatImageInfo,
100}
101
102/// 浮动图片信息
103#[derive(Deserialize, Debug)]
104pub struct FloatImageInfo {
105    /// 浮动图片 ID
106    pub float_image_id: String,
107    /// 浮动图片详细信息
108    #[serde(flatten)]
109    pub float_image: FloatImageData,
110}
111
112impl ApiResponseTrait for GetFloatImageResponseData {
113    fn data_format() -> ResponseFormat {
114        ResponseFormat::Data
115    }
116}
117
118#[cfg(test)]
119mod test {
120    use super::*;
121    use serde_json::json;
122
123    #[test]
124    fn test_get_float_image_response() {
125        let json = json!({
126            "float_image_id": "fimg_001",
127            "image_token": "img_token_123",
128            "position": {
129                "start_col_index": 1,
130                "start_row_index": 1,
131                "offset_x": 10.0,
132                "offset_y": 20.0
133            },
134            "size": {
135                "width": 200.0,
136                "height": 150.0
137            },
138            "name": "示例图片"
139        });
140
141        let response: GetFloatImageResponseData = serde_json::from_value(json).unwrap();
142        assert_eq!(response.float_image.float_image_id, "fimg_001");
143        assert_eq!(
144            response.float_image.float_image.image_token,
145            "img_token_123"
146        );
147    }
148}