Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix href handling #450

Merged
merged 1 commit into from
Oct 9, 2024
Merged
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
26 changes: 15 additions & 11 deletions cli/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::Read;

use crate::{options::Options, Error, Result};
use stac::{Format, Value};
use std::io::Read;
use url::Url;

/// The input to a CLI run.
#[derive(Debug, Default)]
Expand Down Expand Up @@ -59,20 +59,24 @@ impl Input {
/// Gets a serde_json value from the input.
pub(crate) async fn get_json(&self) -> Result<serde_json::Value> {
if let Some(href) = self.href.as_deref() {
let (object_store, path) =
object_store::parse_url_opts(&href.parse()?, self.options.iter())?;
let get_result = object_store.get(&path).await?;
let bytes = get_result.bytes().await?;
let format = self
.format
.or_else(|| Format::infer_from_href(href))
.unwrap_or_default();
match format {
Format::Json(..) => serde_json::from_slice(&bytes).map_err(Error::from),
_ => {
let value: Value = format.from_bytes(bytes)?;
serde_json::to_value(value).map_err(Error::from)
if let Ok(url) = Url::parse(href) {
let (object_store, path) = object_store::parse_url_opts(&url, self.options.iter())?;
let get_result = object_store.get(&path).await?;
let bytes = get_result.bytes().await?;
match format {
Format::Json(..) => serde_json::from_slice(&bytes).map_err(Error::from),
_ => {
let value: Value = format.from_bytes(bytes)?;
serde_json::to_value(value).map_err(Error::from)
}
}
} else {
let value: Value = format.from_path(href)?;
serde_json::to_value(value).map_err(Error::from)
}
} else {
let mut buf = Vec::new();
Expand Down
Loading