Skip to main content

zai_rs/model/voice_clone/
data.rs

1use serde::Serialize;
2use validator::Validate;
3
4use super::{super::traits::*, request::VoiceCloneBody};
5use crate::client::http::HttpClient;
6
7/// Voice clone request wrapper using JSON
8pub struct VoiceCloneRequest<N>
9where
10    N: ModelName + VoiceClone + Serialize,
11{
12    pub key: String,
13    body: VoiceCloneBody<N>,
14}
15
16impl<N> VoiceCloneRequest<N>
17where
18    N: ModelName + VoiceClone + Serialize,
19{
20    /// Create a new voice clone request with required fields
21    pub fn new(
22        model: N,
23        key: String,
24        voice_name: impl Into<String>,
25        input: impl Into<String>,
26        file_id: impl Into<String>,
27    ) -> Self {
28        let body = VoiceCloneBody::new(model, voice_name, input, file_id);
29        Self { key, body }
30    }
31
32    pub fn body_mut(&mut self) -> &mut VoiceCloneBody<N> {
33        &mut self.body
34    }
35
36    /// Optional: reference text of the example audio
37    pub fn with_text(mut self, text: impl Into<String>) -> Self {
38        self.body = self.body.with_text(text);
39        self
40    }
41
42    /// Optional: client-provided request id
43    pub fn with_request_id(mut self, request_id: impl Into<String>) -> Self {
44        self.body = self.body.with_request_id(request_id);
45        self
46    }
47
48    pub fn validate(&self) -> crate::ZaiResult<()> {
49        self.body
50            .validate()
51            .map_err(|e| crate::client::error::ZaiError::ApiError {
52                code: 1200,
53                message: format!("Validation error: {:?}", e),
54            })?;
55        Ok(())
56    }
57
58    pub async fn send(&self) -> crate::ZaiResult<super::response::VoiceCloneResponse> {
59        self.validate()?;
60        let resp = self.post().await?;
61        let parsed = resp.json::<super::response::VoiceCloneResponse>().await?;
62        Ok(parsed)
63    }
64}
65
66impl<N> HttpClient for VoiceCloneRequest<N>
67where
68    N: ModelName + VoiceClone + Serialize,
69{
70    type Body = VoiceCloneBody<N>;
71    type ApiUrl = &'static str;
72    type ApiKey = String;
73
74    fn api_url(&self) -> &Self::ApiUrl {
75        &"https://open.bigmodel.cn/api/paas/v4/voice/clone"
76    }
77    fn api_key(&self) -> &Self::ApiKey {
78        &self.key
79    }
80    fn body(&self) -> &Self::Body {
81        &self.body
82    }
83}