use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::Endpoints,
http::Transport,
req_option::RequestOption,
trait_system::Service,
SDKResult,
},
service::ai::models::{FileRecognizeRequest, OcrResult},
};
pub struct OpticalCharRecognitionService {
pub config: Config,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct OcrResponse {
#[serde(flatten)]
pub ocr_result: OcrResult,
}
impl ApiResponseTrait for OcrResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl OpticalCharRecognitionService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn basic_recognize(
&self,
request: FileRecognizeRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<OcrResponse>> {
let api_req = ApiRequest {
http_method: Method::POST,
api_path: Endpoints::OPTICAL_CHAR_RECOGNITION_V1_BASIC_RECOGNIZE.to_string(),
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
}
}
impl Service for OpticalCharRecognitionService {
fn config(&self) -> &Config {
&self.config
}
fn service_name() -> &'static str {
"optical_char_recognition"
}
fn service_version() -> &'static str {
"v1"
}
}