Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 136 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ flate2 = "1"
tar = "0.4"
semver = "1"
sqlformat = "0.5.0"
sysinfo = { version = "0.38.4", default-features = false, features = ["system"] }

[dev-dependencies]
mockito = "1"
Expand Down
46 changes: 46 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct ApiClient {
api_key: String,
pub api_url: String,
workspace_id: Option<String>,
session_id: Option<String>,
}

impl ApiClient {
Expand Down Expand Up @@ -35,6 +36,13 @@ impl ApiClient {
api_key,
api_url: profile_config.api_url.to_string(),
workspace_id: workspace_id.map(String::from),
session_id: std::env::var("HOTDATA_SESSION").ok().or_else(|| {
if crate::sessions::find_session_run_ancestor().is_some() {
Comment thread
pthurlow marked this conversation as resolved.
eprintln!("error: session has been lost -- restart the process");
std::process::exit(1);
}
profile_config.session
}),
}
}

Expand All @@ -48,6 +56,9 @@ impl ApiClient {
if let Some(ref ws) = self.workspace_id {
headers.push(("X-Workspace-Id", ws.clone()));
}
if let Some(ref sid) = self.session_id {
headers.push(("X-Session-Id", sid.clone()));
}
headers
}

Expand All @@ -63,6 +74,9 @@ impl ApiClient {
if let Some(ref ws) = self.workspace_id {
req = req.header("X-Workspace-Id", ws);
}
if let Some(ref sid) = self.session_id {
req = req.header("X-Session-Id", sid);
}
req
}

Expand Down Expand Up @@ -235,6 +249,38 @@ impl ApiClient {
}
}


/// PATCH request with JSON body, returns parsed response.
pub fn patch<T: DeserializeOwned>(&self, path: &str, body: &serde_json::Value) -> T {
let url = format!("{}{path}", self.api_url);
self.log_request("PATCH", &url, Some(body));

let resp = match self.build_request(reqwest::Method::PATCH, &url)
.json(body)
.send()
{
Ok(r) => r,
Err(e) => {
eprintln!("error connecting to API: {e}");
std::process::exit(1);
}
};

let (status, resp_body) = util::debug_response(resp);
if !status.is_success() {
eprintln!("{}", util::api_error(resp_body).red());
std::process::exit(1);
}

match serde_json::from_str(&resp_body) {
Ok(v) => v,
Err(e) => {
eprintln!("error parsing response: {e}");
std::process::exit(1);
}
}
}

/// POST with a custom request body (for file uploads). Returns raw status and body.
pub fn post_body<R: std::io::Read + Send + 'static>(
&self,
Expand Down
Loading
Loading