use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required,
};
use crate::common::{api_endpoints::CcmSheetApiOld, api_utils::*};
#[derive(Debug, Clone)]
pub struct FloatImageApi {
config: Config,
}
impl FloatImageApi {
pub fn new(config: Config) -> Self {
Self { config }
}
pub fn config(&self) -> &Config {
&self.config
}
}
pub mod models;
pub use models::{
CreateFloatImageParams, CreateFloatImageResponse, DeleteFloatImageParams,
DeleteFloatImageResponse, FloatImageInfo, FloatImagePosition, FloatImageResult,
GetFloatImageParams, GetFloatImageResponse, UpdateFloatImageParams, UpdateFloatImageResponse,
};
impl ApiResponseTrait for CreateFloatImageResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for GetFloatImageResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for UpdateFloatImageResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for DeleteFloatImageResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn create_float_image(
config: &Config,
spreadsheet_token: &str,
params: CreateFloatImageParams,
) -> SDKResult<CreateFloatImageResponse> {
create_float_image_with_options(config, spreadsheet_token, params, RequestOption::default())
.await
}
pub async fn create_float_image_with_options(
config: &Config,
spreadsheet_token: &str,
params: CreateFloatImageParams,
option: RequestOption,
) -> SDKResult<CreateFloatImageResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.image_token.trim(), "图片Token不能为空");
validate_required!(params.sheet_id.trim(), "工作表ID不能为空");
let api_endpoint =
CcmSheetApiOld::CreateFloatImage(spreadsheet_token.to_string(), params.sheet_id.clone());
let api_request: ApiRequest<CreateFloatImageResponse> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "创建浮图")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "创建浮图")
}
pub async fn get_float_image(
config: &Config,
spreadsheet_token: &str,
params: GetFloatImageParams,
) -> SDKResult<GetFloatImageResponse> {
get_float_image_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn get_float_image_with_options(
config: &Config,
spreadsheet_token: &str,
params: GetFloatImageParams,
option: RequestOption,
) -> SDKResult<GetFloatImageResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.float_image_id.trim(), "浮图ID不能为空");
validate_required!(params.sheet_id.trim(), "工作表ID不能为空");
validate_required!(params.sheet_id.trim(), "工作表ID不能为空");
let api_endpoint = CcmSheetApiOld::GetFloatImage(
spreadsheet_token.to_string(),
params.sheet_id.clone(),
params.float_image_id.clone(),
);
let api_request: ApiRequest<GetFloatImageResponse> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "获取浮图")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "获取浮图")
}
pub async fn update_float_image(
config: &Config,
spreadsheet_token: &str,
params: UpdateFloatImageParams,
) -> SDKResult<UpdateFloatImageResponse> {
update_float_image_with_options(config, spreadsheet_token, params, RequestOption::default())
.await
}
pub async fn update_float_image_with_options(
config: &Config,
spreadsheet_token: &str,
params: UpdateFloatImageParams,
option: RequestOption,
) -> SDKResult<UpdateFloatImageResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.float_image_id.trim(), "浮图ID不能为空");
validate_required!(params.sheet_id.trim(), "工作表ID不能为空");
let api_endpoint = CcmSheetApiOld::UpdateFloatImage(
spreadsheet_token.to_string(),
params.sheet_id.clone(),
params.float_image_id.clone(),
);
let api_request: ApiRequest<UpdateFloatImageResponse> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "更新浮图")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "更新浮图")
}
pub async fn delete_float_image(
config: &Config,
spreadsheet_token: &str,
params: DeleteFloatImageParams,
) -> SDKResult<DeleteFloatImageResponse> {
delete_float_image_with_options(config, spreadsheet_token, params, RequestOption::default())
.await
}
pub async fn delete_float_image_with_options(
config: &Config,
spreadsheet_token: &str,
params: DeleteFloatImageParams,
option: RequestOption,
) -> SDKResult<DeleteFloatImageResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.float_image_id.trim(), "浮图ID不能为空");
validate_required!(params.sheet_id.trim(), "工作表ID不能为空");
let api_endpoint = CcmSheetApiOld::DeleteFloatImage(
spreadsheet_token.to_string(),
params.sheet_id.clone(),
params.float_image_id.clone(),
);
let api_request: ApiRequest<DeleteFloatImageResponse> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "删除浮图")?);
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_create_float_image_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path(
"/open-apis/sheets/v3/spreadsheets/token001/sheets/sid001/float_images",
))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success", "data": {}
})))
.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 = create_float_image(
&config,
"token001",
CreateFloatImageParams {
image_token: "img001".into(),
sheet_id: "sid001".into(),
float_image: FloatImagePosition {
offset_x: 0,
offset_y: 0,
width: 100,
height: 50,
scale: 1.0,
z_index: 0,
},
},
)
.await
.expect("创建浮图应成功");
assert!(resp.data.is_none());
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/token001/sheets/sid001/float_images"
);
}
}