use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::{EndpointBuilder, Endpoints},
http::Transport,
req_option::RequestOption,
SDKResult,
},
service::search::v2::models::{CreateSchemaRequest, Schema, UpdateSchemaRequest},
};
pub struct SchemaService {
pub config: Config,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateSchemaResponse {
pub schema: Schema,
}
impl ApiResponseTrait for CreateSchemaResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GetSchemaResponse {
pub schema: Schema,
}
impl ApiResponseTrait for GetSchemaResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateSchemaResponse {
pub schema: Schema,
}
impl ApiResponseTrait for UpdateSchemaResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EmptySchemaResponse {}
impl ApiResponseTrait for EmptySchemaResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl SchemaService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn create(
&self,
data_source_id: &str,
request: CreateSchemaRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<CreateSchemaResponse>> {
let api_req = ApiRequest {
http_method: Method::POST,
api_path: EndpointBuilder::replace_param(
Endpoints::SEARCH_V2_SCHEMA_CREATE,
"data_source_id",
data_source_id,
),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: serde_json::to_vec(&request)?,
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
pub async fn delete(
&self,
data_source_id: &str,
schema_id: &str,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<EmptySchemaResponse>> {
let api_req = ApiRequest {
http_method: Method::DELETE,
api_path: EndpointBuilder::replace_params_from_array(
Endpoints::SEARCH_V2_SCHEMA_OPERATION,
&[("data_source_id", data_source_id), ("schema_id", schema_id)],
),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
pub async fn patch(
&self,
data_source_id: &str,
schema_id: &str,
request: UpdateSchemaRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<UpdateSchemaResponse>> {
let api_req = ApiRequest {
http_method: Method::PATCH,
api_path: EndpointBuilder::replace_params_from_array(
Endpoints::SEARCH_V2_SCHEMA_OPERATION,
&[("data_source_id", data_source_id), ("schema_id", schema_id)],
),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: serde_json::to_vec(&request)?,
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
pub async fn get(
&self,
data_source_id: &str,
schema_id: &str,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<GetSchemaResponse>> {
let api_req = ApiRequest {
http_method: Method::GET,
api_path: EndpointBuilder::replace_params_from_array(
Endpoints::SEARCH_V2_SCHEMA_OPERATION,
&[("data_source_id", data_source_id), ("schema_id", schema_id)],
),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
}