zai_rs/model/voice_clone/
data.rs

1use super::super::traits::*;
2use super::request::VoiceCloneBody;
3use crate::client::http::HttpClient;
4use serde::Serialize;
5use validator::Validate;
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) -> anyhow::Result<()> {
49        self.body.validate().map_err(|e| anyhow::anyhow!(e))?;
50        Ok(())
51    }
52
53    pub async fn send(&self) -> anyhow::Result<super::response::VoiceCloneResponse> {
54        self.validate()?;
55        let resp = self.post().await?;
56        let parsed = resp.json::<super::response::VoiceCloneResponse>().await?;
57        Ok(parsed)
58    }
59}
60
61impl<N> HttpClient for VoiceCloneRequest<N>
62where
63    N: ModelName + VoiceClone + Serialize,
64{
65    type Body = VoiceCloneBody<N>;
66    type ApiUrl = &'static str;
67    type ApiKey = String;
68
69    fn api_url(&self) -> &Self::ApiUrl {
70        &"https://open.bigmodel.cn/api/paas/v4/voice/clone"
71    }
72    fn api_key(&self) -> &Self::ApiKey {
73        &self.key
74    }
75    fn body(&self) -> &Self::Body {
76        &self.body
77    }
78}