Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
PThorpe92 committed Apr 5, 2024
1 parent ded63ba commit 4c84d00
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 95 deletions.
15 changes: 6 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,8 @@ impl<'a> App<'a> {
) => self.go_back_screen(),
Some(Screen::RequestBodyInput) => self.goto_screen(&Screen::Method),
Some(Screen::Error(_)) => self.goto_screen(&Screen::Home),
Some(Screen::RequestMenu(e)) => {
if e.as_ref().is_some() {
self.goto_screen(&Screen::RequestMenu(None));
} else {
self.goto_screen(&Screen::Method);
}
Some(Screen::RequestMenu(_)) => {
self.goto_screen(&Screen::RequestMenu(None));
}
Some(Screen::Method) => self.goto_screen(&Screen::Home),
Some(screen) => {
Expand Down Expand Up @@ -352,12 +348,13 @@ impl<'a> App<'a> {
let file = std::fs::File::open(path)?;
let collection: Result<crate::database::postman::PostmanCollection, String> =
serde_json::from_reader(file).map_err(|e| e.to_string());
if let Ok(collection) = collection {
match collection {
Ok(collection) => {
let name = collection.info.name.clone();
let cmds: Vec<SavedCommand> = collection.into();
self.db.add_collection(&name, cmds.as_slice())
} else {
Err("Failed to import collection".into())
}
Err(e) => Err(e.into()),
}
}

Expand Down
209 changes: 136 additions & 73 deletions src/database/postman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,94 +16,158 @@ impl From<PostmanCollection> for Vec<SavedCommand> {
let mut curl_cmd = Curl::new();
collection.item.iter().for_each(|item| {
if let Some(request) = item.get("request") {
if let Some(method) = request.get("method").unwrap().as_str() {
let method = method.to_uppercase();
match method.as_str() {
"GET" => {
curl_cmd.set_get_method();
}
"POST" => {
curl_cmd.set_post_method();
}
"PUT" => {
curl_cmd.set_put_method();
}
"DELETE" => {
curl_cmd.set_delete_method();
}
"PATCH" => {
curl_cmd.set_patch_method();
if let Some(request) = request.as_str() {
// this means its a get request
curl_cmd.set_url(request);
curl_cmd.set_get_method();
} else if let Some(request) = request.as_object() {
if let Some(method) = request.get("method") {
if let Some(method) = method.as_str() {
match method {
"GET" => {
curl_cmd.set_get_method();
}
"POST" => {
curl_cmd.set_post_method();
}
"PUT" => {
curl_cmd.set_put_method();
}
"DELETE" => {
curl_cmd.set_delete_method();
}
"PATCH" => {
curl_cmd.set_patch_method();
}
"HEAD" => {
curl_cmd.set_head_method();
}
_ => {
curl_cmd.set_get_method();
}
}
}
"HEAD" => {
curl_cmd.set_head_method();
}
if let Some(headers) = request.get("header") {
if let Some(headers) = headers.as_array() {
headers.iter().for_each(|hdr| {
if let Some(hdr) = hdr.as_object() {
if let Some(key) = hdr.get("key") {
if let Some(key) = key.as_str() {
if let Some(value) = hdr.get("value") {
if let Some(value) = value.as_str() {
curl_cmd.add_headers(&format!(
"{}: {}",
key, value
));
}
}
}
}
}
});
}
_ => {
curl_cmd.set_get_method();
}
if let Some(body) = request.get("body") {
if let Some(body) = body.as_object() {
if let Some(mode) = body.get("mode") {
if let Some(mode) = mode.as_str() {
match mode {
"formdata" => {
if let Some(data) = body.get("formdata") {
if let Some(data) = data.as_array() {
let mut form_data = Vec::new();
data.iter().for_each(|d| {
if let Some(d) = d.as_object() {
if let Some(key) = d.get("key") {
if let Some(key) = key.as_str() {
if let Some(value) =
d.get("value")
{
if let Some(value) =
value.as_str()
{
form_data
.push((key, value));
}
}
}
}
}
});
curl_cmd.set_request_body(
&serde_json::to_string(&form_data)
.unwrap_or_default(),
);
}
}
}
"urlencoded" => {
if let Some(data) = body.get("urlencoded") {
if let Some(data) = data.as_array() {
data.iter().for_each(|d| {
if let Some(d) = d.as_object() {
if let Some(key) = d.get("key") {
if let Some(key) = key.as_str() {
if let Some(value) =
d.get("value")
{
if let Some(value) =
value.as_str()
{
curl_cmd.url_encode(
&format!(
"{}={}",
key, value
),
);
}
}
}
}
}
});
}
}
}
"raw" => {
if let Some(data) = body.get("raw") {
if let Some(data) = data.as_str() {
curl_cmd.set_request_body(data);
}
}
}
_ => {}
}
}
}
}
}
}
if let Some(url) = request.get("url") {
} else if let Some(url) = request.get("url") {
if let Some(url) = url.as_object() {
if let Some(url) = url.get("raw") {
let url = url.as_str().unwrap();
curl_cmd.set_url(url);
if let Some(url) = url.as_str() {
curl_cmd.set_url(url);
}
}
}
}
if let Some(cookie) = request.get("cookie") {
if let Some(cookie) = cookie.as_array() {
cookie.iter().for_each(|ck| {
if let Some(ck) = ck.as_object() {
let key = ck.get("key").unwrap().as_str().unwrap();
let value = ck.get("value").unwrap().as_str().unwrap();
curl_cmd.add_cookie(&format!("{}: {}", key, value));
}
});
}
}
if let Some(headers) = request.get("header") {
let headers = headers.as_array().unwrap();
headers.iter().for_each(|hdr| {
if let Some(hdr) = hdr.as_object() {
if let Some(key) = hdr.get("key") {
let key = key.as_str().unwrap();
if let Some(value) = hdr.get("value") {
let value = value.as_str().unwrap();
curl_cmd.add_headers(&format!("{}: {}", key, value));
}
}
}
});
}
}
if let Some(response) = item.get("response") {
if let Some(response) = response.as_array() {
response.iter().for_each(|resp| {
if let Some(resp) = resp.as_object() {
if let Some(body) = resp.get("body") {
if let Some(body) = body.get("raw") {
let body = body.as_str().unwrap();
curl_cmd.set_request_body(body);
}
}
if resp.get("url").is_some() && !curl_cmd.get_url().is_empty() {
let url = resp.get("url").unwrap().as_object().unwrap();
let url = url.get("raw").unwrap().as_str().unwrap();
curl_cmd.set_url(url);
}
if let Some(cookie) = resp.get("cookie") {
if let Some(cookie) = cookie.as_array() {
cookie.iter().for_each(|ck| {
if let Some(ck) = ck.as_object() {
let key = ck.get("key").unwrap().as_str().unwrap();
let value = ck.get("value").unwrap().as_str().unwrap();
curl_cmd.add_cookie(&format!("{}: {}", key, value));
if let Some(key) = ck.get("key") {
if let Some(key) = key.as_str() {
if let Some(value) = ck.get("value") {
if let Some(value) = value.as_str() {
curl_cmd.add_cookie(&format!("{}: {}", key, value));
}
}
});
}
}
}
}
});
});
}
}
}
});
Expand All @@ -116,7 +180,6 @@ impl From<PostmanCollection> for Vec<SavedCommand> {

#[derive(Serialize, Debug, Deserialize)]
pub struct Info {
_postman_id: String,
pub name: String,
schema: String,
}
13 changes: 6 additions & 7 deletions src/display/menuopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub const COLLECTION_ALERT_MENU_OPTS: [&str; 4] = [
pub const REQUEST_MENU_OPTIONS: [&str; 12] = [
"Add a URL 󰖟 ",
"Add a file for uploads  ",
"Cookie options 󰖟  ",
"Cookie options 󰆘 ",
"Authentication 󰯄 ",
"Header Options  ",
"Enable verbose output [-v]",
Expand All @@ -154,11 +154,12 @@ pub const REQUEST_MENU_OPTIONS: [&str; 12] = [
"Clear all options  ",
];

pub const COOKIE_MENU_OPTIONS: [&str; 4] = [
"Set Cookie file path (Use Cookies) 󰖟  ",
"Set Cookie-Jar path (Storage) 󰖟  ",
pub const COOKIE_MENU_OPTIONS: [&str; 5] = [
"Set Cookie file path (Use Cookies) 󰆘 ",
"Set Cookie-Jar path (Storage) 󰆘 ",
"Add New Cookie 󰆘 ",
"Reset Cookie Session 󰆘 ",
"Go back  ",
];

pub const METHOD_MENU_OPTIONS: [&str; 7] = [
Expand All @@ -185,11 +186,9 @@ pub const AUTHENTICATION_MENU_OPTIONS: [&str; 6] = [
"Ntlm",
"SPNEGO",
];
pub const MORE_FLAGS_MENU: [&str; 14] = [
pub const MORE_FLAGS_MENU: [&str; 12] = [
"Follow Redirects 󱀀 ",
"Specify Max redirects 󱀀 ",
"Add New Cookie 󰆘 ",
"Set Cookie-Jar path (Storage) 󰆘 ",
"Enable HTTP Proxy-Tunnel 󱠾 ",
"Unrestricted Auth  ",
"Specify Referrer 󰆽 ",
Expand Down
1 change: 1 addition & 0 deletions src/screens/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn handle_cookies_menu(app: &mut App, frame: &mut Frame<'_>) {
app.add_app_option(AppOptions::NewCookieSession);
app.goto_screen(&Screen::RequestMenu(None));
}
4 => app.goto_screen(&Screen::RequestMenu(None)),
_ => {}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/screens/input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,11 @@ pub fn parse_input(message: String, opt: InputOpt, app: &mut App) {
app.goto_screen(&Screen::RequestMenu(None));
}
InputOpt::ImportCollection => {
if app.import_postman_collection(&message).is_ok() {
if let Err(e) = app.import_postman_collection(&message) {
app.goto_screen(&Screen::Error(e.to_string()));
} else {
app.goto_screen(&Screen::Success);
return;
}
app.goto_screen(&Screen::Error("Failed to import collection".to_string()));
}
InputOpt::CreateCollection => {
if app.create_postman_collection(&message).is_ok() {
Expand Down
2 changes: 1 addition & 1 deletion src/screens/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn handle_request_menu_screen(app: &mut App, frame: &mut Frame<'_>, opt: Opt
}
// Execute command
Some(9) => {
if !app.command.get_url().is_empty() && !app.command.has_unix_socket() {
if app.command.get_url().is_empty() && !app.command.has_unix_socket() {
app.goto_screen(&Screen::RequestMenu(Some(InputOpt::RequestError(
String::from(VALID_COMMAND_ERROR),
))));
Expand Down
3 changes: 1 addition & 2 deletions src/screens/saved_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn handle_saved_commands_screen(app: &mut App, frame: &mut Frame<'_>, coll:
.iter()
.map(|x| {
format!(
"Saved Request: {} Collection: {:?}",
"Request: {} Collection: {:?}",
x.get_command(),
x.get_collection_id()
)
Expand All @@ -27,7 +27,6 @@ pub fn handle_saved_commands_screen(app: &mut App, frame: &mut Frame<'_>, coll:
let mut state = ListState::with_selected(ListState::default(), Some(app.cursor));
app.state = Some(state.clone());
app.state.as_mut().unwrap().select(Some(app.cursor));
frame.set_cursor(0, app.cursor as u16);
frame.render_stateful_widget(menu_options, area, &mut state);
let (paragraph, title) = (&SAVED_COMMANDS_PARAGRAPH, &SAVED_COMMANDS_TITLE);
frame.render_widget(
Expand Down

0 comments on commit 4c84d00

Please sign in to comment.