use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
SDKResult,
};
use serde::{Deserialize, Serialize};
use crate::common::{api_endpoints::DriveApi, api_utils::*};
#[derive(Debug, Clone, Serialize)]
pub struct BatchQueryMetaRequest {
#[serde(skip)]
pub user_id_type: Option<String>,
pub request_docs: Vec<RequestDoc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub with_url: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestDoc {
pub doc_token: String,
pub doc_type: String,
}
impl BatchQueryMetaRequest {
pub fn new(request_docs: Vec<RequestDoc>) -> Self {
Self {
user_id_type: None,
request_docs,
with_url: None,
}
}
pub fn user_id_type(mut self, user_id_type: impl Into<String>) -> Self {
self.user_id_type = Some(user_id_type.into());
self
}
pub fn with_url(mut self, with_url: bool) -> Self {
self.with_url = Some(with_url);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Meta {
pub doc_token: String,
pub doc_type: String,
pub title: String,
pub owner_id: String,
pub create_time: String,
pub latest_modify_user: String,
pub latest_modify_time: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sec_label_name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FailedMeta {
pub token: String,
pub code: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchQueryMetaResponse {
#[serde(default)]
pub metas: Vec<Meta>,
#[serde(default)]
pub failed_list: Vec<FailedMeta>,
}
impl ApiResponseTrait for BatchQueryMetaResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn batch_query(
request: BatchQueryMetaRequest,
config: &Config,
option: Option<openlark_core::req_option::RequestOption>,
) -> SDKResult<BatchQueryMetaResponse> {
if request.request_docs.is_empty() || request.request_docs.len() > 200 {
return Err(openlark_core::error::validation_error(
"request_docs",
"request_docs 数量必须在 1~200 之间",
));
}
for doc in &request.request_docs {
if doc.doc_token.is_empty() {
return Err(openlark_core::error::validation_error(
"request_docs.doc_token",
"doc_token 不能为空",
));
}
if doc.doc_type.is_empty() {
return Err(openlark_core::error::validation_error(
"request_docs.doc_type",
"doc_type 不能为空",
));
}
match doc.doc_type.as_str() {
"doc" | "sheet" | "bitable" | "mindnote" | "file" | "wiki" | "docx" | "folder"
| "synced_block" => {}
_ => {
return Err(openlark_core::error::validation_error(
"request_docs.doc_type",
"doc_type 仅支持 doc/sheet/bitable/mindnote/file/wiki/docx/folder/synced_block",
));
}
}
}
let api_endpoint = DriveApi::BatchQueryMetas;
let mut api_request: ApiRequest<BatchQueryMetaResponse> =
ApiRequest::post(&api_endpoint.to_url())
.body(serialize_params(&request, "获取文件元数据")?);
if let Some(user_id_type) = &request.user_id_type {
match user_id_type.as_str() {
"open_id" | "union_id" | "user_id" => {}
_ => {
return Err(openlark_core::error::validation_error(
"user_id_type",
"user_id_type 仅支持 open_id/union_id/user_id",
));
}
}
api_request = api_request.query("user_id_type", user_id_type);
}
let response = Transport::request(api_request, config, 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).unwrap();
assert_eq!(value["field"], "data");
}
}