Skip to main content

zai_rs/batches/
retrieve.rs

1use std::sync::Arc;
2
3use super::types::BatchItem;
4use crate::{
5    ZaiResult,
6    client::{
7        endpoints::{ApiBase, EndpointConfig, join_url, paths},
8        http::{HttpClient, HttpClientConfig, parse_typed_response},
9    },
10};
11
12/// Retrieve a batch task by ID (GET /paas/v4/batches/{batch_id})
13pub struct BatchesRetrieveRequest {
14    /// Bearer API key
15    pub key: String,
16    /// Full URL with path parameter bound
17    url: String,
18    endpoint_config: EndpointConfig,
19    api_base: ApiBase,
20    batch_id: String,
21    http_config: Arc<HttpClientConfig>,
22    /// No body for GET
23    _body: (),
24}
25
26impl BatchesRetrieveRequest {
27    /// Create a new retrieve request with required path parameter `batch_id`.
28    pub fn new(key: String, batch_id: impl AsRef<str>) -> Self {
29        // Batch IDs are expected to be safe; if special chars appear, consider
30        // encoding.
31        let batch_id = batch_id.as_ref().to_string();
32        let endpoint_config = EndpointConfig::default();
33        let api_base = ApiBase::PaasV4;
34        let url = endpoint_config.url(&api_base, &join_url(paths::BATCHES, &batch_id));
35        Self {
36            key,
37            url,
38            endpoint_config,
39            api_base,
40            batch_id,
41            http_config: Arc::new(HttpClientConfig::default()),
42            _body: (),
43        }
44    }
45
46    fn rebuild_url(&mut self) {
47        self.url = self
48            .endpoint_config
49            .url(&self.api_base, &join_url(paths::BATCHES, &self.batch_id));
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    /// Send request and parse typed response as a single BatchItem
70    pub async fn send(&self) -> ZaiResult<BatchesRetrieveResponse> {
71        let resp: reqwest::Response = self.get().await?;
72        let parsed = parse_typed_response::<BatchesRetrieveResponse>(resp).await?;
73        Ok(parsed)
74    }
75}
76
77impl HttpClient for BatchesRetrieveRequest {
78    type Body = ();
79    type ApiUrl = String;
80    type ApiKey = String;
81
82    fn api_url(&self) -> &Self::ApiUrl {
83        &self.url
84    }
85    fn api_key(&self) -> &Self::ApiKey {
86        &self.key
87    }
88    fn body(&self) -> &Self::Body {
89        &self._body
90    }
91
92    fn http_config(&self) -> Arc<HttpClientConfig> {
93        Arc::clone(&self.http_config)
94    }
95}
96
97/// Response type: a single Batch object
98pub type BatchesRetrieveResponse = BatchItem;