use super::types::DocumentImageListResponse;
use crate::client::http::HttpClient;
pub struct DocumentImageListRequest {
pub key: String,
url: String,
}
impl DocumentImageListRequest {
pub fn new(key: String, document_id: impl AsRef<str>) -> Self {
let url = format!(
"https://open.bigmodel.cn/api/llm-application/open/document/slice/image_list/{}",
document_id.as_ref()
);
Self { key, url }
}
pub async fn send(&self) -> crate::ZaiResult<DocumentImageListResponse> {
let resp: reqwest::Response = self.post().await?;
let parsed = resp.json::<DocumentImageListResponse>().await?;
Ok(parsed)
}
}
impl HttpClient for DocumentImageListRequest {
type Body = (); type ApiUrl = String;
type ApiKey = String;
fn api_url(&self) -> &Self::ApiUrl {
&self.url
}
fn api_key(&self) -> &Self::ApiKey {
&self.key
}
fn body(&self) -> &Self::Body {
&()
}
fn post(
&self,
) -> impl std::future::Future<Output = crate::ZaiResult<reqwest::Response>> + Send {
let url = self.url.clone();
let key = self.key.clone();
async move {
let resp = reqwest::Client::new()
.post(url)
.bearer_auth(key)
.send()
.await?;
let status = resp.status();
if status.is_success() {
return Ok(resp);
}
let text = resp.text().await.unwrap_or_default();
#[derive(serde::Deserialize)]
struct ErrEnv {
error: ErrObj,
}
#[derive(serde::Deserialize)]
struct ErrObj {
_code: serde_json::Value,
message: String,
}
if let Ok(parsed) = serde_json::from_str::<ErrEnv>(&text) {
return Err(crate::client::error::ZaiError::from_api_response(
status.as_u16(),
0,
parsed.error.message,
));
}
Err(crate::client::error::ZaiError::from_api_response(
status.as_u16(),
0,
text,
))
}
}
}