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::*,
http::Transport,
req_option::RequestOption,
SDKResult,
},
impl_executable_builder_owned,
};
pub struct DocumentService {
config: Config,
}
impl DocumentService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn create(
&self,
request: CreateDocumentRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<CreateDocumentRespData>> {
let api_req = ApiRequest {
http_method: Method::POST,
api_path: DOCX_V1_DOCUMENTS.to_string(),
supported_access_token_types: vec![AccessTokenType::User, AccessTokenType::Tenant],
body: serde_json::to_vec(&request)?,
..Default::default()
};
let api_resp = Transport::request(api_req, &self.config, option).await?;
Ok(api_resp)
}
pub async fn get(
&self,
document_id: impl Into<String>,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<GetDocumentRespData>> {
let api_req = ApiRequest {
http_method: Method::GET,
api_path: DOCX_V1_DOCUMENT_GET.replace("{}", &document_id.into()),
supported_access_token_types: vec![AccessTokenType::User, AccessTokenType::Tenant],
..Default::default()
};
let api_resp = Transport::request(api_req, &self.config, option).await?;
Ok(api_resp)
}
pub async fn get_raw_content(
&self,
document_id: impl Into<String>,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<GetRawContentRespData>> {
let mut api_req = ApiRequest {
http_method: Method::GET,
api_path: DOCX_V1_DOCUMENT_RAW_CONTENT.replace("{}", &document_id.into()),
..Default::default()
};
api_req.supported_access_token_types = vec![AccessTokenType::User, AccessTokenType::Tenant];
let api_resp = Transport::request(api_req, &self.config, option).await?;
Ok(api_resp)
}
pub async fn list_blocks(
&self,
request: ListDocumentBlocksRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<ListDocumentBlocksRespData>> {
let mut api_req = ApiRequest {
http_method: Method::GET,
api_path: DOCX_V1_DOCUMENT_BLOCKS.replace("{}", &request.document_id),
..Default::default()
};
api_req.supported_access_token_types = vec![AccessTokenType::User, AccessTokenType::Tenant];
if let Some(page_size) = request.page_size {
api_req
.query_params
.insert("page_size", page_size.to_string());
}
if let Some(page_token) = request.page_token {
api_req.query_params.insert("page_token", page_token);
}
let api_resp = Transport::request(api_req, &self.config, option).await?;
Ok(api_resp)
}
pub async fn convert_to_docx(
&self,
document_id: impl Into<String>,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<ConvertToDocxRespData>> {
let mut api_req = ApiRequest {
http_method: Method::POST,
api_path: DOCX_V1_DOCUMENT_CONVERT.replace("{}", &document_id.into()),
..Default::default()
};
api_req.supported_access_token_types = vec![AccessTokenType::User, AccessTokenType::Tenant];
let api_resp = Transport::request(api_req, &self.config, option).await?;
Ok(api_resp)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CreateDocumentRequest {
pub title: String,
pub content: Option<String>,
pub folder_token: Option<String>,
}
impl CreateDocumentRequest {
pub fn builder() -> CreateDocumentRequestBuilder {
CreateDocumentRequestBuilder::default()
}
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
content: None,
folder_token: None,
}
}
pub fn with_content(mut self, content: impl Into<String>) -> Self {
self.content = Some(content.into());
self
}
pub fn with_folder_token(mut self, folder_token: impl Into<String>) -> Self {
self.folder_token = Some(folder_token.into());
self
}
}
#[derive(Default)]
pub struct CreateDocumentRequestBuilder {
request: CreateDocumentRequest,
}
impl CreateDocumentRequestBuilder {
pub fn title(mut self, title: impl Into<String>) -> Self {
self.request.title = title.into();
self
}
pub fn content(mut self, content: impl Into<String>) -> Self {
self.request.content = Some(content.into());
self
}
pub fn folder_token(mut self, folder_token: impl Into<String>) -> Self {
self.request.folder_token = Some(folder_token.into());
self
}
pub fn build(self) -> CreateDocumentRequest {
self.request
}
}
impl_executable_builder_owned!(
CreateDocumentRequestBuilder,
DocumentService,
CreateDocumentRequest,
BaseResponse<CreateDocumentRespData>,
create
);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateDocumentRespData {
pub document_id: String,
pub document_revision_id: i64,
pub title: String,
}
impl ApiResponseTrait for CreateDocumentRespData {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetDocumentRespData {
pub document: DocumentInfo,
}
impl ApiResponseTrait for GetDocumentRespData {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentInfo {
pub document_id: String,
pub document_revision_id: i64,
pub title: String,
pub create_time: String,
pub update_time: String,
pub creator_id: String,
pub last_editor_id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetRawContentRespData {
pub content: String,
}
impl ApiResponseTrait for GetRawContentRespData {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ListDocumentBlocksRequest {
pub document_id: String,
pub page_size: Option<i32>,
pub page_token: Option<String>,
}
impl ListDocumentBlocksRequest {
pub fn builder() -> ListDocumentBlocksRequestBuilder {
ListDocumentBlocksRequestBuilder::default()
}
pub fn new(document_id: impl Into<String>) -> Self {
Self {
document_id: document_id.into(),
page_size: None,
page_token: None,
}
}
pub fn with_page_size(mut self, page_size: i32) -> Self {
self.page_size = Some(page_size);
self
}
pub fn with_page_token(mut self, page_token: impl Into<String>) -> Self {
self.page_token = Some(page_token.into());
self
}
}
#[derive(Default)]
pub struct ListDocumentBlocksRequestBuilder {
request: ListDocumentBlocksRequest,
}
impl ListDocumentBlocksRequestBuilder {
pub fn document_id(mut self, document_id: impl Into<String>) -> Self {
self.request.document_id = document_id.into();
self
}
pub fn page_size(mut self, page_size: i32) -> Self {
self.request.page_size = Some(page_size);
self
}
pub fn page_token(mut self, page_token: impl Into<String>) -> Self {
self.request.page_token = Some(page_token.into());
self
}
pub fn build(self) -> ListDocumentBlocksRequest {
self.request
}
}
impl_executable_builder_owned!(
ListDocumentBlocksRequestBuilder,
DocumentService,
ListDocumentBlocksRequest,
BaseResponse<ListDocumentBlocksRespData>,
list_blocks
);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListDocumentBlocksRespData {
pub items: Vec<Block>,
pub has_more: bool,
pub page_token: Option<String>,
}
impl ApiResponseTrait for ListDocumentBlocksRespData {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
pub block_id: String,
pub parent_id: String,
pub children: Vec<String>,
pub block_type: i32,
pub index: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConvertToDocxRespData {
pub document_id: String,
pub document_revision_id: i64,
}
impl ApiResponseTrait for ConvertToDocxRespData {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}