1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Synchronous-response HTTP client for the viewer's HTTP server.
//!
//! Bypasses the SDK's fire-and-forget viewer path because callers
//! (`viewer send`) want to see the response. Constructed in
//! [`crate::context::Context::viewer_client`], which resolves the
//! address (config `viewer.address`, else the `viewer spawn` flow's
//! published lock URL) and the signature (env `VIEWER_SIGNATURE`,
//! else config `viewer.signature`).
use crate::error::Error;
#[derive(Debug, Clone)]
pub struct ViewerClient {
http: reqwest::Client,
/// Base URL, e.g. `http://127.0.0.1:51234`.
address: String,
/// `X-VIEWER-SIGNATURE` header value, when configured.
signature: Option<String>,
}
impl ViewerClient {
pub fn new(address: String, signature: Option<String>) -> Self {
Self {
http: reqwest::Client::new(),
address,
signature,
}
}
/// POST `body` as JSON to `{address}{path}`. `path` must start
/// with `/` ([`Error::ViewerPathMissingSlash`]). A non-2xx status
/// is NOT an error — the response is returned as-is; the body is
/// parsed as JSON, falling back to `Value::String(raw_text)`.
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))
}
}