feat(cli): request the whole obj listing for `get`

feat/type-attributes
Tomáš Mládek 2023-06-06 20:20:31 +02:00
parent e4e150801a
commit df98df7394
1 changed files with 40 additions and 25 deletions

View File

@ -229,29 +229,36 @@ async fn main() -> Result<()> {
attribute, attribute,
format, 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 { let entity = match entity {
entity if entity.starts_with('=') => hash_path(&entity[1..])?.to_string(), entity if entity.starts_with('=') => hash_path(&entity[1..])?.to_string(),
entity if entity.starts_with("http") => Address::Url(entity.parse()?).to_string(), entity if entity.starts_with("http") => {
_ => entity, 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()?; response.error_for_status_ref()?;
print_response_entries(response, format).await?; print_response_entries(response, format).await?;
@ -515,15 +522,23 @@ async fn main() -> Result<()> {
} }
} }
type Entries = HashMap<String, serde_json::Value>;
async fn print_response_entries(response: reqwest::Response, format: OutputFormat) -> Result<()> { async fn print_response_entries(response: reqwest::Response, format: OutputFormat) -> Result<()> {
match format { match format {
OutputFormat::Json | OutputFormat::Raw => println!("{}", response.text().await?), OutputFormat::Json | OutputFormat::Raw => println!("{}", response.text().await?),
OutputFormat::Tsv => { OutputFormat::Tsv => {
let mut entries = response let mut entries = if response.url().path().contains("/obj/") {
.json::<HashMap<String, serde_json::Value>>() #[derive(serde::Deserialize)]
.await? struct ObjResponse {
.into_iter() entries: Entries,
.peekable(); }
response.json::<ObjResponse>().await?.entries
} else {
response.json::<Entries>().await?
}
.into_iter()
.peekable();
if entries.peek().is_some() { if entries.peek().is_some() {
eprintln!("entity\tattribute\tvalue\ttimestamp\tprovenance"); eprintln!("entity\tattribute\tvalue\ttimestamp\tprovenance");