use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
validate_required, SDKResult,
};
use serde::{Deserialize, Serialize};
use crate::ccm::wiki::v2::models::WikiSpaceNode;
use crate::common::{api_endpoints::WikiApiV2, api_utils::*};
pub struct MoveWikiSpaceNodeRequest {
space_id: String,
node_token: String,
config: Config,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MoveWikiSpaceNodeParams {
pub target_parent_token: String,
pub target_space_id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MoveWikiSpaceNodeResponse {
pub node: Option<WikiSpaceNode>,
}
impl ApiResponseTrait for MoveWikiSpaceNodeResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl MoveWikiSpaceNodeRequest {
pub fn new(config: Config) -> Self {
Self {
space_id: String::new(),
node_token: String::new(),
config,
}
}
pub fn space_id(mut self, space_id: impl Into<String>) -> Self {
self.space_id = space_id.into();
self
}
pub fn node_token(mut self, node_token: impl Into<String>) -> Self {
self.node_token = node_token.into();
self
}
pub async fn execute(
self,
params: MoveWikiSpaceNodeParams,
) -> SDKResult<MoveWikiSpaceNodeResponse> {
self.execute_with_options(params, openlark_core::req_option::RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
params: MoveWikiSpaceNodeParams,
option: openlark_core::req_option::RequestOption,
) -> SDKResult<MoveWikiSpaceNodeResponse> {
validate_required!(self.space_id, "知识空间ID不能为空");
validate_required!(self.node_token, "节点Token不能为空");
validate_required!(params.target_parent_token, "目标父节点token不能为空");
validate_required!(params.target_space_id, "目标知识空间ID不能为空");
let api_endpoint = WikiApiV2::SpaceNodeMove(self.space_id.clone(), self.node_token.clone());
let api_request: ApiRequest<MoveWikiSpaceNodeResponse> =
ApiRequest::post(&api_endpoint.to_url())
.body(serialize_params(¶ms, "移动知识空间节点")?);
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).unwrap();
assert_eq!(value["field"], "data");
}
}