Skip to main content

zai_rs/model/voice_clone/
data.rs

1use std::sync::Arc;
2
3use serde::Serialize;
4use validator::Validate;
5
6use super::{super::traits::*, request::VoiceCloneBody};
7use crate::client::{
8    endpoints::{ApiBase, EndpointConfig, paths},
9    http::{HttpClient, HttpClientConfig, parse_typed_response},
10};
11
12/// Voice clone request wrapper using JSON
13pub struct VoiceCloneRequest<N>
14where
15    N: ModelName + VoiceClone + Serialize,
16{
17    pub key: String,
18    url: String,
19    endpoint_config: EndpointConfig,
20    api_base: ApiBase,
21    http_config: Arc<HttpClientConfig>,
22    body: VoiceCloneBody<N>,
23}
24
25impl<N> VoiceCloneRequest<N>
26where
27    N: ModelName + VoiceClone + Serialize,
28{
29    /// Create a new voice clone request with required fields
30    pub fn new(
31        model: N,
32        key: String,
33        voice_name: impl Into<String>,
34        input: impl Into<String>,
35        file_id: impl Into<String>,
36    ) -> Self {
37        let body = VoiceCloneBody::new(model, voice_name, input, file_id);
38        let endpoint_config = EndpointConfig::default();
39        let api_base = ApiBase::PaasV4;
40        let url = endpoint_config.url(&api_base, paths::VOICE_CLONE);
41        Self {
42            key,
43            url,
44            endpoint_config,
45            api_base,
46            http_config: Arc::new(HttpClientConfig::default()),
47            body,
48        }
49    }
50
51    fn rebuild_url(&mut self) {
52        self.url = self.endpoint_config.url(&self.api_base, paths::VOICE_CLONE);
53    }
54
55    pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
56        self.api_base = ApiBase::Custom(base.into());
57        self.rebuild_url();
58        self
59    }
60
61    pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
62        self.endpoint_config = endpoint_config;
63        self.rebuild_url();
64        self
65    }
66
67    pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
68        self.http_config = Arc::new(config);
69        self
70    }
71
72    pub fn body_mut(&mut self) -> &mut VoiceCloneBody<N> {
73        &mut self.body
74    }
75
76    /// Optional: reference text of the example audio
77    pub fn with_text(mut self, text: impl Into<String>) -> Self {
78        self.body = self.body.with_text(text);
79        self
80    }
81
82    /// Optional: client-provided request id
83    pub fn with_request_id(mut self, request_id: impl Into<String>) -> Self {
84        self.body = self.body.with_request_id(request_id);
85        self
86    }
87
88    pub fn validate(&self) -> crate::ZaiResult<()> {
89        self.body
90            .validate()
91            .map_err(|e| crate::client::error::ZaiError::ApiError {
92                code: 1200,
93                message: format!("Validation error: {:?}", e),
94            })?;
95        Ok(())
96    }
97
98    pub async fn send(&self) -> crate::ZaiResult<super::response::VoiceCloneResponse> {
99        self.validate()?;
100        let resp = self.post().await?;
101        let parsed = parse_typed_response::<super::response::VoiceCloneResponse>(resp).await?;
102        Ok(parsed)
103    }
104}
105
106impl<N> HttpClient for VoiceCloneRequest<N>
107where
108    N: ModelName + VoiceClone + Serialize,
109{
110    type Body = VoiceCloneBody<N>;
111    type ApiUrl = String;
112    type ApiKey = String;
113
114    fn api_url(&self) -> &Self::ApiUrl {
115        &self.url
116    }
117    fn api_key(&self) -> &Self::ApiKey {
118        &self.key
119    }
120    fn body(&self) -> &Self::Body {
121        &self.body
122    }
123
124    fn http_config(&self) -> Arc<HttpClientConfig> {
125        Arc::clone(&self.http_config)
126    }
127}