use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required,
};
use serde::{Deserialize, Serialize};
use crate::ccm::wiki::v2::models::WikiSpaceNode;
use crate::common::{api_endpoints::WikiApiV2, api_utils::*};
pub struct GetWikiSpaceNodeRequest {
config: Config,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetWikiSpaceNodeParams {
pub token: String,
pub obj_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetWikiSpaceNodeResponse {
pub node: Option<WikiSpaceNode>,
}
impl ApiResponseTrait for GetWikiSpaceNodeResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl GetWikiSpaceNodeRequest {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn execute(
self,
params: GetWikiSpaceNodeParams,
) -> SDKResult<GetWikiSpaceNodeResponse> {
self.execute_with_options(params, RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
params: GetWikiSpaceNodeParams,
option: RequestOption,
) -> SDKResult<GetWikiSpaceNodeResponse> {
validate_required!(params.token, "token不能为空");
let api_endpoint = WikiApiV2::SpaceGetNode;
let mut api_request: ApiRequest<GetWikiSpaceNodeResponse> =
ApiRequest::get(&api_endpoint.to_url());
api_request = api_request.query("token", ¶ms.token);
if let Some(obj_type) = params.obj_type {
api_request = api_request.query("obj_type", &obj_type);
}
let response = Transport::request(api_request, &self.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");
}
}