Skip to content

Commit 584a9a4

Browse files
committed
Add session command feature
1 parent 07d4582 commit 584a9a4

10 files changed

Lines changed: 622 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 136 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ flate2 = "1"
3535
tar = "0.4"
3636
semver = "1"
3737
sqlformat = "0.5.0"
38+
sysinfo = { version = "0.38.4", default-features = false, features = ["system"] }
3839

3940
[dev-dependencies]
4041
mockito = "1"

src/api.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct ApiClient {
88
api_key: String,
99
pub api_url: String,
1010
workspace_id: Option<String>,
11+
session_id: Option<String>,
1112
}
1213

1314
impl ApiClient {
@@ -35,6 +36,13 @@ impl ApiClient {
3536
api_key,
3637
api_url: profile_config.api_url.to_string(),
3738
workspace_id: workspace_id.map(String::from),
39+
session_id: std::env::var("HOTDATA_SESSION").ok().or_else(|| {
40+
if crate::sessions::find_session_run_ancestor().is_some() {
41+
eprintln!("error: session has been lost -- restart the process");
42+
std::process::exit(1);
43+
}
44+
profile_config.session
45+
}),
3846
}
3947
}
4048

@@ -48,6 +56,9 @@ impl ApiClient {
4856
if let Some(ref ws) = self.workspace_id {
4957
headers.push(("X-Workspace-Id", ws.clone()));
5058
}
59+
if let Some(ref sid) = self.session_id {
60+
headers.push(("X-Session-Id", sid.clone()));
61+
}
5162
headers
5263
}
5364

@@ -63,6 +74,9 @@ impl ApiClient {
6374
if let Some(ref ws) = self.workspace_id {
6475
req = req.header("X-Workspace-Id", ws);
6576
}
77+
if let Some(ref sid) = self.session_id {
78+
req = req.header("X-Session-Id", sid);
79+
}
6680
req
6781
}
6882

@@ -235,6 +249,38 @@ impl ApiClient {
235249
}
236250
}
237251

252+
253+
/// PATCH request with JSON body, returns parsed response.
254+
pub fn patch<T: DeserializeOwned>(&self, path: &str, body: &serde_json::Value) -> T {
255+
let url = format!("{}{path}", self.api_url);
256+
self.log_request("PATCH", &url, Some(body));
257+
258+
let resp = match self.build_request(reqwest::Method::PATCH, &url)
259+
.json(body)
260+
.send()
261+
{
262+
Ok(r) => r,
263+
Err(e) => {
264+
eprintln!("error connecting to API: {e}");
265+
std::process::exit(1);
266+
}
267+
};
268+
269+
let (status, resp_body) = util::debug_response(resp);
270+
if !status.is_success() {
271+
eprintln!("{}", util::api_error(resp_body).red());
272+
std::process::exit(1);
273+
}
274+
275+
match serde_json::from_str(&resp_body) {
276+
Ok(v) => v,
277+
Err(e) => {
278+
eprintln!("error parsing response: {e}");
279+
std::process::exit(1);
280+
}
281+
}
282+
}
283+
238284
/// POST with a custom request body (for file uploads). Returns raw status and body.
239285
pub fn post_body<R: std::io::Read + Send + 'static>(
240286
&self,

0 commit comments

Comments
 (0)