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 CreateFloatImageRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub float_image_id: Option<String>,
pub float_image_token: String,
pub range: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub width: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub offset_x: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub offset_y: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateFloatImageResponse {
pub float_image: FloatImage,
}
impl ApiResponseTrait for CreateFloatImageResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn create_float_image(
config: &Config,
spreadsheet_token: &str,
sheet_id: &str,
params: CreateFloatImageRequest,
) -> SDKResult<CreateFloatImageResponse> {
create_float_image_with_options(
config,
spreadsheet_token,
sheet_id,
params,
RequestOption::default(),
)
.await
}
pub async fn create_float_image_with_options(
config: &Config,
spreadsheet_token: &str,
sheet_id: &str,
params: CreateFloatImageRequest,
option: RequestOption,
) -> SDKResult<CreateFloatImageResponse> {
let api_endpoint =
SheetsApiV3::CreateFloatImage(spreadsheet_token.to_string(), sheet_id.to_string());
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, "创建浮动图片")
}
#[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");
}
}