use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
use crate::common::{api_endpoints::WikiApiV2, api_utils::*};
pub struct MoveDocsToWikiRequest {
config: Config,
space_id: String,
obj_token: String,
obj_type: String,
parent_wiki_token: String,
apply: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct MoveDocsToWikiRequestBody {
obj_token: String,
obj_type: String,
parent_wiki_token: String,
#[serde(skip_serializing_if = "Option::is_none")]
apply: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MoveDocsToWikiResponse {
pub wiki_token: Option<String>,
pub task_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub applied: Option<bool>,
}
impl ApiResponseTrait for MoveDocsToWikiResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl MoveDocsToWikiRequest {
pub fn new(config: Config) -> Self {
Self {
config,
space_id: String::new(),
obj_token: String::new(),
obj_type: String::new(),
parent_wiki_token: String::new(),
apply: None,
}
}
pub fn space_id(mut self, space_id: impl Into<String>) -> Self {
self.space_id = space_id.into();
self
}
pub fn obj_token(mut self, obj_token: impl Into<String>) -> Self {
self.obj_token = obj_token.into();
self
}
pub fn obj_type(mut self, obj_type: impl Into<String>) -> Self {
self.obj_type = obj_type.into();
self
}
pub fn parent_wiki_token(mut self, parent_wiki_token: impl Into<String>) -> Self {
self.parent_wiki_token = parent_wiki_token.into();
self
}
pub fn apply(mut self, apply: bool) -> Self {
self.apply = Some(apply);
self
}
pub async fn execute(self) -> SDKResult<MoveDocsToWikiResponse> {
self.execute_with_options(openlark_core::req_option::RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
option: openlark_core::req_option::RequestOption,
) -> SDKResult<MoveDocsToWikiResponse> {
validate_required!(self.space_id, "知识空间ID不能为空");
validate_required!(self.obj_token, "源文档token不能为空");
validate_required!(self.obj_type, "源文档类型不能为空");
validate_required!(self.parent_wiki_token, "目标父节点wiki token不能为空");
let api_endpoint = WikiApiV2::SpaceNodeMoveDocsToWiki(self.space_id);
let request_body = MoveDocsToWikiRequestBody {
obj_token: self.obj_token,
obj_type: self.obj_type,
parent_wiki_token: self.parent_wiki_token,
apply: self.apply,
};
let api_request: ApiRequest<MoveDocsToWikiResponse> = api_endpoint
.to_request()
.body(serialize_params(&request_body, "移动云空间文档至知识空间")?);
Transport::request_typed(api_request, &self.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_move_docs_to_wiki_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path(
"/open-apis/wiki/v2/spaces/sp001/nodes/move_docs_to_wiki",
))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success", "data": { "wiki_token": "wt001" }
})))
.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 = MoveDocsToWikiRequest::new(config)
.space_id("sp001")
.obj_token("obj001")
.obj_type("docx")
.parent_wiki_token("pwt001")
.execute()
.await
.expect("移动云文档至知识空间应成功");
assert_eq!(resp.wiki_token.as_deref(), Some("wt001"));
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/wiki/v2/spaces/sp001/nodes/move_docs_to_wiki"
);
}
}