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 SpreadsheetApi {
config: Config,
}
impl SpreadsheetApi {
pub fn new(config: Config) -> Self {
Self { config }
}
pub fn config(&self) -> &Config {
&self.config
}
}
pub mod models;
pub use models::{
CreateSpreadsheetParams, CreateSpreadsheetResponse, CreateSpreadsheetResult,
GetSpreadsheetParams, GetSpreadsheetResponse, SpreadsheetInfo, SpreadsheetSheetInfo,
UpdateSpreadsheetParams, UpdateSpreadsheetResponse, UpdateSpreadsheetResult, UserInfo,
};
impl ApiResponseTrait for GetSpreadsheetResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for CreateSpreadsheetResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for UpdateSpreadsheetResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn get_spreadsheet(
config: &Config,
spreadsheet_token: &str,
params: GetSpreadsheetParams,
) -> SDKResult<GetSpreadsheetResponse> {
get_spreadsheet_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn get_spreadsheet_with_options(
config: &Config,
spreadsheet_token: &str,
params: GetSpreadsheetParams,
option: RequestOption,
) -> SDKResult<GetSpreadsheetResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
let api_endpoint = CcmSheetApiOld::GetSpreadsheet(spreadsheet_token.to_string());
let api_request: ApiRequest<GetSpreadsheetResponse> = api_endpoint
.to_request()
.query_opt("include_sheet", params.include_sheet.map(|v| v.to_string()));
Transport::request_typed(api_request, config, Some(option), "获取表格信息").await
}
pub async fn create_spreadsheet(
config: &Config,
params: CreateSpreadsheetParams,
) -> SDKResult<CreateSpreadsheetResponse> {
create_spreadsheet_with_options(config, params, RequestOption::default()).await
}
pub async fn create_spreadsheet_with_options(
config: &Config,
params: CreateSpreadsheetParams,
option: RequestOption,
) -> SDKResult<CreateSpreadsheetResponse> {
validate_required!(params.title.trim(), "表格标题不能为空");
let api_endpoint = CcmSheetApiOld::CreateSpreadsheet;
let api_request: ApiRequest<CreateSpreadsheetResponse> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "创建表格")?);
Transport::request_typed(api_request, config, Some(option), "创建表格").await
}
pub async fn update_spreadsheet(
config: &Config,
spreadsheet_token: &str,
params: UpdateSpreadsheetParams,
) -> SDKResult<UpdateSpreadsheetResponse> {
update_spreadsheet_with_options(config, spreadsheet_token, params, RequestOption::default())
.await
}
pub async fn update_spreadsheet_with_options(
config: &Config,
spreadsheet_token: &str,
params: UpdateSpreadsheetParams,
option: RequestOption,
) -> SDKResult<UpdateSpreadsheetResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
let api_endpoint = CcmSheetApiOld::UpdateSpreadsheet(spreadsheet_token.to_string());
let api_request: ApiRequest<UpdateSpreadsheetResponse> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "更新表格")?);
Transport::request_typed(api_request, config, Some(option), "更新表格").await
}
#[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_get_spreadsheet_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/open-apis/sheets/v3/spreadsheets/token001"))
.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 = get_spreadsheet(
&config,
"token001",
GetSpreadsheetParams {
include_sheet: None,
},
)
.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"
);
}
}