Skip to main content

zai_rs/model/async_chat_get/
data.rs

1use std::marker::PhantomData;
2
3use super::super::traits::*;
4use crate::client::http::HttpClient;
5pub struct AsyncChatGetRequest<N>
6where
7    N: ModelName + AsyncChat,
8{
9    pub key: String,
10    url: String,
11    // Empty body placeholder to satisfy HttpClient::Body
12    _body: (),
13    // Phantom placeholder to carry generic N for compile-time constraints
14    _marker: PhantomData<N>,
15}
16
17impl<N> AsyncChatGetRequest<N>
18where
19    N: ModelName + AsyncChat,
20{
21    pub fn new(_model: N, task_id: String, key: String) -> Self {
22        let url = format!(
23            "https://open.bigmodel.cn/api/paas/v4/async-result/{}",
24            task_id
25        );
26        Self {
27            key,
28            url,
29            _body: (),
30            _marker: PhantomData,
31        }
32    }
33
34    pub fn validate(&self) -> crate::ZaiResult<()> {
35        if self.url.trim().is_empty() {
36            return Err(crate::client::error::ZaiError::ApiError {
37                code: 1200,
38                message: "empty URL".to_string(),
39            });
40        }
41        Ok(())
42    }
43
44    pub async fn send(
45        &self,
46    ) -> crate::ZaiResult<crate::model::chat_base_response::ChatCompletionResponse> {
47        self.validate()?;
48
49        let resp = self.get().await?;
50
51        let parsed = resp
52            .json::<crate::model::chat_base_response::ChatCompletionResponse>()
53            .await?;
54
55        Ok(parsed)
56    }
57}
58
59impl<N> HttpClient for AsyncChatGetRequest<N>
60where
61    N: ModelName + AsyncChat,
62{
63    type Body = ();
64    type ApiUrl = String;
65    type ApiKey = String;
66
67    fn api_url(&self) -> &Self::ApiUrl {
68        &self.url
69    }
70
71    fn api_key(&self) -> &Self::ApiKey {
72        &self.key
73    }
74
75    fn body(&self) -> &Self::Body {
76        &self._body
77    }
78}