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> {
|
||||
metadata
|
||||
.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)]
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use crate::network::http::client::{
|
||||
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
|
||||
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 super::client::{send_request2, HttpBody};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SubCommand;
|
||||
|
||||
|
@ -15,10 +17,10 @@ impl Command for SubCommand {
|
|||
|
||||
fn signature(&self) -> Signature {
|
||||
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)
|
||||
.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(
|
||||
"user",
|
||||
SyntaxShape::Any,
|
||||
|
@ -131,7 +133,7 @@ impl Command for SubCommand {
|
|||
struct Arguments {
|
||||
url: Value,
|
||||
headers: Option<Value>,
|
||||
data: Value,
|
||||
data: HttpBody,
|
||||
content_type: Option<String>,
|
||||
raw: bool,
|
||||
insecure: bool,
|
||||
|
@ -147,13 +149,37 @@ fn run_patch(
|
|||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
input: PipelineData,
|
||||
) -> 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 {
|
||||
url: call.req(engine_state, stack, 0)?,
|
||||
headers: call.get_flag(engine_state, stack, "headers")?,
|
||||
data: call.req(engine_state, stack, 1)?,
|
||||
content_type: call.get_flag(engine_state, stack, "content-type")?,
|
||||
data,
|
||||
content_type,
|
||||
raw: call.has_flag(engine_state, stack, "raw")?,
|
||||
insecure: call.has_flag(engine_state, stack, "insecure")?,
|
||||
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_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 {
|
||||
raw: args.raw,
|
||||
|
|
|
@ -163,18 +163,14 @@ fn run_post(
|
|||
.get_flag(engine_state, stack, "content-type")?
|
||||
.or_else(|| maybe_metadata.and_then(|m| m.content_type));
|
||||
|
||||
match data {
|
||||
HttpBody::None => {
|
||||
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![],
|
||||
})
|
||||
}
|
||||
_ => (),
|
||||
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 {
|
||||
|
|
|
@ -160,18 +160,14 @@ fn run_put(
|
|||
_ => (HttpBody::None, None),
|
||||
});
|
||||
|
||||
match data {
|
||||
HttpBody::None => {
|
||||
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![],
|
||||
})
|
||||
}
|
||||
_ => (),
|
||||
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 content_type = call
|
||||
|
|
Loading…
Reference in New Issue
Block a user