Skip to main content

zai_rs/model/voice_list/
data.rs

1use std::sync::Arc;
2
3use super::request::VoiceListQuery;
4use crate::{
5    ZaiResult,
6    client::{
7        endpoints::{ApiBase, EndpointConfig, build_query, paths},
8        http::{HttpClient, HttpClientConfig, parse_typed_response},
9    },
10};
11
12/// GET voice list request
13pub struct VoiceListRequest {
14    pub key: String,
15    url: String,
16    endpoint_config: EndpointConfig,
17    api_base: ApiBase,
18    http_config: Arc<HttpClientConfig>,
19    query: VoiceListQuery,
20    // Empty body placeholder to satisfy HttpClient::Body
21    _body: (),
22}
23
24impl VoiceListRequest {
25    pub fn new(key: String) -> Self {
26        let endpoint_config = EndpointConfig::default();
27        let api_base = ApiBase::PaasV4;
28        let url = endpoint_config.url(&api_base, paths::VOICE_LIST);
29        Self {
30            key,
31            url,
32            endpoint_config,
33            api_base,
34            http_config: Arc::new(HttpClientConfig::default()),
35            query: VoiceListQuery::new(),
36            _body: (),
37        }
38    }
39
40    fn rebuild_url(&mut self) {
41        let endpoint = self.endpoint_config.url(&self.api_base, paths::VOICE_LIST);
42        let mut params: Vec<(&str, String)> = Vec::new();
43        if let Some(ref n) = self.query.voice_name {
44            params.push(("voiceName", n.clone()));
45        }
46        if let Some(ref t) = self.query.voice_type {
47            params.push(("voiceType", t.as_str().to_string()));
48        }
49        self.url = build_query(&endpoint, params);
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 validate(&self) -> ZaiResult<()> {
70        // No required params; URL already built. Optionally, validate query formats
71        // here.
72        Ok(())
73    }
74
75    pub async fn send(&self) -> ZaiResult<super::response::VoiceListResponse> {
76        self.validate()?;
77        let resp = self.get().await?;
78        let parsed = parse_typed_response::<super::response::VoiceListResponse>(resp).await?;
79        Ok(parsed)
80    }
81
82    pub fn with_query(mut self, q: VoiceListQuery) -> Self {
83        self.query = q;
84        self.rebuild_url();
85        self
86    }
87}
88
89impl HttpClient for VoiceListRequest {
90    type Body = ();
91    type ApiUrl = String;
92    type ApiKey = String;
93
94    fn api_url(&self) -> &Self::ApiUrl {
95        &self.url
96    }
97    fn api_key(&self) -> &Self::ApiKey {
98        &self.key
99    }
100    fn body(&self) -> &Self::Body {
101        &self._body
102    }
103    fn http_config(&self) -> Arc<HttpClientConfig> {
104        Arc::clone(&self.http_config)
105    }
106}