Skip to content

Commit f48c893

Browse files
committed
refactor: migrate raw HTTP to typed SDK (#131)
1 parent 9cd8bc6 commit f48c893

4 files changed

Lines changed: 135 additions & 106 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ sysinfo = { version = "0.38.4", default-features = false, features = ["system"]
5454
self_update = { version = "0.42", default-features = false, features = ["rustls"] }
5555
lzma-rs = "0.3"
5656
tempfile = "3"
57-
urlencoding = "2.1.3"
5857

5958
[dev-dependencies]
6059
mockito = "1"

src/connections.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,8 @@ pub fn resolve_connection_id(api: &Api, name_or_id: &str) -> String {
210210
// matches — prefer it over any stale connection entry with the same name.
211211
if let Some(ws) = api.workspace_id()
212212
&& let Some(active_id) = crate::config::load_current_database("default", ws)
213-
&& let Some(active_db) = none_if_404(
214-
api.get_json::<crate::databases::Database>(&format!("/databases/{active_id}"), &[]),
215-
)
216-
.unwrap_or_else(|e| e.exit())
213+
&& let Some(active_db) =
214+
none_if_404(crate::databases::get_database(api, &active_id)).unwrap_or_else(|e| e.exit())
217215
&& (active_db.default_catalog.as_deref() == Some(name_or_id)
218216
|| active_db.name.as_deref() == Some(name_or_id))
219217
{
@@ -312,47 +310,42 @@ struct CreateResponse {
312310
}
313311

314312
pub fn create(workspace_id: &str, name: &str, source_type: &str, config: &str, format: &str) {
315-
let config_value: serde_json::Value = match serde_json::from_str(config) {
316-
Ok(v) => v,
317-
Err(e) => {
318-
eprintln!("error: --config must be a valid JSON object: {e}");
319-
std::process::exit(1);
320-
}
321-
};
322-
323-
let body = serde_json::json!({
324-
"name": name,
325-
"source_type": source_type,
326-
"config": config_value,
327-
});
313+
let config_map: std::collections::HashMap<String, serde_json::Value> =
314+
match serde_json::from_str(config) {
315+
Ok(v) => v,
316+
Err(e) => {
317+
eprintln!("error: --config must be a valid JSON object: {e}");
318+
std::process::exit(1);
319+
}
320+
};
328321

329322
let api = Api::new(Some(workspace_id));
330323
let is_table = format == "table";
331324

325+
let request = hotdata::models::CreateConnectionRequest::new(
326+
config_map,
327+
name.to_string(),
328+
source_type.to_string(),
329+
);
330+
332331
let spinner = is_table.then(|| crate::util::spinner("Creating connection..."));
333-
let (status, resp_body) = api.post_raw("/connections", &body).unwrap_or_else(|e| {
332+
let resp = block(api.client().connections().create(request)).unwrap_or_else(|e| {
334333
if let Some(s) = &spinner {
335334
s.finish_and_clear();
336335
}
337-
eprintln!("{}", error_text(e));
338-
std::process::exit(1);
336+
e.exit()
339337
});
340338
if let Some(s) = &spinner {
341339
s.finish_and_clear();
342340
}
343341

344-
if !status.is_success() {
345-
use crossterm::style::Stylize;
346-
eprintln!("{}", crate::util::api_error(resp_body).red());
347-
std::process::exit(1);
348-
}
349-
350-
let result: CreateResponse = match serde_json::from_str(&resp_body) {
351-
Ok(v) => v,
352-
Err(e) => {
353-
eprintln!("error parsing response: {e}");
354-
std::process::exit(1);
355-
}
342+
let result = CreateResponse {
343+
id: resp.id,
344+
name: resp.name,
345+
source_type: resp.source_type,
346+
tables_discovered: resp.tables_discovered.max(0) as u64,
347+
discovery_status: resp.discovery_status.to_string(),
348+
discovery_error: resp.discovery_error.flatten(),
356349
};
357350

358351
let health = fetch_health(&api, &result.id, is_table);

0 commit comments

Comments
 (0)