handle new content-types from marketplace.get

This commit is contained in:
Aiden McClelland
2022-03-10 16:41:04 -07:00
committed by Aiden McClelland
parent c640749c7c
commit 8b286431e6
3 changed files with 35 additions and 5 deletions

1
backend/Cargo.lock generated
View File

@@ -865,6 +865,7 @@ dependencies = [
"async-trait",
"avahi-sys",
"base32",
"base64",
"basic-cookies",
"bollard",
"chrono",

View File

@@ -51,6 +51,7 @@ avahi-sys = { git = "https://github.com/Start9Labs/avahi-sys", version = "0.10.0
"dynamic",
], optional = true }
base32 = "0.4.0"
base64 = "0.13.0"
basic-cookies = "0.1.4"
bollard = "0.11.0"
chrono = { version = "0.4.19", features = ["serde"] }

View File

@@ -12,15 +12,43 @@ pub fn marketplace() -> Result<(), Error> {
#[command]
pub async fn get(#[arg] url: Url) -> Result<Value, Error> {
let response = reqwest::get(url)
let mut response = reqwest::get(url)
.await
.with_kind(crate::ErrorKind::Network)?;
let status = response.status();
if status.is_success() {
response
match response
.headers_mut()
.remove("Content-Type")
.as_ref()
.and_then(|h| h.to_str().ok())
{
Some("application/json") => response
.json()
.await
.with_kind(crate::ErrorKind::Deserialization)
.with_kind(crate::ErrorKind::Deserialization),
Some("text/plain") => Ok(Value::String(
response
.text()
.await
.with_kind(crate::ErrorKind::Registry)?,
)),
Some(ctype) => Ok(Value::String(format!(
"data:{};base64,{}",
ctype,
base64::encode_config(
&response
.bytes()
.await
.with_kind(crate::ErrorKind::Registry)?,
base64::URL_SAFE
)
))),
_ => Err(Error::new(
eyre!("missing Content-Type"),
crate::ErrorKind::Registry,
)),
}
} else {
let message = response.text().await.with_kind(crate::ErrorKind::Network)?;
Err(Error::new(