Merge pull request #556 from jonathandturner/improve_post

Fix unwraps in post
This commit is contained in:
Jonathan Turner 2019-09-01 19:33:02 +12:00 committed by GitHub
commit bf19dff602
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -151,25 +151,31 @@ pub async fn post(
let registry = registry.clone(); let registry = registry.clone();
let raw_args = raw_args.clone(); let raw_args = raw_args.clone();
if location.starts_with("http:") || location.starts_with("https:") { if location.starts_with("http:") || location.starts_with("https:") {
let login = encode(&format!("{}:{}", user.unwrap(), password.unwrap())); let login = match (user, password) {
(Some(user), Some(password)) => Some(encode(&format!("{}:{}", user, password))),
(Some(user), _) => Some(encode(&format!("{}:", user))),
_ => None,
};
let response = match body { let response = match body {
Tagged { Tagged {
item: Value::Primitive(Primitive::String(body_str)), item: Value::Primitive(Primitive::String(body_str)),
.. ..
} => { } => {
surf::post(location) let mut s = surf::post(location).body_string(body_str.to_string());
.body_string(body_str.to_string()) if let Some(login) = login {
.set_header("Authorization", format!("Basic {}", login)) s = s.set_header("Authorization", format!("Basic {}", login));
.await }
s.await
} }
Tagged { Tagged {
item: Value::Binary(b), item: Value::Binary(b),
.. ..
} => { } => {
surf::post(location) let mut s = surf::post(location).body_bytes(b);
.body_bytes(b) if let Some(login) = login {
.set_header("Authorization", format!("Basic {}", login)) s = s.set_header("Authorization", format!("Basic {}", login));
.await }
s.await
} }
Tagged { item, tag } => { Tagged { item, tag } => {
if let Some(converter) = registry.get_command("to-json") { if let Some(converter) = registry.get_command("to-json") {
@ -211,10 +217,13 @@ pub async fn post(
} }
} }
} }
surf::post(location)
.body_string(result_string) let mut s = surf::post(location).body_string(result_string);
.set_header("Authorization", format!("Basic {}", login))
.await if let Some(login) = login {
s = s.set_header("Authorization", format!("Basic {}", login));
}
s.await
} else { } else {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Could not automatically convert table", "Could not automatically convert table",