use crate::error::Error;
#[derive(Debug, Clone)]
pub struct ViewerClient {
http: reqwest::Client,
address: String,
signature: Option<String>,
}
impl ViewerClient {
pub fn new(address: String, signature: Option<String>) -> Self {
Self {
http: reqwest::Client::new(),
address,
signature,
}
}
pub async fn send(
&self,
path: &str,
body: &serde_json::Value,
) -> Result<(u16, serde_json::Value), Error> {
if !path.starts_with('/') {
return Err(Error::ViewerPathMissingSlash(path.to_string()));
}
let url = format!("{}{}", self.address.trim_end_matches('/'), path);
let mut req = self
.http
.post(&url)
.header("Content-Type", "application/json")
.json(body);
if let Some(sig) = &self.signature {
req = req.header("X-VIEWER-SIGNATURE", sig);
}
let response = req
.send()
.await
.map_err(|e| Error::ViewerSendHttp(e.to_string()))?;
let status = response.status().as_u16();
let text = response.text().await.unwrap_or_default();
let body = serde_json::from_str::<serde_json::Value>(&text)
.unwrap_or(serde_json::Value::String(text));
Ok((status, body))
}
}