Skip to content

Commit

Permalink
Added result headers and cookies within new tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-cpsn committed Feb 6, 2024
1 parent a613a4b commit 85e2002
Show file tree
Hide file tree
Showing 14 changed files with 378 additions and 84 deletions.
102 changes: 101 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
crossterm = "0.27.0"
ratatui = "0.26.0"
reqwest = "0.11.24"
reqwest = { version = "0.11.24", features = ["cookies"] }
strum = "0.26.1"
throbber-widgets-tui = "0.3.0"
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
Expand Down
43 changes: 36 additions & 7 deletions src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use ratatui::Terminal;
use reqwest::Method;
use strum::Display;
use tui_textarea::TextArea;
use crate::app::tabs::tabs::RequestTabs;
use crate::request::request::Request;
use crate::app::request_ui::param_tabs::RequestParamsTabs;
use crate::app::request_ui::result_tabs::RequestResultTabs;
use crate::app::request_ui::views::RequestView;
use crate::request::request::{Request, RequestResult};
use crate::utils::stateful_list::StatefulList;
use crate::utils::stateful_scrollbar::StatefulScrollbar;
use crate::utils::text_input::TextInput;
Expand All @@ -15,7 +17,9 @@ pub struct App<'a> {

pub collection: StatefulList<Request<'a>>,

pub request_tab: RequestTabs,
pub request_view: RequestView,
pub request_param_tab: RequestParamsTabs,
pub request_result_tab: RequestResultTabs,

pub new_request_input: TextInput,
pub url_text_input: TextInput,
Expand Down Expand Up @@ -47,21 +51,44 @@ impl App<'_> {
body: Some(String::from(r#"{
"json": 32
}"#)),
result: None,
result: RequestResult {
body: None,
cookies: None,
headers: None,
},
},
Request {
name: "Get User",
url: "http://127.0.0.1:8080/api/get_user",
method: Method::GET,
body: None,
result: None,
result: RequestResult {
body: None,
cookies: None,
headers: None,
},
},
Request {
name: "Rust Homepage",
url: "https://www.rust-lang.org",
method: Method::GET,
body: None,
result: None,
result: RequestResult {
body: None,
cookies: None,
headers: None,
},
},
Request {
name: "Google fr",
url: "https://www.google.fr/",
method: Method::GET,
body: None,
result: RequestResult {
body: None,
cookies: None,
headers: None,
},
},
];

Expand All @@ -74,7 +101,9 @@ impl App<'_> {
selected: None,
last_selected: None,
},
request_tab: Default::default(),
request_view: RequestView::Normal,
request_param_tab: RequestParamsTabs::Params,
request_result_tab: RequestResultTabs::Body,
new_request_input: TextInput {
text: String::new(),
cursor_position: 0,
Expand Down
20 changes: 13 additions & 7 deletions src/app/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};
use std::io::Result;
use tui_textarea::CursorMove;
use crate::app::app::{App, AppState};
use crate::app::tabs::tabs::next_request_tab;
use crate::app::request_ui::param_tabs::next_request_tab;
use crate::app::request_ui::views::next_request_view;

impl App<'_> {
/// Handle events
pub async fn handle_events(&mut self) -> Result<bool> {
if let Event::Key(key) = event::read()? {

let control_pressed: bool = key.modifiers == KeyModifiers::CONTROL;
let shift_pressed: bool = key.modifiers == KeyModifiers::SHIFT;

// Debug tool
//println!("{:?} {:?}", key.modifiers, key.code);

if key.kind == KeyEventKind::Press {
match self.state {
Expand Down Expand Up @@ -47,18 +52,19 @@ impl App<'_> {
AppState::Normal => match key.code {
KeyCode::Char('b') if control_pressed => self.toggle_request_body(),
KeyCode::Char('b') => self.load_request_body_tab(),
KeyCode::Tab => {
self.request_tab = next_request_tab(self.request_tab);
},

KeyCode::PageUp => self.result_scrollbar.page_up(),
KeyCode::PageDown => self.result_scrollbar.page_down(),

KeyCode::BackTab if shift_pressed => self.request_view = next_request_view(self.request_view),
KeyCode::Tab if control_pressed => self.next_request_result_tab(),
KeyCode::Tab => self.request_param_tab = next_request_tab(self.request_param_tab),

KeyCode::Char('u') => self.state = AppState::EditingUrl,
KeyCode::Char('m') => self.modify_request_method(),

KeyCode::Enter => self.send_request().await,

KeyCode::PageUp => self.result_scrollbar.page_up(),
KeyCode::PageDown => self.result_scrollbar.page_down(),

_ => {}

},
Expand Down
53 changes: 39 additions & 14 deletions src/app/logic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use reqwest::{Client, Method};
use tui_textarea::{TextArea};
use crate::app::app::{App, AppState};
use crate::app::tabs::tabs::RequestTabs;
use crate::app::request_ui::param_tabs::RequestParamsTabs;
use crate::request::method::next_method;
use crate::request::request::Request;
use crate::request::request::{Request, RequestResult};

impl<'a> App<'a> {
pub fn select_request(&mut self) {
Expand Down Expand Up @@ -37,7 +37,11 @@ impl<'a> App<'a> {
url: "",
method: Method::GET,
body: None,
result: None,
result: RequestResult {
body: None,
cookies: None,
headers: None
},
};

self.collection.items.push(new_request);
Expand Down Expand Up @@ -68,7 +72,7 @@ impl<'a> App<'a> {
}

pub fn load_request_body_tab(&mut self) {
self.request_tab = RequestTabs::Body;
self.request_param_tab = RequestParamsTabs::Body;

let selected_request_index = self.collection.selected.unwrap();
let selected_request = &self.collection.items[selected_request_index];
Expand Down Expand Up @@ -143,17 +147,38 @@ impl<'a> App<'a> {
request = request.body(body);
}


let result = match request.send().await {
Ok(result) => result.text().await.unwrap(),
Err(error) => error.to_string()
match request.send().await {
Ok(response) => {
let headers = response.headers().clone()
.iter()
.map(|(header_name, header_value)| {
format!("{:?}: {:?}", header_name, header_value)
})
.collect::<Vec<String>>()
.join("\n");

let cookies = response.cookies()
.map(|cookie| {
format!("{}: {}", cookie.name(), cookie.value())
})
.collect::<Vec<String>>()
.join("\n");

let result_body = response.text().await.unwrap();

selected_request.result.body = Some(result_body);
selected_request.result.cookies = Some(cookies);
selected_request.result.headers = Some(headers);
},
Err(error) => {
let result_body = error.to_string();

selected_request.result.body = Some(result_body);
selected_request.result.cookies = None;
selected_request.result.headers = None;
}
};


let lines_count = result.lines().count();

self.result_scrollbar.set_scroll(lines_count);

selected_request.result = Some(result);
self.refresh_result_scrollbar();
}
}
2 changes: 1 addition & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod app;
pub mod events;
pub mod ui;
pub mod logic;
mod tabs;
mod request_ui;
3 changes: 3 additions & 0 deletions src/app/request_ui/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod param_tabs;
pub mod result_tabs;
pub mod views;
Loading

0 comments on commit 85e2002

Please sign in to comment.