Compare commits

...

2 commits

Author SHA1 Message Date
6261b3fb03
fix: none-version, multipart.
Improve the multipart inputs, add some logging.
2024-10-30 03:25:36 +01:00
1c60d426c9
fix: make optional inputs actually optional. 2024-10-30 01:22:40 +01:00
4 changed files with 117 additions and 76 deletions

98
Cargo.lock generated
View file

@ -107,7 +107,6 @@ dependencies = [
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
"serde_yaml",
"sha3", "sha3",
"tokio", "tokio",
"url", "url",
@ -142,12 +141,6 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -178,6 +171,29 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-io"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-macro"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7"
[[package]] [[package]]
name = "futures-task" name = "futures-task"
version = "0.3.31" version = "0.3.31"
@ -191,9 +207,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]
@ -223,12 +244,6 @@ version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "hashbrown"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.9" version = "0.3.9"
@ -347,16 +362,6 @@ dependencies = [
"unicode-normalization", "unicode-normalization",
] ]
[[package]]
name = "indexmap"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.10.1" version = "2.10.1"
@ -615,10 +620,12 @@ dependencies = [
"sync_wrapper", "sync_wrapper",
"tokio", "tokio",
"tokio-rustls", "tokio-rustls",
"tokio-util",
"tower-service", "tower-service",
"url", "url",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"wasm-streams",
"web-sys", "web-sys",
"webpki-roots", "webpki-roots",
"windows-registry", "windows-registry",
@ -741,19 +748,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_yaml"
version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]] [[package]]
name = "sha3" name = "sha3"
version = "0.10.8" version = "0.10.8"
@ -900,6 +894,19 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "tokio-util"
version = "0.7.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a"
dependencies = [
"bytes",
"futures-core",
"futures-sink",
"pin-project-lite",
"tokio",
]
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.3" version = "0.3.3"
@ -964,12 +971,6 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]] [[package]]
name = "untrusted" name = "untrusted"
version = "0.9.0" version = "0.9.0"
@ -1076,6 +1077,19 @@ version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d"
[[package]]
name = "wasm-streams"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65"
dependencies = [
"futures-util",
"js-sys",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
]
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.72" version = "0.3.72"

View file

@ -6,10 +6,9 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
hex = "0" hex = "0"
reqwest = { version = "0", features = ["json", "multipart", "rustls-tls"], default-features=false } reqwest = { version = "0", features = ["json", "multipart", "rustls-tls", "stream"], default-features=false }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
serde_yaml = "0"
sha3 = "0" sha3 = "0"
tokio = { version = "1", features = ["fs", "macros", "rt-multi-thread"] } tokio = { version = "1", features = ["fs", "macros", "rt-multi-thread"] }
url = { version = "2", features = ["serde"] } url = { version = "2", features = ["serde"] }

View file

@ -58,21 +58,21 @@ async fn build_request(
request_builder request_builder
.form(&serde_json::from_slice::<HashMap<String, String>>(&buffer)?) .form(&serde_json::from_slice::<HashMap<String, String>>(&buffer)?)
} }
model::Body::Multipart(fbvec) => { model::Body::Multipart(mp) => {
let mut form = reqwest::multipart::Form::new(); let mut form = reqwest::multipart::Form::new();
for mb in fbvec { for (name, path) in &mp.files {
let mb_ = srcpath_.to_owned().join(mb); let mb_: PathBuf = srcpath_.to_owned().join(path);
form = form.file(name.clone(), mb_).await?;
}
if let Some(mp_form) = &mp.form {
let mut buffer = Vec::<u8>::new(); let mut buffer = Vec::<u8>::new();
let mut file = tokio::fs::File::open(&mb_).await?; let mut file = tokio::fs::File::open(srcpath_.to_owned().join(mp_form)).await?;
let read = file.read_buf(&mut buffer).await?; let read = file.read_buf(&mut buffer).await?;
buffer.truncate(read); buffer.truncate(read);
let name = mb_ let form_hash_map: HashMap<String, String> = serde_json::from_slice(&buffer)?;
.file_name() for (k, v) in form_hash_map.into_iter() {
.ok_or_else(|| anyhow!("Failed to get file name"))? form = form.text(k, v);
.to_str() }
.ok_or_else(|| anyhow!("Failed to get file name"))?
.to_string();
form = form.part(name, reqwest::multipart::Part::bytes(buffer));
} }
request_builder.multipart(form) request_builder.multipart(form)
} }
@ -85,11 +85,17 @@ async fn build_request(
} }
async fn check(input: model::CheckInput) -> Result<model::CheckOutput> { async fn check(input: model::CheckInput) -> Result<model::CheckOutput> {
eprintln!("Calling {:?} {}", &input.source.method, &input.source.url);
let (client, request) = build_request(&input.source, None).await?; let (client, request) = build_request(&input.source, None).await?;
let mut response = client.execute(request).await?; let mut response = client.execute(request).await?;
eprintln!("Response code {}", &response.status());
eprintln!("Headers {:?}", &response.headers());
Ok(model::CheckOutput( Ok(model::CheckOutput(
match input.source.version_check_method { match input.source.version_check_method {
model::VersionCheckMethod::None => vec![model::Version::None], model::VersionCheckMethod::None => vec![model::Version::None("none".to_string())],
model::VersionCheckMethod::Hash => { model::VersionCheckMethod::Hash => {
let mut hasher = sha3::Sha3_256::new(); let mut hasher = sha3::Sha3_256::new();
while let Some(chunk) = response.chunk().await? { while let Some(chunk) = response.chunk().await? {
@ -115,12 +121,18 @@ async fn in_(input: model::InInput, dest: &Path) -> Result<model::InOutput> {
version: input.version, version: input.version,
params: input.params, params: input.params,
}; };
eprintln!("Calling {:?} {}", &input.source.method, &input.source.url);
let (client, request) = build_request(&input.source, None).await?; let (client, request) = build_request(&input.source, None).await?;
let mut response = client.execute(request).await?; let mut response = client.execute(request).await?;
eprintln!("Response code {}", &response.status());
eprintln!("Headers {:?}", &response.headers());
let dest_code = dest.to_owned().join("code"); let dest_code = dest.to_owned().join("code");
let dest_headers = dest.to_owned().join("headers"); let dest_headers = dest.to_owned().join("headers");
let dest_bytes = dest.to_owned().join("output"); let dest_bytes = dest.to_owned().join("body");
let mut dest_code_writer = tokio::io::BufWriter::new(tokio::fs::File::create(dest_code).await?); let mut dest_code_writer = tokio::io::BufWriter::new(tokio::fs::File::create(dest_code).await?);
dest_code_writer dest_code_writer
@ -158,7 +170,7 @@ async fn in_(input: model::InInput, dest: &Path) -> Result<model::InOutput> {
let out = model::InOutput { let out = model::InOutput {
version: match input.source.version_check_method { version: match input.source.version_check_method {
model::VersionCheckMethod::None => model::Version::None, model::VersionCheckMethod::None => model::Version::None("none".to_string()),
model::VersionCheckMethod::Hash => model::Version::Hash(hex::encode(hash)), model::VersionCheckMethod::Hash => model::Version::Hash(hex::encode(hash)),
model::VersionCheckMethod::Header(headername) => { model::VersionCheckMethod::Header(headername) => {
let headervalue: &HeaderValue = response let headervalue: &HeaderValue = response
@ -170,11 +182,13 @@ async fn in_(input: model::InInput, dest: &Path) -> Result<model::InOutput> {
}, },
metadata: vec![], metadata: vec![],
}; };
if out.version != input.version { if let Some(input_version) = &input.version {
return Err(anyhow!(format!( if out.version != *input_version {
"Did not get correct version of resource, requested '{:?}', got '{:?}'", return Err(anyhow!(format!(
input.version, out.version "Did not get correct version of resource, requested '{:?}', got '{:?}'",
))); input.version, out.version
)));
}
} }
Ok(out) Ok(out)
} }
@ -184,18 +198,20 @@ async fn out(input: model::OutInput, src: &Path) -> Result<model::OutOutput> {
source: input.source.merge_params(&input.params), source: input.source.merge_params(&input.params),
params: input.params, params: input.params,
}; };
eprintln!("Calling {:?} {}", &input.source.method, &input.source.url);
let (client, request) = build_request(&input.source, Some(src)).await?; let (client, request) = build_request(&input.source, Some(src)).await?;
let response = client.execute(request).await?; let response = client.execute(request).await?;
response.error_for_status()?; let err = response.error_for_status_ref().map(|_| ());
let version = check(model::CheckInput {
source: input.source.clone(), eprintln!("Response code {}", &response.status());
version: model::Version::None, eprintln!("Headers {:?}", &response.headers());
}) eprintln!("Body {:?}", response.text().await?);
.await?
.0[0] err?;
.clone();
Ok(model::OutOutput { Ok(model::OutOutput {
version, version: model::Version::None("none".to_string()),
metadata: vec![], metadata: vec![],
}) })
} }

View file

@ -1,5 +1,6 @@
use anyhow::Result; use anyhow::Result;
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
@ -54,12 +55,20 @@ pub enum Auth {
Bearer(String), Bearer(String),
} }
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct BodyMultipart {
#[serde(default)]
pub files: HashMap<String, PathBuf>,
#[serde(default)]
pub form: Option<PathBuf>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum Body { pub enum Body {
Raw(PathBuf), Raw(PathBuf),
Form(PathBuf), Form(PathBuf),
Multipart(Vec<PathBuf>), Multipart(BodyMultipart),
} }
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)] #[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
@ -129,7 +138,7 @@ impl Source {
pub enum Version { pub enum Version {
Header(String), Header(String),
Hash(String), Hash(String),
None, None(String),
} }
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@ -150,19 +159,22 @@ pub struct Params {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct CheckInput { pub struct CheckInput {
pub source: Source, pub source: Source,
pub version: Version, #[serde(default)]
pub version: Option<Version>,
} }
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct InInput { pub struct InInput {
pub source: Source, pub source: Source,
pub version: Version, pub version: Option<Version>,
#[serde(default)]
pub params: Params, pub params: Params,
} }
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct OutInput { pub struct OutInput {
pub source: Source, pub source: Source,
#[serde(default)]
pub params: Params, pub params: Params,
} }