Skip to main content

zai_rs/knowledge/
document_image_list.rs

1use super::types::DocumentImageListResponse;
2use crate::client::http::HttpClient;
3
4/// Retrieve parsed image index-url mapping for a document (POST, no body)
5pub struct DocumentImageListRequest {
6    /// Bearer API key
7    pub key: String,
8    url: String,
9}
10
11impl DocumentImageListRequest {
12    /// Create a new request with the target document id
13    pub fn new(key: String, document_id: impl AsRef<str>) -> Self {
14        let url = format!(
15            "https://open.bigmodel.cn/api/llm-application/open/document/slice/image_list/{}",
16            document_id.as_ref()
17        );
18        Self { key, url }
19    }
20
21    /// Send POST request and parse typed response
22    pub async fn send(&self) -> crate::ZaiResult<DocumentImageListResponse> {
23        let resp: reqwest::Response = self.post().await?;
24
25        let parsed = resp.json::<DocumentImageListResponse>().await?;
26
27        Ok(parsed)
28    }
29}
30
31impl HttpClient for DocumentImageListRequest {
32    type Body = (); // unused
33    type ApiUrl = String;
34    type ApiKey = String;
35
36    fn api_url(&self) -> &Self::ApiUrl {
37        &self.url
38    }
39    fn api_key(&self) -> &Self::ApiKey {
40        &self.key
41    }
42    fn body(&self) -> &Self::Body {
43        &()
44    }
45
46    // Override POST: send no body, only auth header
47    fn post(
48        &self,
49    ) -> impl std::future::Future<Output = crate::ZaiResult<reqwest::Response>> + Send {
50        let url = self.url.clone();
51        let key = self.key.clone();
52        async move {
53            let resp = reqwest::Client::new()
54                .post(url)
55                .bearer_auth(key)
56                .send()
57                .await?;
58
59            let status = resp.status();
60            if status.is_success() {
61                return Ok(resp);
62            }
63
64            // Standard error envelope {"error": { code, message }}
65            let text = resp.text().await.unwrap_or_default();
66            #[derive(serde::Deserialize)]
67            struct ErrEnv {
68                error: ErrObj,
69            }
70            #[derive(serde::Deserialize)]
71            struct ErrObj {
72                _code: serde_json::Value,
73                message: String,
74            }
75
76            if let Ok(parsed) = serde_json::from_str::<ErrEnv>(&text) {
77                return Err(crate::client::error::ZaiError::from_api_response(
78                    status.as_u16(),
79                    0,
80                    parsed.error.message,
81                ));
82            }
83
84            Err(crate::client::error::ZaiError::from_api_response(
85                status.as_u16(),
86                0,
87                text,
88            ))
89        }
90    }
91}