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:
KazukiY 2023-01-21 03:38:30 +09:00 committed by GitHub
parent bbe694a622
commit cde56741fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 23 deletions

View File

@ -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,

View File

@ -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()),
]) ])
} }

View File

@ -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,
}, },
] ]

View 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())
}
}

View 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;

View File

@ -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,
}, },
] ]

View File

@ -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;

View File

@ -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,
}, },
] ]