Skip to main content

zai_rs/model/voice_list/
data.rs

1use url::Url;
2
3use super::request::VoiceListQuery;
4use crate::{ZaiResult, client::http::HttpClient};
5
6/// GET voice list request
7pub struct VoiceListRequest {
8    pub key: String,
9    url: String,
10    // Empty body placeholder to satisfy HttpClient::Body
11    _body: (),
12}
13
14impl VoiceListRequest {
15    pub fn new(key: String) -> Self {
16        let url = "https://open.bigmodel.cn/api/paas/v4/voice/list".to_string();
17        Self {
18            key,
19            url,
20            _body: (),
21        }
22    }
23
24    fn rebuild_url(&mut self, q: &VoiceListQuery) {
25        let mut url = Url::parse("https://open.bigmodel.cn/api/paas/v4/voice/list").unwrap();
26        {
27            let mut pairs = url.query_pairs_mut();
28            if let Some(ref n) = q.voice_name {
29                pairs.append_pair("voiceName", n);
30            }
31            if let Some(ref t) = q.voice_type {
32                pairs.append_pair("voiceType", t.as_str());
33            }
34        }
35        self.url = url.to_string();
36    }
37
38    pub fn validate(&self) -> ZaiResult<()> {
39        // No required params; URL already built. Optionally, validate query formats
40        // here.
41        Ok(())
42    }
43
44    pub async fn send(&self) -> ZaiResult<super::response::VoiceListResponse> {
45        self.validate()?;
46        let resp = self.get().await?;
47        let parsed = resp.json::<super::response::VoiceListResponse>().await?;
48        Ok(parsed)
49    }
50
51    pub fn with_query(mut self, q: VoiceListQuery) -> Self {
52        self.rebuild_url(&q);
53        self
54    }
55}
56
57impl HttpClient for VoiceListRequest {
58    type Body = ();
59    type ApiUrl = String;
60    type ApiKey = String;
61
62    fn api_url(&self) -> &Self::ApiUrl {
63        &self.url
64    }
65    fn api_key(&self) -> &Self::ApiKey {
66        &self.key
67    }
68    fn body(&self) -> &Self::Body {
69        &self._body
70    }
71}