mirror of
https://github.com/nushell/nushell.git
synced 2024-12-24 16:09:11 +01:00
fetch
-> http get
and post
-> http post
(#7796)
# Updated description by @rgwood This PR changes `fetch` to `http get` and `post` to `http post`. `fetch` and `post` are now deprecated. [I surveyed people on Discord](https://discord.com/channels/601130461678272522/601130461678272524/1065706282566307910) and users strongly approved of this change. # Original Description This PR is related to #2741 and my first pull request in rust :) Implemented a new http mod to better http support and alias `fetch` and `post` commands to `http get` and `http post` respectively. # User-Facing Changes Users will be able to use HTTP method via http command, for example ``` shell > http get "https://www.example.com" <!doctype html> <html> ... ```
This commit is contained in:
parent
bbe694a622
commit
cde56741fb
@ -433,8 +433,9 @@ pub fn create_default_context() -> EngineState {
|
|||||||
|
|
||||||
// Network
|
// Network
|
||||||
bind_command! {
|
bind_command! {
|
||||||
Fetch,
|
Http,
|
||||||
Post,
|
HttpGet,
|
||||||
|
HttpPost,
|
||||||
Url,
|
Url,
|
||||||
UrlBuildQuery,
|
UrlBuildQuery,
|
||||||
UrlEncode,
|
UrlEncode,
|
||||||
|
@ -18,5 +18,7 @@ pub fn deprecated_commands() -> HashMap<String, String> {
|
|||||||
"build-string".to_string(),
|
"build-string".to_string(),
|
||||||
"str join'/'string concatenation with '+'".to_string(),
|
"str join'/'string concatenation with '+'".to_string(),
|
||||||
),
|
),
|
||||||
|
("fetch".to_string(), "http get".to_string()),
|
||||||
|
("post".to_string(), "http post".to_string()),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
@ -20,11 +20,11 @@ pub struct SubCommand;
|
|||||||
|
|
||||||
impl Command for SubCommand {
|
impl Command for SubCommand {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"fetch"
|
"http get"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("fetch")
|
Signature::build("http get")
|
||||||
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
||||||
.required(
|
.required(
|
||||||
"URL",
|
"URL",
|
||||||
@ -74,7 +74,7 @@ impl Command for SubCommand {
|
|||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec![
|
vec![
|
||||||
"network", "get", "pull", "request", "http", "download", "curl", "wget",
|
"network", "fetch", "pull", "request", "download", "curl", "wget",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,18 +91,18 @@ impl Command for SubCommand {
|
|||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Fetch content from example.com",
|
description: "http get content from example.com",
|
||||||
example: "fetch https://www.example.com",
|
example: "http get https://www.example.com",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Fetch content from example.com, with username and password",
|
description: "http get content from example.com, with username and password",
|
||||||
example: "fetch -u myuser -p mypass https://www.example.com",
|
example: "http get -u myuser -p mypass https://www.example.com",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Fetch content from example.com, with custom header",
|
description: "http get content from example.com, with custom header",
|
||||||
example: "fetch -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,
|
||||||
},
|
},
|
||||||
]
|
]
|
55
crates/nu-command/src/network/http/http_.rs
Normal file
55
crates/nu-command/src/network/http/http_.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
use nu_engine::get_full_help;
|
||||||
|
use nu_protocol::{
|
||||||
|
ast::Call,
|
||||||
|
engine::{Command, EngineState, Stack},
|
||||||
|
Category, IntoPipelineData, PipelineData, Signature, Type, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Http;
|
||||||
|
|
||||||
|
impl Command for Http {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"http"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("http")
|
||||||
|
.input_output_types(vec![(Type::Nothing, Type::String)])
|
||||||
|
.category(Category::Network)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Various commands for working with http methods"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
"You must use one of the following subcommands. Using this command as-is will only produce this help message."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec![
|
||||||
|
"network", "fetch", "pull", "request", "download", "curl", "wget",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
Ok(Value::String {
|
||||||
|
val: get_full_help(
|
||||||
|
&Http.signature(),
|
||||||
|
&Http.examples(),
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
self.is_parser_keyword(),
|
||||||
|
),
|
||||||
|
span: call.head,
|
||||||
|
}
|
||||||
|
.into_pipeline_data())
|
||||||
|
}
|
||||||
|
}
|
7
crates/nu-command/src/network/http/mod.rs
Normal file
7
crates/nu-command/src/network/http/mod.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
mod get;
|
||||||
|
mod http_;
|
||||||
|
mod post;
|
||||||
|
|
||||||
|
pub use get::SubCommand as HttpGet;
|
||||||
|
pub use http_::Http;
|
||||||
|
pub use post::SubCommand as HttpPost;
|
@ -19,11 +19,11 @@ pub struct SubCommand;
|
|||||||
|
|
||||||
impl Command for SubCommand {
|
impl Command for SubCommand {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"post"
|
"http post"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("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("path", SyntaxShape::String, "the URL to post to")
|
||||||
.required("body", SyntaxShape::Any, "the contents of the post body")
|
.required("body", SyntaxShape::Any, "the contents of the post body")
|
||||||
@ -80,7 +80,7 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["network", "send", "push", "http"]
|
vec!["network", "send", "push"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -96,22 +96,22 @@ impl Command for SubCommand {
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Post content to url.com",
|
description: "Post content to url.com",
|
||||||
example: "post url.com 'body'",
|
example: "http post url.com 'body'",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Post content to url.com, with username and password",
|
description: "Post content to url.com, with username and password",
|
||||||
example: "post -u myuser -p mypass url.com 'body'",
|
example: "http post -u myuser -p mypass url.com 'body'",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Post content to url.com, with custom header",
|
description: "Post content to url.com, with custom header",
|
||||||
example: "post -H [my-header-key my-header-value] url.com",
|
example: "http post -H [my-header-key my-header-value] url.com",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Post content to url.com with a json body",
|
description: "Post content to url.com with a json body",
|
||||||
example: "post -t application/json url.com { field: value }",
|
example: "http post -t application/json url.com { field: value }",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
]
|
]
|
@ -1,9 +1,8 @@
|
|||||||
mod fetch;
|
mod http;
|
||||||
mod port;
|
mod port;
|
||||||
mod post;
|
|
||||||
mod url;
|
mod url;
|
||||||
|
|
||||||
|
pub use self::http::*;
|
||||||
pub use self::url::*;
|
pub use self::url::*;
|
||||||
pub use fetch::SubCommand as Fetch;
|
|
||||||
pub use port::SubCommand as Port;
|
pub use port::SubCommand as Port;
|
||||||
pub use post::SubCommand as Post;
|
|
||||||
|
@ -88,7 +88,7 @@ impl Command for SubCommand {
|
|||||||
Example {
|
Example {
|
||||||
description:
|
description:
|
||||||
"A real-world example of splitting words",
|
"A real-world example of splitting words",
|
||||||
example: "fetch https://www.gutenberg.org/files/11/11-0.txt | str downcase | split words -l 2 | uniq -c | sort-by count --reverse | first 10",
|
example: "http get https://www.gutenberg.org/files/11/11-0.txt | str downcase | split words -l 2 | uniq -c | sort-by count --reverse | first 10",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user