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
66 changes: 59 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 @@ -16,6 +16,7 @@ anstyle = "1.0.13"
clap = { version = "4", features = ["derive"] }
directories = "6"
reqwest = { version = "0.12", features = ["blocking", "json"] }
rayon = "1.10"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
Expand Down
32 changes: 32 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::util;
use crossterm::style::Stylize;
use serde::de::DeserializeOwned;

#[derive(Clone)]
pub struct ApiClient {
client: reqwest::blocking::Client,
api_key: String,
Expand Down Expand Up @@ -156,6 +157,37 @@ impl ApiClient {
}
}

/// GET request; returns `None` on HTTP 404. Other status codes use the same handling as
/// [`Self::get`]. Used when probing many paths where a missing resource is normal.
pub fn get_none_if_not_found<T: DeserializeOwned>(&self, path: &str) -> Option<T> {
let url = format!("{}{path}", self.api_url);
self.log_request("GET", &url, None);

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

let (status, body) = util::debug_response(resp);
if status == reqwest::StatusCode::NOT_FOUND {
return None;
}
if !status.is_success() {
self.fail_response(status, body);
}

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

/// POST request with JSON body, returns parsed response.
pub fn post<T: DeserializeOwned>(&self, path: &str, body: &serde_json::Value) -> T {
let url = format!("{}{path}", self.api_url);
Expand Down
14 changes: 7 additions & 7 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,19 @@ pub enum AuthCommands {

#[derive(Subcommand)]
pub enum IndexesCommands {
/// List indexes on a table
/// List indexes (defaults to the whole workspace; narrow with filters)
List {
/// Connection ID
/// Filter by connection ID
#[arg(long, short = 'c')]
connection_id: String,
connection_id: Option<String>,

/// Schema name
/// Filter by schema name
#[arg(long)]
schema: String,
schema: Option<String>,

/// Table name
/// Filter by table name
#[arg(long)]
table: String,
table: Option<String>,

/// Output format
#[arg(long = "output", short = 'o', default_value = "table", value_parser = ["table", "json", "yaml"])]
Expand Down
Loading
Loading