use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::{cloud_docs::*, EndpointBuilder},
http::Transport,
req_option::RequestOption,
SDKResult,
},
impl_executable_builder_owned,
};
#[derive(Debug, Serialize, Default)]
pub struct CreateSpaceNodeRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
space_id: String,
obj_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
parent_node_token: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
node_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
}
impl CreateSpaceNodeRequest {
pub fn builder() -> CreateSpaceNodeRequestBuilder {
CreateSpaceNodeRequestBuilder::default()
}
pub fn new(space_id: impl ToString, obj_type: impl ToString) -> Self {
Self {
space_id: space_id.to_string(),
obj_type: obj_type.to_string(),
..Default::default()
}
}
}
#[derive(Default)]
pub struct CreateSpaceNodeRequestBuilder {
request: CreateSpaceNodeRequest,
}
impl CreateSpaceNodeRequestBuilder {
pub fn space_id(mut self, space_id: impl ToString) -> Self {
self.request.space_id = space_id.to_string();
self
}
pub fn obj_type(mut self, obj_type: impl ToString) -> Self {
self.request.obj_type = obj_type.to_string();
self
}
pub fn with_doc_type(mut self) -> Self {
self.request.obj_type = "doc".to_string();
self
}
pub fn with_sheet_type(mut self) -> Self {
self.request.obj_type = "sheet".to_string();
self
}
pub fn with_mindnote_type(mut self) -> Self {
self.request.obj_type = "mindnote".to_string();
self
}
pub fn with_bitable_type(mut self) -> Self {
self.request.obj_type = "bitable".to_string();
self
}
pub fn parent_node_token(mut self, parent_node_token: impl ToString) -> Self {
self.request.parent_node_token = Some(parent_node_token.to_string());
self
}
pub fn node_type(mut self, node_type: impl ToString) -> Self {
self.request.node_type = Some(node_type.to_string());
self
}
pub fn with_origin_node(mut self) -> Self {
self.request.node_type = Some("origin".to_string());
self
}
pub fn with_shortcut_node(mut self) -> Self {
self.request.node_type = Some("shortcut".to_string());
self
}
pub fn title(mut self, title: impl ToString) -> Self {
self.request.title = Some(title.to_string());
self
}
pub fn build(mut self) -> CreateSpaceNodeRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
impl_executable_builder_owned!(
CreateSpaceNodeRequestBuilder,
crate::service::cloud_docs::wiki::v2::space_node::SpaceNodeService,
CreateSpaceNodeRequest,
CreateSpaceNodeResponse,
create
);
#[derive(Debug, Deserialize)]
pub struct CreatedNode {
pub space_id: String,
pub node_token: String,
pub obj_type: String,
pub parent_node_token: Option<String>,
pub node_type: Option<String>,
pub obj_token: Option<String>,
pub title: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct CreateSpaceNodeResponse {
pub node: CreatedNode,
}
impl ApiResponseTrait for CreateSpaceNodeResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn create_space_node(
request: CreateSpaceNodeRequest,
config: &Config,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<CreateSpaceNodeResponse>> {
let mut api_req = request.api_request;
api_req.http_method = Method::POST;
api_req.api_path =
EndpointBuilder::replace_param(WIKI_V2_SPACE_NODES, "space_id", &request.space_id);
api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
let api_resp = Transport::request(api_req, config, option).await?;
Ok(api_resp)
}
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
use super::*;
#[test]
fn test_create_space_node_request_builder() {
let request = CreateSpaceNodeRequest::builder()
.space_id("spcxxxxxx")
.with_doc_type()
.parent_node_token("wikcnxxxxxx")
.with_origin_node()
.title("我的文档")
.build();
assert_eq!(request.space_id, "spcxxxxxx");
assert_eq!(request.obj_type, "doc");
assert_eq!(request.parent_node_token, Some("wikcnxxxxxx".to_string()));
assert_eq!(request.node_type, Some("origin".to_string()));
assert_eq!(request.title, Some("我的文档".to_string()));
}
}