objectiveai_cli/
viewer_client.rs1use crate::error::Error;
11
12#[derive(Debug, Clone)]
13pub struct ViewerClient {
14 http: reqwest::Client,
15 address: String,
17 signature: Option<String>,
19}
20
21impl ViewerClient {
22 pub fn new(address: String, signature: Option<String>) -> Self {
23 Self {
24 http: reqwest::Client::new(),
25 address,
26 signature,
27 }
28 }
29
30 pub async fn send(
35 &self,
36 path: &str,
37 body: &serde_json::Value,
38 ) -> Result<(u16, serde_json::Value), Error> {
39 if !path.starts_with('/') {
40 return Err(Error::ViewerPathMissingSlash(path.to_string()));
41 }
42 let url = format!("{}{}", self.address.trim_end_matches('/'), path);
43 let mut req = self
44 .http
45 .post(&url)
46 .header("Content-Type", "application/json")
47 .json(body);
48 if let Some(sig) = &self.signature {
49 req = req.header("X-VIEWER-SIGNATURE", sig);
50 }
51 let response = req
52 .send()
53 .await
54 .map_err(|e| Error::ViewerSendHttp(e.to_string()))?;
55
56 let status = response.status().as_u16();
57 let text = response.text().await.unwrap_or_default();
58 let body = serde_json::from_str::<serde_json::Value>(&text)
59 .unwrap_or(serde_json::Value::String(text));
60 Ok((status, body))
61 }
62}