From df98df739453c786002460c7532cab569a5942d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Tue, 6 Jun 2023 20:20:31 +0200 Subject: [PATCH] feat(cli): request the whole obj listing for `get` --- cli/src/main.rs | 65 ++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 9e47662..561eef9 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -229,29 +229,36 @@ async fn main() -> Result<()> { attribute, format, } => { - let api_url = url.join("/api/query")?; + let response = if let Some(attribute) = attribute { + let api_url = url.join("/api/query")?; - let entity = match entity { - entity if entity.starts_with('=') => hash_path(&entity[1..])?.to_string(), - entity if entity.starts_with("http") => Address::Url(entity.parse()?).to_string(), - _ => entity, + let entity = match entity { + entity if entity.starts_with('=') => hash_path(&entity[1..])?.to_string(), + entity if entity.starts_with("http") => { + Address::Url(entity.parse()?).to_string() + } + _ => entity, + }; + + let query = format!("(matches @{} \"{}\" ?)", entity, attribute); + + debug!("Querying \"{}\": {}", api_url, query); + REQWEST_ASYNC_CLIENT + .post(api_url) + .body(query) + .send() + .await? + } else { + let entity = match entity { + entity if entity.starts_with('=') => hash_path(&entity[1..])?.to_string(), + _ => todo!("Only GETting blobs (files) is implemented."), + }; + let api_url = url.join(&format!("/api/obj/{entity}"))?; + + debug!("Getting object \"{}\" from {}", entity, api_url); + REQWEST_ASYNC_CLIENT.get(api_url).send().await? }; - let query = format!( - "(matches @{} {} ?)", - entity, - attribute - .map(|a| format!("\"{a}\"")) - .unwrap_or("?".to_string()) - ); - - debug!("Querying \"{}\": {}", api_url, query); - let response = REQWEST_ASYNC_CLIENT - .post(api_url) - .body(query) - .send() - .await?; - response.error_for_status_ref()?; print_response_entries(response, format).await?; @@ -515,15 +522,23 @@ async fn main() -> Result<()> { } } +type Entries = HashMap; + async fn print_response_entries(response: reqwest::Response, format: OutputFormat) -> Result<()> { match format { OutputFormat::Json | OutputFormat::Raw => println!("{}", response.text().await?), OutputFormat::Tsv => { - let mut entries = response - .json::>() - .await? - .into_iter() - .peekable(); + let mut entries = if response.url().path().contains("/obj/") { + #[derive(serde::Deserialize)] + struct ObjResponse { + entries: Entries, + } + response.json::().await?.entries + } else { + response.json::().await? + } + .into_iter() + .peekable(); if entries.peek().is_some() { eprintln!("entity\tattribute\tvalue\ttimestamp\tprovenance");