use serde::Serialize;
use validator::Validate;
use super::{
super::traits::*,
request::{TextToAudioBody, TtsAudioFormat, Voice},
};
use crate::client::ZaiClient;
pub struct TextToAudioRequest<N>
where
N: ModelName + TextToAudio + Serialize,
{
body: TextToAudioBody<N>,
}
impl<N> TextToAudioRequest<N>
where
N: ModelName + TextToAudio + Serialize,
{
pub fn new(model: N) -> Self {
let body = TextToAudioBody::new(model);
Self { body }
}
pub fn body_mut(&mut self) -> &mut TextToAudioBody<N> {
&mut self.body
}
pub fn with_input(mut self, input: impl Into<String>) -> Self {
self.body = self.body.with_input(input);
self
}
pub fn with_voice(mut self, voice: Voice) -> Self {
self.body = self.body.with_voice(voice);
self
}
pub fn with_speed(mut self, speed: f32) -> Self {
self.body = self.body.with_speed(speed);
self
}
pub fn with_volume(mut self, volume: f32) -> Self {
self.body = self.body.with_volume(volume);
self
}
pub fn with_response_format(mut self, fmt: TtsAudioFormat) -> Self {
self.body = self.body.with_response_format(fmt);
self
}
pub fn with_watermark_enabled(mut self, enabled: bool) -> Self {
self.body = self.body.with_watermark_enabled(enabled);
self
}
pub fn validate(&self) -> crate::ZaiResult<()> {
self.body
.validate()
.map_err(|e| crate::client::error::ZaiError::ApiError {
code: crate::client::error::codes::SDK_VALIDATION,
message: format!("Validation error: {e:?}"),
})?;
Ok(())
}
pub async fn send_via(&self, client: &ZaiClient) -> crate::ZaiResult<bytes::Bytes> {
self.validate()?;
let route = crate::client::routes::AUDIO_SYNTHESIZE;
let url = client.endpoints().resolve_route(route, &[])?;
client
.send_json_bytes(route.method(), url, &self.body)
.await
}
}