zai_rs/model/async_chat_get/
data.rs1use std::{marker::PhantomData, sync::Arc};
2
3use super::super::traits::*;
4use crate::client::{
5 endpoints::{ApiBase, EndpointConfig, join_url, paths},
6 http::{HttpClient, HttpClientConfig, parse_typed_response},
7};
8pub struct AsyncChatGetRequest<N>
9where
10 N: ModelName + AsyncChat,
11{
12 pub key: String,
13 url: String,
14 endpoint_config: EndpointConfig,
15 api_base: ApiBase,
16 task_id: String,
17 http_config: Arc<HttpClientConfig>,
18 _body: (),
20 _marker: PhantomData<N>,
22}
23
24impl<N> AsyncChatGetRequest<N>
25where
26 N: ModelName + AsyncChat,
27{
28 pub fn new(_model: N, task_id: String, key: String) -> Self {
29 let endpoint_config = EndpointConfig::default();
30 let api_base = ApiBase::PaasV4;
31 let path = join_url(paths::ASYNC_RESULT, &task_id);
32 let url = endpoint_config.url(&api_base, &path);
33 Self {
34 key,
35 url,
36 endpoint_config,
37 api_base,
38 task_id,
39 http_config: Arc::new(HttpClientConfig::default()),
40 _body: (),
41 _marker: PhantomData,
42 }
43 }
44
45 fn rebuild_url(&mut self) {
46 let path = join_url(paths::ASYNC_RESULT, &self.task_id);
47 self.url = self.endpoint_config.url(&self.api_base, &path);
48 }
49
50 pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
51 self.api_base = ApiBase::Custom(base_url.into());
52 self.rebuild_url();
53 self
54 }
55
56 pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
57 self.endpoint_config = endpoint_config;
58 self.rebuild_url();
59 self
60 }
61
62 pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
63 self.http_config = Arc::new(config);
64 self
65 }
66
67 pub fn validate(&self) -> crate::ZaiResult<()> {
68 if self.url.trim().is_empty() {
69 return Err(crate::client::error::ZaiError::ApiError {
70 code: 1200,
71 message: "empty URL".to_string(),
72 });
73 }
74 Ok(())
75 }
76
77 pub async fn send(
78 &self,
79 ) -> crate::ZaiResult<crate::model::chat_base_response::ChatCompletionResponse> {
80 self.validate()?;
81
82 let resp = self.get().await?;
83
84 let parsed =
85 parse_typed_response::<crate::model::chat_base_response::ChatCompletionResponse>(resp)
86 .await?;
87
88 Ok(parsed)
89 }
90}
91
92impl<N> HttpClient for AsyncChatGetRequest<N>
93where
94 N: ModelName + AsyncChat,
95{
96 type Body = ();
97 type ApiUrl = String;
98 type ApiKey = String;
99
100 fn api_url(&self) -> &Self::ApiUrl {
101 &self.url
102 }
103
104 fn api_key(&self) -> &Self::ApiKey {
105 &self.key
106 }
107
108 fn body(&self) -> &Self::Body {
109 &self._body
110 }
111
112 fn http_config(&self) -> Arc<HttpClientConfig> {
113 self.http_config.clone()
114 }
115}