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,
format,
} => {
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 if entity.starts_with("http") => {
Address::Url(entity.parse()?).to_string()
}
_ => entity,
};
let query = format!(
"(matches @{} {} ?)",
entity,
attribute
.map(|a| format!("\"{a}\""))
.unwrap_or("?".to_string())
);
let query = format!("(matches @{} \"{}\" ?)", entity, attribute);
debug!("Querying \"{}\": {}", api_url, query);
let response = REQWEST_ASYNC_CLIENT
REQWEST_ASYNC_CLIENT
.post(api_url)
.body(query)
.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()?;
@ -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<()> {
match format {
OutputFormat::Json | OutputFormat::Raw => println!("{}", response.text().await?),
OutputFormat::Tsv => {
let mut entries = response
.json::<HashMap<String, serde_json::Value>>()
.await?
let mut entries = if response.url().path().contains("/obj/") {
#[derive(serde::Deserialize)]
struct ObjResponse {
entries: Entries,
}
response.json::<ObjResponse>().await?.entries
} else {
response.json::<Entries>().await?
}
.into_iter()
.peekable();