Skip to main content

zai_rs/model/text_to_audio/
data.rs

1use std::sync::Arc;
2
3use serde::Serialize;
4
5use super::{
6    super::traits::*,
7    request::{TextToAudioBody, TtsAudioFormat, Voice},
8};
9use crate::client::{
10    endpoints::{ApiBase, EndpointConfig, paths},
11    http::{HttpClient, HttpClientConfig},
12};
13
14/// Text-to-speech request wrapper using JSON body
15pub struct TextToAudioRequest<N>
16where
17    N: ModelName + TextToAudio + Serialize,
18{
19    pub key: String,
20    url: String,
21    endpoint_config: EndpointConfig,
22    api_base: ApiBase,
23    http_config: Arc<HttpClientConfig>,
24    body: TextToAudioBody<N>,
25}
26
27impl<N> TextToAudioRequest<N>
28where
29    N: ModelName + TextToAudio + Serialize,
30{
31    pub fn new(model: N, key: String) -> Self {
32        let body = TextToAudioBody::new(model);
33        let endpoint_config = EndpointConfig::default();
34        let api_base = ApiBase::PaasV4;
35        let url = endpoint_config.url(&api_base, paths::AUDIO_SPEECH);
36        Self {
37            key,
38            url,
39            endpoint_config,
40            api_base,
41            http_config: Arc::new(HttpClientConfig::default()),
42            body,
43        }
44    }
45
46    fn rebuild_url(&mut self) {
47        self.url = self
48            .endpoint_config
49            .url(&self.api_base, paths::AUDIO_SPEECH);
50    }
51
52    pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
53        self.api_base = ApiBase::Custom(base.into());
54        self.rebuild_url();
55        self
56    }
57
58    pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
59        self.endpoint_config = endpoint_config;
60        self.rebuild_url();
61        self
62    }
63
64    pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
65        self.http_config = Arc::new(config);
66        self
67    }
68
69    pub fn body_mut(&mut self) -> &mut TextToAudioBody<N> {
70        &mut self.body
71    }
72
73    pub fn with_input(mut self, input: impl Into<String>) -> Self {
74        self.body = self.body.with_input(input);
75        self
76    }
77    pub fn with_voice(mut self, voice: Voice) -> Self {
78        self.body = self.body.with_voice(voice);
79        self
80    }
81    pub fn with_speed(mut self, speed: f32) -> Self {
82        self.body = self.body.with_speed(speed);
83        self
84    }
85    pub fn with_volume(mut self, volume: f32) -> Self {
86        self.body = self.body.with_volume(volume);
87        self
88    }
89    pub fn with_response_format(mut self, fmt: TtsAudioFormat) -> Self {
90        self.body = self.body.with_response_format(fmt);
91        self
92    }
93    pub fn with_watermark_enabled(mut self, enabled: bool) -> Self {
94        self.body = self.body.with_watermark_enabled(enabled);
95        self
96    }
97}
98
99impl<N> HttpClient for TextToAudioRequest<N>
100where
101    N: ModelName + TextToAudio + Serialize,
102{
103    type Body = TextToAudioBody<N>;
104    type ApiUrl = String;
105    type ApiKey = String;
106
107    fn api_url(&self) -> &Self::ApiUrl {
108        &self.url
109    }
110    fn api_key(&self) -> &Self::ApiKey {
111        &self.key
112    }
113    fn body(&self) -> &Self::Body {
114        &self.body
115    }
116
117    fn http_config(&self) -> Arc<HttpClientConfig> {
118        Arc::clone(&self.http_config)
119    }
120}