use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
};
use serde::{Deserialize, Serialize};
use super::FloatImage;
use crate::common::{api_endpoints::SheetsApiV3, api_utils::*};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryFloatImagesResponse {
pub items: Vec<FloatImage>,
}
impl ApiResponseTrait for QueryFloatImagesResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn query_float_images(
config: &Config,
spreadsheet_token: &str,
sheet_id: &str,
) -> SDKResult<QueryFloatImagesResponse> {
query_float_images_with_options(
config,
spreadsheet_token,
sheet_id,
RequestOption::default(),
)
.await
}
pub async fn query_float_images_with_options(
config: &Config,
spreadsheet_token: &str,
sheet_id: &str,
option: RequestOption,
) -> SDKResult<QueryFloatImagesResponse> {
let api_endpoint =
SheetsApiV3::QueryFloatImages(spreadsheet_token.to_string(), sheet_id.to_string());
let api_request: ApiRequest<QueryFloatImagesResponse> = api_endpoint.to_request();
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "查询浮动图片")
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_query_float_images_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/open-apis/sheets/v3/spreadsheets/tokenAbc/sheets/sheetId001/float_images/query"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0,
"msg": "success",
"data": {
"items": [
{ "float_image_id": "fi001", "float_image_token": "tok001", "range": "A1", "width": 100, "height": 50, "offset_x": 0, "offset_y": 0 },
{ "float_image_id": "fi002", "float_image_token": "tok002", "range": "B1", "width": 200, "height": 100, "offset_x": 0, "offset_y": 0 }
]
}
})))
.mount(&server)
.await;
let config = Config::builder()
.app_id("ci_app_id")
.app_secret("ci_app_secret")
.base_url(server.uri())
.enable_token_cache(false)
.build();
let resp = query_float_images(&config, "tokenAbc", "sheetId001")
.await
.expect("查询浮动图片应成功");
assert_eq!(resp.items.len(), 2);
assert_eq!(resp.items[0].float_image_id, "fi001");
assert_eq!(resp.items[1].range, "B1");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/sheets/v3/spreadsheets/tokenAbc/sheets/sheetId001/float_images/query"
);
}
}