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) -> anyhow::Result<DocumentImageListResponse> {
23        let resp = self.post().await?;
24        let parsed = resp.json::<DocumentImageListResponse>().await?;
25        Ok(parsed)
26    }
27}
28
29impl HttpClient for DocumentImageListRequest {
30    type Body = (); // unused
31    type ApiUrl = String;
32    type ApiKey = String;
33
34    fn api_url(&self) -> &Self::ApiUrl {
35        &self.url
36    }
37    fn api_key(&self) -> &Self::ApiKey {
38        &self.key
39    }
40    fn body(&self) -> &Self::Body {
41        &()
42    }
43
44    // Override POST: send no body, only auth header
45    fn post(&self) -> impl std::future::Future<Output = anyhow::Result<reqwest::Response>> + Send {
46        let url = self.url.clone();
47        let key = self.key.clone();
48        async move {
49            let resp = reqwest::Client::new()
50                .post(url)
51                .bearer_auth(key)
52                .send()
53                .await?;
54
55            let status = resp.status();
56            if status.is_success() {
57                return Ok(resp);
58            }
59
60            // Standard error envelope {"error": { code, message }}
61            let text = resp.text().await.unwrap_or_default();
62            #[derive(serde::Deserialize)]
63            struct ErrEnv {
64                error: ErrObj,
65            }
66            #[derive(serde::Deserialize)]
67            struct ErrObj {
68                code: serde_json::Value,
69                message: String,
70            }
71            if let Ok(parsed) = serde_json::from_str::<ErrEnv>(&text) {
72                return Err(anyhow::anyhow!(
73                    "HTTP {} {} | code={} | message={}",
74                    status.as_u16(),
75                    status.canonical_reason().unwrap_or(""),
76                    parsed.error.code,
77                    parsed.error.message
78                ));
79            }
80            Err(anyhow::anyhow!(
81                "HTTP {} {} | body={}",
82                status.as_u16(),
83                status.canonical_reason().unwrap_or(""),
84                text
85            ))
86        }
87    }
88}