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,28 +229,35 @@ async fn main() -> Result<()> {
attribute, attribute,
format, format,
} => { } => {
let response = if let Some(attribute) = attribute {
let api_url = url.join("/api/query")?; 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") => {
Address::Url(entity.parse()?).to_string()
}
_ => entity, _ => entity,
}; };
let query = format!( let query = format!("(matches @{} \"{}\" ?)", entity, attribute);
"(matches @{} {} ?)",
entity,
attribute
.map(|a| format!("\"{a}\""))
.unwrap_or("?".to_string())
);
debug!("Querying \"{}\": {}", api_url, query); debug!("Querying \"{}\": {}", api_url, query);
let response = REQWEST_ASYNC_CLIENT REQWEST_ASYNC_CLIENT
.post(api_url) .post(api_url)
.body(query) .body(query)
.send() .send()
.await?; .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?
};
response.error_for_status_ref()?; response.error_for_status_ref()?;
@ -515,13 +522,21 @@ 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 {
entries: Entries,
}
response.json::<ObjResponse>().await?.entries
} else {
response.json::<Entries>().await?
}
.into_iter() .into_iter()
.peekable(); .peekable();