open_lark/service/search/v2/schema/
mod.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    core::{
6        api_req::ApiRequest,
7        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
8        config::Config,
9        constants::AccessTokenType,
10        endpoints::{EndpointBuilder, Endpoints},
11        http::Transport,
12        req_option::RequestOption,
13        SDKResult,
14    },
15    service::search::v2::models::{CreateSchemaRequest, Schema, UpdateSchemaRequest},
16};
17
18/// 数据范式服务
19pub struct SchemaService {
20    pub config: Config,
21}
22
23/// 创建数据范式响应
24#[derive(Debug, Serialize, Deserialize)]
25pub struct CreateSchemaResponse {
26    /// 数据范式信息
27    pub schema: Schema,
28}
29
30impl ApiResponseTrait for CreateSchemaResponse {
31    fn data_format() -> ResponseFormat {
32        ResponseFormat::Data
33    }
34}
35
36/// 获取数据范式响应
37#[derive(Debug, Serialize, Deserialize)]
38pub struct GetSchemaResponse {
39    /// 数据范式信息
40    pub schema: Schema,
41}
42
43impl ApiResponseTrait for GetSchemaResponse {
44    fn data_format() -> ResponseFormat {
45        ResponseFormat::Data
46    }
47}
48
49/// 更新数据范式响应
50#[derive(Debug, Serialize, Deserialize)]
51pub struct UpdateSchemaResponse {
52    /// 数据范式信息
53    pub schema: Schema,
54}
55
56impl ApiResponseTrait for UpdateSchemaResponse {
57    fn data_format() -> ResponseFormat {
58        ResponseFormat::Data
59    }
60}
61
62/// 空响应(用于删除等操作)
63#[derive(Debug, Serialize, Deserialize)]
64pub struct EmptySchemaResponse {}
65
66impl ApiResponseTrait for EmptySchemaResponse {
67    fn data_format() -> ResponseFormat {
68        ResponseFormat::Data
69    }
70}
71
72impl SchemaService {
73    pub fn new(config: Config) -> Self {
74        Self { config }
75    }
76
77    /// 创建数据范式
78    ///
79    /// 该接口用于创建搜索连接器的数据范式。
80    ///
81    /// 注意事项:
82    /// - 需要申请相应权限
83    /// - 数据范式定义需要符合JSON Schema规范
84    ///
85    /// # 参数
86    ///
87    /// - `data_source_id`: 数据源ID
88    /// - `request`: 创建数据范式请求参数
89    /// - `option`: 可选的请求配置
90    pub async fn create(
91        &self,
92        data_source_id: &str,
93        request: CreateSchemaRequest,
94        option: Option<RequestOption>,
95    ) -> SDKResult<BaseResponse<CreateSchemaResponse>> {
96        let api_req = ApiRequest {
97            http_method: Method::POST,
98            api_path: EndpointBuilder::replace_param(
99                Endpoints::SEARCH_V2_SCHEMA_CREATE,
100                "data_source_id",
101                data_source_id,
102            ),
103            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
104            body: serde_json::to_vec(&request)?,
105            ..Default::default()
106        };
107
108        Transport::request(api_req, &self.config, option).await
109    }
110
111    /// 删除数据范式
112    ///
113    /// 该接口用于删除指定的数据范式。
114    ///
115    /// # 参数
116    ///
117    /// - `data_source_id`: 数据源ID
118    /// - `schema_id`: 数据范式ID
119    /// - `option`: 可选的请求配置
120    pub async fn delete(
121        &self,
122        data_source_id: &str,
123        schema_id: &str,
124        option: Option<RequestOption>,
125    ) -> SDKResult<BaseResponse<EmptySchemaResponse>> {
126        let api_req = ApiRequest {
127            http_method: Method::DELETE,
128            api_path: EndpointBuilder::replace_params_from_array(
129                Endpoints::SEARCH_V2_SCHEMA_OPERATION,
130                &[("data_source_id", data_source_id), ("schema_id", schema_id)],
131            ),
132            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
133            ..Default::default()
134        };
135
136        Transport::request(api_req, &self.config, option).await
137    }
138
139    /// 修改数据范式
140    ///
141    /// 该接口用于修改指定数据范式的信息。
142    ///
143    /// # 参数
144    ///
145    /// - `data_source_id`: 数据源ID
146    /// - `schema_id`: 数据范式ID
147    /// - `request`: 更新数据范式请求参数
148    /// - `option`: 可选的请求配置
149    pub async fn patch(
150        &self,
151        data_source_id: &str,
152        schema_id: &str,
153        request: UpdateSchemaRequest,
154        option: Option<RequestOption>,
155    ) -> SDKResult<BaseResponse<UpdateSchemaResponse>> {
156        let api_req = ApiRequest {
157            http_method: Method::PATCH,
158            api_path: EndpointBuilder::replace_params_from_array(
159                Endpoints::SEARCH_V2_SCHEMA_OPERATION,
160                &[("data_source_id", data_source_id), ("schema_id", schema_id)],
161            ),
162            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
163            body: serde_json::to_vec(&request)?,
164            ..Default::default()
165        };
166
167        Transport::request(api_req, &self.config, option).await
168    }
169
170    /// 获取数据范式
171    ///
172    /// 该接口用于获取指定数据范式的详细信息。
173    ///
174    /// # 参数
175    ///
176    /// - `data_source_id`: 数据源ID
177    /// - `schema_id`: 数据范式ID
178    /// - `option`: 可选的请求配置
179    pub async fn get(
180        &self,
181        data_source_id: &str,
182        schema_id: &str,
183        option: Option<RequestOption>,
184    ) -> SDKResult<BaseResponse<GetSchemaResponse>> {
185        let api_req = ApiRequest {
186            http_method: Method::GET,
187            api_path: EndpointBuilder::replace_params_from_array(
188                Endpoints::SEARCH_V2_SCHEMA_OPERATION,
189                &[("data_source_id", data_source_id), ("schema_id", schema_id)],
190            ),
191            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
192            ..Default::default()
193        };
194
195        Transport::request(api_req, &self.config, option).await
196    }
197}