pub mod models;
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::human_authentication::models::*,
};
pub struct HumanAuthenticationService {
pub config: Config,
}
impl HumanAuthenticationService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn create_identity(
&self,
request: IdentityCreateRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<IdentityCreateResponse>> {
let api_req = ApiRequest {
http_method: Method::POST,
api_path: Endpoints::HUMAN_AUTHENTICATION_V1_IDENTITIES.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
}
pub async fn upload_face_image(
&self,
request: FaceImageUploadRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<FaceImageUploadResponse>> {
let api_req = ApiRequest {
http_method: Method::POST,
api_path: Endpoints::HUMAN_AUTHENTICATION_V1_FACE_IMAGES.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
}
pub async fn crop_face_image(
&self,
request: FaceImageCropRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<FaceImageCropResponse>> {
let api_req = ApiRequest {
http_method: Method::POST,
api_path: Endpoints::HUMAN_AUTHENTICATION_V1_FACE_IMAGES_CROP.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
}
pub async fn query_authentication_result(
&self,
identity_id: &str,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<AuthenticationResultResponse>> {
let api_req = ApiRequest {
http_method: Method::GET,
api_path: EndpointBuilder::replace_param(
Endpoints::HUMAN_AUTHENTICATION_V1_IDENTITY_RESULT,
"identity_id",
identity_id,
),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
body: vec![],
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdentityCreateRequest {
pub name: String,
pub id_number: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub id_type: Option<IdType>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IdentityCreateResponse {
pub identity_id: String,
pub created_at: i64,
}
impl ApiResponseTrait for IdentityCreateResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FaceImageUploadRequest {
pub identity_id: String,
pub image_content: String,
pub image_type: ImageType,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FaceImageUploadResponse {
pub image_id: String,
pub uploaded_at: i64,
}
impl ApiResponseTrait for FaceImageUploadResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FaceImageCropRequest {
pub image_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub crop_params: Option<CropParameters>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FaceImageCropResponse {
pub cropped_image_id: String,
pub cropped_at: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub detected_face: Option<FaceRegion>,
}
impl ApiResponseTrait for FaceImageCropResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AuthenticationResultResponse {
pub identity_id: String,
pub status: AuthenticationStatus,
#[serde(skip_serializing_if = "Option::is_none")]
pub confidence_score: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub completed_at: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error_message: Option<String>,
}
impl ApiResponseTrait for AuthenticationResultResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn test_human_authentication_service_creation() {
let config = Config::default();
let service = HumanAuthenticationService::new(config.clone());
assert_eq!(service.config.app_id, config.app_id);
assert_eq!(service.config.app_secret, config.app_secret);
}
#[test]
fn test_human_authentication_service_with_custom_config() {
let config = Config::builder()
.app_id("human_auth_test_app")
.app_secret("human_auth_test_secret")
.req_timeout(Duration::from_secs(120))
.build();
let service = HumanAuthenticationService::new(config.clone());
assert_eq!(service.config.app_id, "human_auth_test_app");
assert_eq!(service.config.app_secret, "human_auth_test_secret");
assert_eq!(service.config.req_timeout, Some(Duration::from_secs(120)));
}
#[test]
fn test_human_authentication_service_config_independence() {
let config1 = Config::builder().app_id("human_auth_app_1").build();
let config2 = Config::builder().app_id("human_auth_app_2").build();
let service1 = HumanAuthenticationService::new(config1);
let service2 = HumanAuthenticationService::new(config2);
assert_eq!(service1.config.app_id, "human_auth_app_1");
assert_eq!(service2.config.app_id, "human_auth_app_2");
assert_ne!(service1.config.app_id, service2.config.app_id);
}
#[test]
fn test_human_authentication_service_accessible() {
let config = Config::default();
let service = HumanAuthenticationService::new(config.clone());
assert_eq!(service.config.app_id, config.app_id);
}
#[test]
fn test_human_authentication_service_config_cloning() {
let config = Config::builder()
.app_id("clone_test_app")
.app_secret("clone_test_secret")
.build();
let service = HumanAuthenticationService::new(config.clone());
assert_eq!(service.config.app_id, "clone_test_app");
assert_eq!(service.config.app_secret, "clone_test_secret");
}
#[test]
fn test_human_authentication_service_timeout_propagation() {
let config = Config::builder()
.req_timeout(Duration::from_secs(150))
.build();
let service = HumanAuthenticationService::new(config);
assert_eq!(service.config.req_timeout, Some(Duration::from_secs(150)));
}
#[test]
fn test_human_authentication_service_multiple_instances() {
let config = Config::default();
let service1 = HumanAuthenticationService::new(config.clone());
let service2 = HumanAuthenticationService::new(config.clone());
assert_eq!(service1.config.app_id, service2.config.app_id);
assert_eq!(service1.config.app_secret, service2.config.app_secret);
}
#[test]
fn test_human_authentication_service_config_consistency() {
let config = Config::builder()
.app_id("consistency_test")
.app_secret("consistency_secret")
.req_timeout(Duration::from_secs(180))
.build();
let service = HumanAuthenticationService::new(config);
assert_eq!(service.config.app_id, "consistency_test");
assert_eq!(service.config.app_secret, "consistency_secret");
assert_eq!(service.config.req_timeout, Some(Duration::from_secs(180)));
}
}