Skip to content

Commit

Permalink
Add flag to display body only
Browse files Browse the repository at this point in the history
  • Loading branch information
SummerGram committed Feb 26, 2024
1 parent ebfd224 commit eea6ef7
Show file tree
Hide file tree
Showing 28 changed files with 131 additions and 43 deletions.
2 changes: 0 additions & 2 deletions src/app/service_commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::FnOnce;

use tokio::sync::oneshot;

pub type Functor<ServiceInstance> =
Expand Down
10 changes: 9 additions & 1 deletion src/view/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ pub trait ViewCommand {
pub enum ViewCommandChoice {
SubmitRequest {
request: RequestData,
print_body_only: bool,
},

SubmitSavedRequest {
request_name: String,
request_data: PartialRequestData,
print_body_only: bool,
},

SaveNewRequest {
Expand Down Expand Up @@ -75,18 +77,24 @@ impl ViewCommandChoice {
let writer_stderr = CrosstermCliWriter::from(stderr());

match self {
ViewCommandChoice::SubmitRequest { request } => BasicRequestExecutor {
ViewCommandChoice::SubmitRequest {
request,
print_body_only,
} => BasicRequestExecutor {
request,
print_body_only,
writer_stdout,
writer_stderr,
}
.into(),
ViewCommandChoice::SubmitSavedRequest {
request_name,
request_data,
print_body_only,
} => SubmitSavedRequestExecutor {
request_name,
input_request_data: request_data,
print_body_only,
writer_stdout,
writer_stderr,
}
Expand Down
21 changes: 13 additions & 8 deletions src/view/commands/submit_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ where
W2: CliWriterRepository,
{
pub request: RequestData,
pub print_body_only: bool,
pub writer_stdout: W1,
pub writer_stderr: W2,
}
Expand Down Expand Up @@ -58,10 +59,12 @@ where
.collect()
};

self.writer_stderr.print_lines([BREAK_LINE]);
self.writer_stderr.print_lines_styled([title]);
self.writer_stderr.print_lines_styled(headers);
self.writer_stderr.print_lines([BREAK_LINE]);
if !self.print_body_only {
self.writer_stderr.print_lines([BREAK_LINE]);
self.writer_stderr.print_lines_styled([title]);
self.writer_stderr.print_lines_styled(headers);
self.writer_stderr.print_lines([BREAK_LINE]);
}

let request_id = provider.add_request(self.request).await?;
let response_submit = provider.submit_request_async(request_id).await?;
Expand Down Expand Up @@ -135,10 +138,12 @@ where
.collect()
};

self.writer_stderr.print_lines_styled([title_status]);
self.writer_stderr.print_lines([BREAK_LINE_WITH_GAP]);
self.writer_stderr.print_lines_styled(headers);
self.writer_stderr.print_lines([BREAK_LINE_WITH_GAP]);
if !self.print_body_only {
self.writer_stderr.print_lines_styled([title_status]);
self.writer_stderr.print_lines([BREAK_LINE_WITH_GAP]);
self.writer_stderr.print_lines_styled(headers);
self.writer_stderr.print_lines([BREAK_LINE_WITH_GAP]);
}
self.writer_stdout.print_lines([response.body]);

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions src/view/commands/submit_saved_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ where
{
pub request_name: String,
pub input_request_data: PartialRequestData,
pub print_body_only: bool,
pub writer_stdout: W1,
pub writer_stderr: W2,
}
Expand Down Expand Up @@ -43,6 +44,7 @@ where

Box::new(BasicRequestExecutor {
request,
print_body_only: self.print_body_only,
writer_stdout: self.writer_stdout,
writer_stderr: self.writer_stderr,
})
Expand Down
13 changes: 13 additions & 0 deletions src/view/input/cli_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn root_command() -> Command {
.map(add_request_items_args)
.map(add_raw_flag)
.map(add_save_as_flag)
.map(add_print_body_only_flag)
.unwrap(),
)
}
Expand All @@ -35,6 +36,7 @@ pub fn root_command() -> Command {
.map(add_save_changes_to_current_request_flag)
.map(add_manual_url_flag)
.map(add_manual_method_flag)
.map(add_print_body_only_flag)
.unwrap(),
)
.subcommand(
Expand Down Expand Up @@ -106,6 +108,7 @@ pub fn root_command() -> Command {
.map(add_request_items_args)
.map(add_raw_flag)
.map(add_save_as_flag)
.map(add_print_body_only_flag)
.unwrap();

app = app.override_usage(
Expand Down Expand Up @@ -214,6 +217,16 @@ Optional key-value pairs to be included in the request. The separator used deter
)
}

fn add_print_body_only_flag(command: Command) -> Command {
command.arg(
Arg::new("print-body-only")
.long("body")
.short('b')
.action(ArgAction::SetTrue)
.help("Print the response body only"),
)
}

fn add_raw_flag(command: Command) -> Command {
command.arg(
Arg::new("raw")
Expand Down
18 changes: 16 additions & 2 deletions src/view/input/cli_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ pub struct CliInput {
pub enum CliCommandChoice {
DefaultBasicRequest {
url: String,
print_body_only: bool,
},
BasicRequest {
method: METHODS,
url: String,
print_body_only: bool,
},
Run {
request_name: String,
print_body_only: bool,
save: bool,
},
Edit {
Expand All @@ -45,10 +48,14 @@ impl CliInput {
if matches.subcommand().is_none() {
let url = clap_args_utils::get_input(matches)?.to_string();
let request_input = RequestBuildingOptions::from_clap_matches(matches)?;
let print_body_only = *matches.get_one::<bool>("print-body-only").unwrap_or(&false);
let save_options = SavingOptions::from_clap_matches(matches)?;

return Ok(CliInput {
choice: CliCommandChoice::DefaultBasicRequest { url },
choice: CliCommandChoice::DefaultBasicRequest {
url,
print_body_only,
},
request_input,
save_options,
});
Expand Down Expand Up @@ -111,10 +118,12 @@ impl CliInput {
"run" => {
let request_name = clap_args_utils::get_input(matches)?.to_string();
let should_save_current_request = matches.get_one::<bool>("save").unwrap_or(&false);
let print_body_only = *matches.get_one::<bool>("print-body-only").unwrap_or(&false);

Ok(CliInput {
choice: CliCommandChoice::Run {
request_name,
print_body_only,
save: *should_save_current_request,
},
request_input,
Expand All @@ -124,9 +133,14 @@ impl CliInput {
"GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH" => {
let url = clap_args_utils::get_input(matches)?.to_string();
let method = METHODS::from_str(subcommand)?;
let print_body_only = *matches.get_one::<bool>("print-body-only").unwrap_or(&false);

Ok(CliInput {
choice: CliCommandChoice::BasicRequest { method, url },
choice: CliCommandChoice::BasicRequest {
method,
url,
print_body_only,
},
request_input,
save_options,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use crate::utils::regexes;
use crate::view::input::cli_input::{CliCommandChoice, CliInput};

pub fn validate_basic_request_without_explicit_method(mut input: CliInput) -> Result<CliInput> {
if let CliCommandChoice::DefaultBasicRequest { ref url } = input.choice {
if let CliCommandChoice::DefaultBasicRequest {
ref url,
print_body_only,
} = input.choice
{
let url = url.clone();
input
.request_input
Expand All @@ -16,6 +20,7 @@ pub fn validate_basic_request_without_explicit_method(mut input: CliInput) -> Re
input.choice = CliCommandChoice::BasicRequest {
method: METHODS::POST,
url,
print_body_only,
};
});
}
Expand Down
17 changes: 14 additions & 3 deletions src/view/input_parsers/main_command_choices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ pub fn parse_inputs_to_main_command_choices(
request_data: base_request.clone(),
}]
}
CliCommandChoice::Run { request_name, save } => {
CliCommandChoice::Run {
request_name,
save,
print_body_only,
} => {
let main_command = ViewCommandChoice::SubmitSavedRequest {
request_name: request_name.to_string(),
request_data: base_request.clone(),
print_body_only: *print_body_only,
};

if *save {
Expand All @@ -51,14 +56,20 @@ pub fn parse_inputs_to_main_command_choices(
vec![main_command]
}
}
CliCommandChoice::DefaultBasicRequest { .. } => {
CliCommandChoice::DefaultBasicRequest {
print_body_only, ..
} => {
vec![ViewCommandChoice::SubmitRequest {
request: base_request.clone().to_request_data(),
print_body_only: *print_body_only,
}]
}
CliCommandChoice::BasicRequest { .. } => {
CliCommandChoice::BasicRequest {
print_body_only, ..
} => {
vec![ViewCommandChoice::SubmitRequest {
request: base_request.clone().to_request_data(),
print_body_only: *print_body_only,
}]
}
};
Expand Down
48 changes: 36 additions & 12 deletions src/view/input_parsers/request_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub fn parse_inputs_to_request_data(input: &CliInput) -> Result<PartialRequestDa

// Request data from 'CliCommandChoice'
let base_request = match input.choice {
CliCommandChoice::BasicRequest { method, ref url } => {
base_request.with_method(method).with_url(url)
}
CliCommandChoice::DefaultBasicRequest { ref url } => {
CliCommandChoice::BasicRequest {
method, ref url, ..
} => base_request.with_method(method).with_url(url),
CliCommandChoice::DefaultBasicRequest { ref url, .. } => {
base_request.with_method(METHODS::GET).with_url(url)
}
_ => base_request,
Expand Down Expand Up @@ -289,14 +289,38 @@ pub mod tests_parsers_request_items {
#[test]
fn test_non_string_body_value_nested() {
let cases = [
(r#"hobbies:='["http", "pies"]'"#, r#"{ "hobbies": ["http", "pies"] }"#),
(r#"hobbies:="["http", "pies"]""#, r#"{ "hobbies": ["http", "pies"] }"#),
(r#"hobbies:=["http", "pies"]"#, r#"{ "hobbies": ["http", "pies"] }"#),
(r#"favorite:={"tool": "HTTPie"}"#, r#"{ "favorite": { "tool": "HTTPie"} }"#),
(r#"favorite:="{"tool": "HTTPie"}""#, r#"{ "favorite": { "tool": "HTTPie"} }"#),
(r#"favorite:='{"tool": "HTTPie"}'"#, r#"{ "favorite": { "tool": "HTTPie"} }"#),
(r#"complex:=[null,{},["a", false], true]"#, r#"{ "complex": [null, {}, ["a", false], true] }"#),
(r#"complex:='{"tool": {"all":[true, 29, {"name": ["Mary", "John"]}]}}'"#, r#"{ "complex": {"tool": {"all":[true, 29, {"name": ["Mary", "John"]}]}} }"#),
(
r#"hobbies:='["http", "pies"]'"#,
r#"{ "hobbies": ["http", "pies"] }"#,
),
(
r#"hobbies:="["http", "pies"]""#,
r#"{ "hobbies": ["http", "pies"] }"#,
),
(
r#"hobbies:=["http", "pies"]"#,
r#"{ "hobbies": ["http", "pies"] }"#,
),
(
r#"favorite:={"tool": "HTTPie"}"#,
r#"{ "favorite": { "tool": "HTTPie"} }"#,
),
(
r#"favorite:="{"tool": "HTTPie"}""#,
r#"{ "favorite": { "tool": "HTTPie"} }"#,
),
(
r#"favorite:='{"tool": "HTTPie"}'"#,
r#"{ "favorite": { "tool": "HTTPie"} }"#,
),
(
r#"complex:=[null,{},["a", false], true]"#,
r#"{ "complex": [null, {}, ["a", false], true] }"#,
),
(
r#"complex:='{"tool": {"all":[true, 29, {"name": ["Mary", "John"]}]}}'"#,
r#"{ "complex": {"tool": {"all":[true, 29, {"name": ["Mary", "John"]}]}} }"#,
),
];

for (input, output) in cases {
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/cli_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async fn should_submit_a_basic_request() -> anyhow::Result<()> {

let executor: Box<dyn ViewCommand> = BasicRequestExecutor {
request: request_to_do.clone(),
print_body_only: false,
writer_stdout: CliWriterUseLess,
writer_stderr: CliWriterUseLess,
}
Expand Down Expand Up @@ -59,6 +60,7 @@ async fn should_submit_a_request_after_saved() -> anyhow::Result<()> {

let basic_request_executor: Box<dyn ViewCommand> = BasicRequestExecutor {
request: first_request_to_do.clone(),
print_body_only: false,
writer_stdout: CliWriterUseLess,
writer_stderr: CliWriterUseLess,
}
Expand Down Expand Up @@ -87,6 +89,7 @@ async fn should_submit_a_request_after_saved() -> anyhow::Result<()> {
let submit_save_request_executor: Box<dyn ViewCommand> = SubmitSavedRequestExecutor {
request_name: "some_request".into(),
input_request_data: PartialRequestData::default(),
print_body_only: false,
writer_stdout: CliWriterUseLess,
writer_stderr: CliWriterUseLess,
}
Expand Down
2 changes: 2 additions & 0 deletions tests/view/map_input_to_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ fn should_parse_number_json() {
"treq",
"GET",
"url.com",
"-b",
"Hello=World",
"age:=29",
"amount:=-30.8",
Expand All @@ -277,6 +278,7 @@ fn should_parse_combine_json() {
r#"hobbies:='["http", "pies"]'"#,
r#"utils:='{"tool": "HTTPie"}'"#,
r#"favorite:='{"tool": {"all":[true, 29, {"name": ["Mary", "John"]}]}}'"#,
"--body",
];
let output = process(input).unwrap();
//assert!(output.len() == 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ expression: output
body:
Json:
Hello: World

print_body_only: false
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ expression: output.unwrap()
age: "40"
job: Dev
name: Thales

print_body_only: false
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ expression: output
headers: {}
body:
Raw: ""

print_body_only: false
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ expression: output
headers: {}
body:
Raw: ""

print_body_only: false
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ expression: output
headers: {}
body:
Raw: ""

print_body_only: false
Loading

0 comments on commit eea6ef7

Please sign in to comment.