-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresults.rs
More file actions
113 lines (104 loc) · 3.46 KB
/
results.rs
File metadata and controls
113 lines (104 loc) · 3.46 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::config;
use serde::Deserialize;
use serde_json::Value;
#[derive(Deserialize)]
#[allow(dead_code)]
struct ResultResponse {
result_id: String,
status: Option<String>,
columns: Vec<String>,
#[serde(default)]
nullable: Vec<bool>,
rows: Vec<Vec<Value>>,
row_count: u64,
#[serde(default)]
execution_time_ms: u64,
}
fn value_to_string(v: &Value) -> String {
match v {
Value::Null => "NULL".to_string(),
Value::Bool(b) => b.to_string(),
Value::Number(n) => n.to_string(),
Value::String(s) => s.clone(),
Value::Array(_) | Value::Object(_) => v.to_string(),
}
}
pub fn get(result_id: &str, workspace_id: &str, format: &str) {
let profile_config = match config::load("default") {
Ok(c) => c,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
let api_key = match &profile_config.api_key {
Some(key) if key != "PLACEHOLDER" => key.clone(),
_ => {
eprintln!("error: not authenticated. Run 'hotdata auth login' to log in.");
std::process::exit(1);
}
};
let url = format!("{}/results/{result_id}", profile_config.api_url);
let client = reqwest::blocking::Client::new();
let resp = match client
.get(&url)
.header("Authorization", format!("Bearer {api_key}"))
.header("X-Workspace-Id", workspace_id)
.send()
{
Ok(r) => r,
Err(e) => {
eprintln!("error connecting to API: {e}");
std::process::exit(1);
}
};
if !resp.status().is_success() {
let body = resp.text().unwrap_or_default();
let message = serde_json::from_str::<Value>(&body)
.ok()
.and_then(|v| v["error"]["message"].as_str().map(str::to_string))
.unwrap_or(body);
use crossterm::style::Stylize;
eprintln!("{}", message.red());
std::process::exit(1);
}
let result: ResultResponse = match resp.json() {
Ok(r) => r,
Err(e) => {
eprintln!("error parsing response: {e}");
std::process::exit(1);
}
};
match format {
"json" => {
let out = serde_json::json!({
"result_id": result.result_id,
"columns": result.columns,
"rows": result.rows,
"row_count": result.row_count,
"execution_time_ms": result.execution_time_ms,
});
println!("{}", serde_json::to_string_pretty(&out).unwrap());
}
"csv" => {
println!("{}", result.columns.join(","));
for row in &result.rows {
let cells: Vec<String> = row.iter().map(|v| {
let s = value_to_string(v);
if s.contains(',') || s.contains('"') || s.contains('\n') {
format!("\"{}\"", s.replace('"', "\"\""))
} else {
s
}
}).collect();
println!("{}", cells.join(","));
}
}
"table" => {
crate::table::print_json(&result.columns, &result.rows);
use crossterm::style::Stylize;
eprintln!("{}", format!("\n{} row{} ({} ms) [result-id: {}]", result.row_count, if result.row_count == 1 { "" } else { "s" }, result.execution_time_ms, result.result_id).dark_grey());
}
_ => unreachable!(),
}
}