http patch now supports pipeline data. Cleaning up data error handling based on clippy feedback
This commit is contained in:
parent
c215ff1a98
commit
9449473294
|
@ -266,7 +266,9 @@ fn convert_string_to_value_strict(string_input: &str, span: Span) -> Result<Valu
|
||||||
fn update_metadata(metadata: Option<PipelineMetadata>) -> Option<PipelineMetadata> {
|
fn update_metadata(metadata: Option<PipelineMetadata>) -> Option<PipelineMetadata> {
|
||||||
metadata
|
metadata
|
||||||
.map(|md| md.with_content_type(Some("application/json".into())))
|
.map(|md| md.with_content_type(Some("application/json".into())))
|
||||||
.or_else(|| Some(PipelineMetadata::default().with_content_type(Some("application/json".into()))))
|
.or_else(|| {
|
||||||
|
Some(PipelineMetadata::default().with_content_type(Some("application/json".into())))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
use crate::network::http::client::{
|
use crate::network::http::client::{
|
||||||
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
|
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
|
||||||
request_add_authorization_header, request_add_custom_headers, request_handle_response,
|
request_add_authorization_header, request_add_custom_headers, request_handle_response,
|
||||||
request_set_timeout, send_request, RequestFlags,
|
request_set_timeout, RequestFlags,
|
||||||
};
|
};
|
||||||
use nu_engine::command_prelude::*;
|
use nu_engine::command_prelude::*;
|
||||||
|
|
||||||
|
use super::client::{send_request2, HttpBody};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SubCommand;
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
@ -15,10 +17,10 @@ impl Command for SubCommand {
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("http patch")
|
Signature::build("http patch")
|
||||||
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
.input_output_types(vec![(Type::Any, Type::Any)])
|
||||||
.allow_variants_without_examples(true)
|
.allow_variants_without_examples(true)
|
||||||
.required("URL", SyntaxShape::String, "The URL to post to.")
|
.required("URL", SyntaxShape::String, "The URL to post to.")
|
||||||
.required("data", SyntaxShape::Any, "The contents of the post body.")
|
.optional("data", SyntaxShape::Any, "The contents of the post body.")
|
||||||
.named(
|
.named(
|
||||||
"user",
|
"user",
|
||||||
SyntaxShape::Any,
|
SyntaxShape::Any,
|
||||||
|
@ -131,7 +133,7 @@ impl Command for SubCommand {
|
||||||
struct Arguments {
|
struct Arguments {
|
||||||
url: Value,
|
url: Value,
|
||||||
headers: Option<Value>,
|
headers: Option<Value>,
|
||||||
data: Value,
|
data: HttpBody,
|
||||||
content_type: Option<String>,
|
content_type: Option<String>,
|
||||||
raw: bool,
|
raw: bool,
|
||||||
insecure: bool,
|
insecure: bool,
|
||||||
|
@ -147,13 +149,37 @@ fn run_patch(
|
||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
_input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let (data, maybe_metadata) = call
|
||||||
|
.opt::<Value>(engine_state, stack, 1)?
|
||||||
|
.map(|v| (HttpBody::Value(v), None))
|
||||||
|
.unwrap_or_else(|| match input {
|
||||||
|
PipelineData::Value(v, metadata) => (HttpBody::Value(v), metadata),
|
||||||
|
PipelineData::ByteStream(byte_stream, metadata) => {
|
||||||
|
(HttpBody::ByteStream(byte_stream), metadata)
|
||||||
|
}
|
||||||
|
_ => (HttpBody::None, None),
|
||||||
|
});
|
||||||
|
let content_type = call
|
||||||
|
.get_flag(engine_state, stack, "content-type")?
|
||||||
|
.or_else(|| maybe_metadata.and_then(|m| m.content_type));
|
||||||
|
|
||||||
|
if let HttpBody::None = data {
|
||||||
|
return Err(ShellError::GenericError {
|
||||||
|
error: "Data must be provided either through pipeline or positional argument".into(),
|
||||||
|
msg: "".into(),
|
||||||
|
span: Some(call.head),
|
||||||
|
help: None,
|
||||||
|
inner: vec![],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let args = Arguments {
|
let args = Arguments {
|
||||||
url: call.req(engine_state, stack, 0)?,
|
url: call.req(engine_state, stack, 0)?,
|
||||||
headers: call.get_flag(engine_state, stack, "headers")?,
|
headers: call.get_flag(engine_state, stack, "headers")?,
|
||||||
data: call.req(engine_state, stack, 1)?,
|
data,
|
||||||
content_type: call.get_flag(engine_state, stack, "content-type")?,
|
content_type,
|
||||||
raw: call.has_flag(engine_state, stack, "raw")?,
|
raw: call.has_flag(engine_state, stack, "raw")?,
|
||||||
insecure: call.has_flag(engine_state, stack, "insecure")?,
|
insecure: call.has_flag(engine_state, stack, "insecure")?,
|
||||||
user: call.get_flag(engine_state, stack, "user")?,
|
user: call.get_flag(engine_state, stack, "user")?,
|
||||||
|
@ -187,7 +213,7 @@ fn helper(
|
||||||
request = request_add_authorization_header(args.user, args.password, request);
|
request = request_add_authorization_header(args.user, args.password, request);
|
||||||
request = request_add_custom_headers(args.headers, request)?;
|
request = request_add_custom_headers(args.headers, request)?;
|
||||||
|
|
||||||
let response = send_request(request.clone(), Some(args.data), args.content_type, ctrl_c);
|
let response = send_request2(request.clone(), args.data, args.content_type, ctrl_c);
|
||||||
|
|
||||||
let request_flags = RequestFlags {
|
let request_flags = RequestFlags {
|
||||||
raw: args.raw,
|
raw: args.raw,
|
||||||
|
|
|
@ -163,18 +163,14 @@ fn run_post(
|
||||||
.get_flag(engine_state, stack, "content-type")?
|
.get_flag(engine_state, stack, "content-type")?
|
||||||
.or_else(|| maybe_metadata.and_then(|m| m.content_type));
|
.or_else(|| maybe_metadata.and_then(|m| m.content_type));
|
||||||
|
|
||||||
match data {
|
if let HttpBody::None = data {
|
||||||
HttpBody::None => {
|
return Err(ShellError::GenericError {
|
||||||
return Err(ShellError::GenericError {
|
error: "Data must be provided either through pipeline or positional argument".into(),
|
||||||
error: "Data must be provided either through pipeline or positional argument"
|
msg: "".into(),
|
||||||
.into(),
|
span: Some(call.head),
|
||||||
msg: "".into(),
|
help: None,
|
||||||
span: Some(call.head),
|
inner: vec![],
|
||||||
help: None,
|
});
|
||||||
inner: vec![],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let args = Arguments {
|
let args = Arguments {
|
||||||
|
|
|
@ -160,18 +160,14 @@ fn run_put(
|
||||||
_ => (HttpBody::None, None),
|
_ => (HttpBody::None, None),
|
||||||
});
|
});
|
||||||
|
|
||||||
match data {
|
if let HttpBody::None = data {
|
||||||
HttpBody::None => {
|
return Err(ShellError::GenericError {
|
||||||
return Err(ShellError::GenericError {
|
error: "Data must be provided either through pipeline or positional argument".into(),
|
||||||
error: "Data must be provided either through pipeline or positional argument"
|
msg: "".into(),
|
||||||
.into(),
|
span: Some(call.head),
|
||||||
msg: "".into(),
|
help: None,
|
||||||
span: Some(call.head),
|
inner: vec![],
|
||||||
help: None,
|
});
|
||||||
inner: vec![],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let content_type = call
|
let content_type = call
|
||||||
|
|
Loading…
Reference in New Issue
Block a user