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> =
ApiRequest::post(&api_endpoint.to_url()).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> =
ApiRequest::post(&api_endpoint.to_url()).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> =
ApiRequest::post(&api_endpoint.to_url()).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> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(¶ms, "删除浮图")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "删除浮图")
}
#[cfg(test)]
mod tests {
use serde_json;
#[test]
fn test_serialization_roundtrip() {
let json = r#"{"test": "value"}"#;
assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
}
#[test]
fn test_deserialization_from_json() {
let json = r#"{"field": "data"}"#;
let value: serde_json::Value = serde_json::from_str(json).expect("JSON 反序列化失败");
assert_eq!(value["field"], "data");
}
}