Final rework for the HTTP commands (#8135)
# Description Final rework for https://github.com/nushell/nushell/issues/2741, after this one, we'll add for free HTTP PUT, PATCH, DELETE and HEAD. # User-Facing Changes We can now post data using HTTP GET. I add some examples in the output of `http get --help` to demonstrate this new behavior. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
parent
66e52b7cfc
commit
c4d1aa452d
|
@ -12,6 +12,7 @@ use std::io::BufReader;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq)]
|
||||||
pub enum BodyType {
|
pub enum BodyType {
|
||||||
|
@ -30,6 +31,28 @@ pub fn http_client(allow_insecure: bool) -> reqwest::blocking::Client {
|
||||||
.expect("Failed to build reqwest client")
|
.expect("Failed to build reqwest client")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn http_parse_url(
|
||||||
|
call: &Call,
|
||||||
|
span: Span,
|
||||||
|
raw_url: Value,
|
||||||
|
) -> Result<(String, Url), ShellError> {
|
||||||
|
let requested_url = raw_url.as_string()?;
|
||||||
|
let url = match url::Url::parse(&requested_url) {
|
||||||
|
Ok(u) => u,
|
||||||
|
Err(_e) => {
|
||||||
|
return Err(ShellError::UnsupportedInput(
|
||||||
|
"Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com"
|
||||||
|
.to_string(),
|
||||||
|
format!("value: '{requested_url:?}'"),
|
||||||
|
call.head,
|
||||||
|
span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((requested_url, url))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn response_to_buffer(
|
pub fn response_to_buffer(
|
||||||
response: blocking::Response,
|
response: blocking::Response,
|
||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
use crate::network::http::client::{
|
|
||||||
http_client, request_add_authorization_header, request_add_custom_headers,
|
|
||||||
request_handle_response, request_set_timeout,
|
|
||||||
};
|
|
||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
@ -9,6 +5,11 @@ use nu_protocol::{
|
||||||
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::network::http::client::{
|
||||||
|
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
|
||||||
|
request_handle_response, request_set_body, request_set_timeout,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SubCommand;
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
@ -37,6 +38,19 @@ impl Command for SubCommand {
|
||||||
"the password when authenticating",
|
"the password when authenticating",
|
||||||
Some('p'),
|
Some('p'),
|
||||||
)
|
)
|
||||||
|
.named("data", SyntaxShape::Any, "the content to post", Some('d'))
|
||||||
|
.named(
|
||||||
|
"content-type",
|
||||||
|
SyntaxShape::Any,
|
||||||
|
"the MIME type of content to post",
|
||||||
|
Some('t'),
|
||||||
|
)
|
||||||
|
.named(
|
||||||
|
"content-length",
|
||||||
|
SyntaxShape::Any,
|
||||||
|
"the length of the content being posted",
|
||||||
|
Some('l'),
|
||||||
|
)
|
||||||
.named(
|
.named(
|
||||||
"max-time",
|
"max-time",
|
||||||
SyntaxShape::Int,
|
SyntaxShape::Int,
|
||||||
|
@ -104,18 +118,31 @@ impl Command for SubCommand {
|
||||||
example: "http get -H [my-header-key my-header-value] https://www.example.com",
|
example: "http get -H [my-header-key my-header-value] https://www.example.com",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "http get from example.com, with body",
|
||||||
|
example: "http get -d 'body' https://www.example.com",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "http get from example.com, with JSON body",
|
||||||
|
example: "http get -t application/json -d { field: value } https://www.example.com",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Arguments {
|
struct Arguments {
|
||||||
url: Value,
|
url: Value,
|
||||||
|
headers: Option<Value>,
|
||||||
|
data: Option<Value>,
|
||||||
|
content_type: Option<String>,
|
||||||
|
content_length: Option<String>,
|
||||||
raw: bool,
|
raw: bool,
|
||||||
insecure: Option<bool>,
|
insecure: Option<bool>,
|
||||||
user: Option<String>,
|
user: Option<String>,
|
||||||
password: Option<String>,
|
password: Option<String>,
|
||||||
timeout: Option<Value>,
|
timeout: Option<Value>,
|
||||||
headers: Option<Value>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_post(
|
fn run_post(
|
||||||
|
@ -126,14 +153,17 @@ fn run_post(
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
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")?,
|
||||||
|
data: call.get_flag(engine_state, stack, "data")?,
|
||||||
|
content_type: call.get_flag(engine_state, stack, "content-type")?,
|
||||||
|
content_length: call.get_flag(engine_state, stack, "content-length")?,
|
||||||
raw: call.has_flag("raw"),
|
raw: call.has_flag("raw"),
|
||||||
insecure: call.get_flag(engine_state, stack, "insecure")?,
|
insecure: call.get_flag(engine_state, stack, "insecure")?,
|
||||||
user: call.get_flag(engine_state, stack, "user")?,
|
user: call.get_flag(engine_state, stack, "user")?,
|
||||||
password: call.get_flag(engine_state, stack, "password")?,
|
password: call.get_flag(engine_state, stack, "password")?,
|
||||||
timeout: call.get_flag(engine_state, stack, "timeout")?,
|
timeout: call.get_flag(engine_state, stack, "timeout")?,
|
||||||
headers: call.get_flag(engine_state, stack, "headers")?,
|
|
||||||
};
|
};
|
||||||
helper(engine_state, stack, args)
|
helper(engine_state, stack, call, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function that actually goes to retrieve the resource from the url given
|
// Helper function that actually goes to retrieve the resource from the url given
|
||||||
|
@ -141,35 +171,29 @@ fn run_post(
|
||||||
fn helper(
|
fn helper(
|
||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
args: Arguments,
|
args: Arguments,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let url_value = args.url;
|
let span = args.url.span()?;
|
||||||
let user = args.user.clone();
|
let (requested_url, url) = http_parse_url(call, span, args.url)?;
|
||||||
let password = args.password;
|
|
||||||
let timeout = args.timeout;
|
|
||||||
let headers = args.headers;
|
|
||||||
let raw = args.raw;
|
|
||||||
|
|
||||||
let span = url_value.span()?;
|
|
||||||
let requested_url = url_value.as_string()?;
|
|
||||||
let url = match url::Url::parse(&requested_url) {
|
|
||||||
Ok(u) => u,
|
|
||||||
Err(_e) => {
|
|
||||||
return Err(ShellError::TypeMismatch(
|
|
||||||
"Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com"
|
|
||||||
.to_string(),
|
|
||||||
span,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let client = http_client(args.insecure.is_some());
|
let client = http_client(args.insecure.is_some());
|
||||||
let mut request = client.get(url);
|
let mut request = client.get(url);
|
||||||
|
|
||||||
request = request_set_timeout(timeout, request)?;
|
if let Some(data) = args.data {
|
||||||
request = request_add_authorization_header(user, password, request);
|
request = request_set_body(args.content_type, args.content_length, data, request)?;
|
||||||
request = request_add_custom_headers(headers, request)?;
|
}
|
||||||
|
request = request_set_timeout(args.timeout, request)?;
|
||||||
|
request = request_add_authorization_header(args.user, args.password, request);
|
||||||
|
request = request_add_custom_headers(args.headers, request)?;
|
||||||
|
|
||||||
let response = request.send().and_then(|r| r.error_for_status());
|
let response = request.send().and_then(|r| r.error_for_status());
|
||||||
request_handle_response(engine_state, stack, span, &requested_url, raw, response)
|
request_handle_response(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
span,
|
||||||
|
&requested_url,
|
||||||
|
args.raw,
|
||||||
|
response,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ use nu_protocol::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::network::http::client::{
|
use crate::network::http::client::{
|
||||||
http_client, request_add_authorization_header, request_add_custom_headers,
|
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
|
||||||
request_handle_response, request_set_body, request_set_timeout,
|
request_handle_response, request_set_body, request_set_timeout,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -21,8 +21,8 @@ impl Command for SubCommand {
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("http post")
|
Signature::build("http post")
|
||||||
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
||||||
.required("path", SyntaxShape::String, "the URL to post to")
|
.required("URL", SyntaxShape::String, "the URL to post to")
|
||||||
.required("body", SyntaxShape::Any, "the contents of the post body")
|
.required("data", SyntaxShape::Any, "the contents of the post body")
|
||||||
.named(
|
.named(
|
||||||
"user",
|
"user",
|
||||||
SyntaxShape::Any,
|
SyntaxShape::Any,
|
||||||
|
@ -113,7 +113,7 @@ impl Command for SubCommand {
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Post content to url.com with a json body",
|
description: "Post content to url.com, with JSON body",
|
||||||
example: "http post -t application/json url.com { field: value }",
|
example: "http post -t application/json url.com { field: value }",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
|
@ -122,16 +122,16 @@ impl Command for SubCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Arguments {
|
struct Arguments {
|
||||||
path: Value,
|
url: Value,
|
||||||
body: Value,
|
|
||||||
timeout: Option<Value>,
|
|
||||||
headers: Option<Value>,
|
headers: Option<Value>,
|
||||||
|
data: Value,
|
||||||
|
content_type: Option<String>,
|
||||||
|
content_length: Option<String>,
|
||||||
raw: bool,
|
raw: bool,
|
||||||
insecure: Option<bool>,
|
insecure: Option<bool>,
|
||||||
user: Option<String>,
|
user: Option<String>,
|
||||||
password: Option<String>,
|
password: Option<String>,
|
||||||
content_type: Option<String>,
|
timeout: Option<Value>,
|
||||||
content_length: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_post(
|
fn run_post(
|
||||||
|
@ -141,16 +141,16 @@ fn run_post(
|
||||||
_input: PipelineData,
|
_input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let args = Arguments {
|
let args = Arguments {
|
||||||
path: call.req(engine_state, stack, 0)?,
|
url: call.req(engine_state, stack, 0)?,
|
||||||
body: call.req(engine_state, stack, 1)?,
|
|
||||||
timeout: call.get_flag(engine_state, stack, "timeout")?,
|
|
||||||
headers: call.get_flag(engine_state, stack, "headers")?,
|
headers: call.get_flag(engine_state, stack, "headers")?,
|
||||||
raw: call.has_flag("raw"),
|
data: call.req(engine_state, stack, 1)?,
|
||||||
user: call.get_flag(engine_state, stack, "user")?,
|
|
||||||
password: call.get_flag(engine_state, stack, "password")?,
|
|
||||||
insecure: call.get_flag(engine_state, stack, "insecure")?,
|
|
||||||
content_type: call.get_flag(engine_state, stack, "content-type")?,
|
content_type: call.get_flag(engine_state, stack, "content-type")?,
|
||||||
content_length: call.get_flag(engine_state, stack, "content-length")?,
|
content_length: call.get_flag(engine_state, stack, "content-length")?,
|
||||||
|
raw: call.has_flag("raw"),
|
||||||
|
insecure: call.get_flag(engine_state, stack, "insecure")?,
|
||||||
|
user: call.get_flag(engine_state, stack, "user")?,
|
||||||
|
password: call.get_flag(engine_state, stack, "password")?,
|
||||||
|
timeout: call.get_flag(engine_state, stack, "timeout")?,
|
||||||
};
|
};
|
||||||
helper(engine_state, stack, call, args)
|
helper(engine_state, stack, call, args)
|
||||||
}
|
}
|
||||||
|
@ -163,38 +163,24 @@ fn helper(
|
||||||
call: &Call,
|
call: &Call,
|
||||||
args: Arguments,
|
args: Arguments,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let url_value = args.path;
|
let span = args.url.span()?;
|
||||||
let body = args.body;
|
let (requested_url, url) = http_parse_url(call, span, args.url)?;
|
||||||
let user = args.user.clone();
|
|
||||||
let password = args.password;
|
|
||||||
let timeout = args.timeout;
|
|
||||||
let headers = args.headers;
|
|
||||||
let content_type = args.content_type;
|
|
||||||
let content_length = args.content_length;
|
|
||||||
let raw = args.raw;
|
|
||||||
|
|
||||||
let span = url_value.span()?;
|
let client = http_client(args.insecure.is_some());
|
||||||
let requested_url = url_value.as_string()?;
|
let mut request = client.post(url);
|
||||||
let url = match url::Url::parse(&requested_url) {
|
|
||||||
Ok(u) => u,
|
|
||||||
Err(_e) => {
|
|
||||||
return Err(ShellError::UnsupportedInput(
|
|
||||||
"Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com"
|
|
||||||
.to_string(),
|
|
||||||
format!("value: '{requested_url:?}'"),
|
|
||||||
call.head,
|
|
||||||
span,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut request = http_client(args.insecure.is_some()).post(url);
|
request = request_set_body(args.content_type, args.content_length, args.data, request)?;
|
||||||
|
request = request_set_timeout(args.timeout, request)?;
|
||||||
request = request_set_body(content_type, content_length, body, request)?;
|
request = request_add_authorization_header(args.user, args.password, request);
|
||||||
request = request_set_timeout(timeout, request)?;
|
request = request_add_custom_headers(args.headers, request)?;
|
||||||
request = request_add_authorization_header(user, password, request);
|
|
||||||
request = request_add_custom_headers(headers, request)?;
|
|
||||||
|
|
||||||
let response = request.send().and_then(|r| r.error_for_status());
|
let response = request.send().and_then(|r| r.error_for_status());
|
||||||
request_handle_response(engine_state, stack, span, &requested_url, raw, response)
|
request_handle_response(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
span,
|
||||||
|
&requested_url,
|
||||||
|
args.raw,
|
||||||
|
response,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user