Skip to main content

zai_rs/model/text_tokenizer/
data.rs

1use std::sync::Arc;
2
3use super::{
4    request::{TokenizerBody, TokenizerMessage, TokenizerModel},
5    response::TokenizerResponse,
6};
7use crate::client::{
8    endpoints::{ApiBase, EndpointConfig, paths},
9    http::{HttpClient, HttpClientConfig, parse_typed_response},
10};
11
12/// Text Tokenizer request client (JSON POST)
13pub struct TokenizerRequest {
14    pub key: String,
15    url: String,
16    endpoint_config: EndpointConfig,
17    api_base: ApiBase,
18    http_config: Arc<HttpClientConfig>,
19    body: TokenizerBody,
20}
21
22impl TokenizerRequest {
23    pub fn new(key: String, model: TokenizerModel, messages: Vec<TokenizerMessage>) -> Self {
24        let endpoint_config = EndpointConfig::default();
25        let api_base = ApiBase::PaasV4;
26        let url = endpoint_config.url(&api_base, paths::TOKENIZER);
27        let body = TokenizerBody::new(model, messages);
28        Self {
29            key,
30            url,
31            endpoint_config,
32            api_base,
33            http_config: Arc::new(HttpClientConfig::default()),
34            body,
35        }
36    }
37
38    fn rebuild_url(&mut self) {
39        self.url = self.endpoint_config.url(&self.api_base, paths::TOKENIZER);
40    }
41
42    pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
43        self.api_base = ApiBase::Custom(base.into());
44        self.rebuild_url();
45        self
46    }
47
48    pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
49        self.endpoint_config = endpoint_config;
50        self.rebuild_url();
51        self
52    }
53
54    pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
55        self.http_config = Arc::new(config);
56        self
57    }
58
59    pub fn with_request_id(mut self, v: impl Into<String>) -> Self {
60        self.body = self.body.with_request_id(v);
61        self
62    }
63    pub fn with_user_id(mut self, v: impl Into<String>) -> Self {
64        self.body = self.body.with_user_id(v);
65        self
66    }
67
68    /// Optional: validate constraints before sending
69    pub fn validate(&self) -> crate::ZaiResult<()> {
70        if self.body.messages.is_empty() {
71            return Err(crate::client::error::ZaiError::ApiError {
72                code: 1200,
73                message: "messages must not be empty".to_string(),
74            });
75        }
76        Ok(())
77    }
78
79    /// Send the request and parse typed response.
80    /// Automatically runs `validate()` before sending.
81    pub async fn send(&self) -> crate::ZaiResult<TokenizerResponse> {
82        self.validate()?;
83        let resp: reqwest::Response = self.post().await?;
84        let parsed = parse_typed_response::<TokenizerResponse>(resp).await?;
85        Ok(parsed)
86    }
87
88    #[deprecated(note = "Use send() instead")]
89    /// Deprecated: use `send()`.
90    pub async fn execute(&self) -> crate::ZaiResult<TokenizerResponse> {
91        self.send().await
92    }
93}
94
95impl HttpClient for TokenizerRequest {
96    type Body = TokenizerBody;
97    type ApiUrl = String;
98    type ApiKey = String;
99
100    fn api_url(&self) -> &Self::ApiUrl {
101        &self.url
102    }
103    fn api_key(&self) -> &Self::ApiKey {
104        &self.key
105    }
106    fn body(&self) -> &Self::Body {
107        &self.body
108    }
109    fn http_config(&self) -> Arc<HttpClientConfig> {
110        Arc::clone(&self.http_config)
111    }
112}