mirror of
https://github.com/nushell/nushell.git
synced 2025-05-22 15:45:35 +00:00
# Description _(Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes.)_ This pull request removes `Reqwest` and replaces it with `Ureq` to remove some of our dependencies, giving us faster compile times as well as smaller binaries. `Ureq` does not have an async runtime included so we do not need build heavy dependencies such as `Tokio`. From older tests I had the number of build units be reduced from `430 -> 392`. The default of `Ureq` uses `Rustls` but it has been configured to instead use `native_tls` which should work exactly the same as the `tls` works now. I removed `content-length` from the http commands as after refactoring i did not see a reason to have it available, correct me if this is something we should preserve. In the medium, to long term, we should maybe consider changing to `rustls` to have the same `tls` on all platforms. # User-Facing Changes _(List of all changes that impact the user experience here. This helps us keep track of breaking changes.)_ # 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.
192 lines
5.6 KiB
Rust
192 lines
5.6 KiB
Rust
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
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_timeout, send_request,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct SubCommand;
|
|
|
|
impl Command for SubCommand {
|
|
fn name(&self) -> &str {
|
|
"http patch"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("http patch")
|
|
.input_output_types(vec![(Type::Nothing, 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")
|
|
.named(
|
|
"user",
|
|
SyntaxShape::Any,
|
|
"the username when authenticating",
|
|
Some('u'),
|
|
)
|
|
.named(
|
|
"password",
|
|
SyntaxShape::Any,
|
|
"the password when authenticating",
|
|
Some('p'),
|
|
)
|
|
.named(
|
|
"content-type",
|
|
SyntaxShape::Any,
|
|
"the MIME type of content to post",
|
|
Some('t'),
|
|
)
|
|
.named(
|
|
"max-time",
|
|
SyntaxShape::Int,
|
|
"timeout period in seconds",
|
|
Some('m'),
|
|
)
|
|
.named(
|
|
"headers",
|
|
SyntaxShape::Any,
|
|
"custom headers you want to add ",
|
|
Some('H'),
|
|
)
|
|
.switch(
|
|
"raw",
|
|
"return values as a string instead of a table",
|
|
Some('r'),
|
|
)
|
|
.switch(
|
|
"insecure",
|
|
"allow insecure server connections when using SSL",
|
|
Some('k'),
|
|
)
|
|
.filter()
|
|
.category(Category::Network)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Patch a body to a URL."
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
"Performs HTTP PATCH operation."
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["network", "send", "push"]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
run_patch(engine_state, stack, call, input)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Patch content to example.com",
|
|
example: "http patch https://www.example.com 'body'",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Patch content to example.com, with username and password",
|
|
example: "http patch -u myuser -p mypass https://www.example.com 'body'",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Patch content to example.com, with custom header",
|
|
example: "http patch -H [my-header-key my-header-value] https://www.example.com",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Patch content to example.com, with JSON body",
|
|
example: "http patch -t application/json https://www.example.com { field: value }",
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
struct Arguments {
|
|
url: Value,
|
|
headers: Option<Value>,
|
|
data: Value,
|
|
content_type: Option<String>,
|
|
raw: bool,
|
|
insecure: bool,
|
|
user: Option<String>,
|
|
password: Option<String>,
|
|
timeout: Option<Value>,
|
|
}
|
|
|
|
fn run_patch(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
_input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
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")?,
|
|
raw: call.has_flag("raw"),
|
|
insecure: call.has_flag("insecure"),
|
|
user: call.get_flag(engine_state, stack, "user")?,
|
|
password: call.get_flag(engine_state, stack, "password")?,
|
|
timeout: call.get_flag(engine_state, stack, "max-time")?,
|
|
};
|
|
|
|
helper(engine_state, stack, call, args)
|
|
}
|
|
|
|
// Helper function that actually goes to retrieve the resource from the url given
|
|
// The Option<String> return a possible file extension which can be used in AutoConvert commands
|
|
fn helper(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
args: Arguments,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let span = args.url.span()?;
|
|
let (requested_url, _) = http_parse_url(call, span, args.url)?;
|
|
|
|
let client = http_client(args.insecure);
|
|
let mut request = client.patch(&requested_url);
|
|
|
|
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 = send_request(request, span, Some(args.data), args.content_type);
|
|
request_handle_response(
|
|
engine_state,
|
|
stack,
|
|
span,
|
|
&requested_url,
|
|
args.raw,
|
|
response,
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
use crate::test_examples;
|
|
|
|
test_examples(SubCommand {})
|
|
}
|
|
}
|