Skip to content

Commit

Permalink
wip: auth method selector
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed Jul 7, 2024
1 parent 09cbf4d commit 85c2fa6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
2 changes: 2 additions & 0 deletions hac-client/benches/collection_viewer_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn create_sample_collection() -> Collection {
headers: None,
name: "testing".to_string(),
parent: None,
auth_method: None,
uri: "https://jsonplaceholder.typicode.com/users".to_string(),
method: RequestMethod::Get,
body: Some("[\r\n {\r\n \"id\": 1,\r\n \"name\": \"Leanne Graham\",\r\n \"username\": \"Bret\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Kulas Light\",\r\n \"suite\": \"Apt. 556\",\r\n \"city\": \"Gwenborough\",\r\n \"zipcode\": \"92998-3874\",\r\n \"geo\": {\r\n \"lat\": \"-37.3159\",\r\n \"lng\": \"81.1496\"\r\n }\r\n },\r\n \"phone\": \"1-770-736-8031 x56442\",\r\n \"website\": \"hildegard.org\",\r\n \"company\": {\r\n \"name\": \"Romaguera-Crona\",\r\n \"catchPhrase\": \"Multi-layered client-server neural-net\",\r\n \"bs\": \"harness real-time e-markets\"\r\n }\r\n },\r\n {\r\n \"id\": 2,\r\n \"name\": \"Ervin Howell\",\r\n \"username\": \"Antonette\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Victor Plains\",\r\n \"suite\": \"Suite 879\",\r\n \"city\": \"Wisokyburgh\",\r\n \"zipcode\": \"90566-7771\",\r\n \"geo\": {\r\n \"lat\": \"-43.9509\",\r\n \"lng\": \"-34.4618\"\r\n }\r\n },\r\n \"phone\": \"010-692-6593 x09125\",\r\n \"website\": \"anastasia.net\",\r\n \"company\": {\r\n \"name\": \"Deckow-Crist\",\r\n \"catchPhrase\": \"Proactive didactic contingency\",\r\n \"bs\": \"synergize scalable supply-chains\"\r\n }\r\n },\r\n {\r\n \"id\": 3,\r\n \"name\": \"Clementine Bauch\",\r\n \"username\": \"Samantha\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Douglas Extension\",\r\n \"suite\": \"Suite 847\",\r\n \"city\": \"McKenziehaven\",\r\n \"zipcode\": \"59590-4157\",\r\n \"geo\": {\r\n \"lat\": \"-68.6102\",\r\n \"lng\": \"-47.0653\"\r\n }\r\n },\r\n \"phone\": \"1-463-123-4447\",\r\n \"website\": \"ramiro.info\",\r\n \"company\": {\r\n \"name\": \"Romaguera-Jacobson\",\r\n \"catchPhrase\": \"Face to face bifurcated interface\",\r\n \"bs\": \"e-enable strategic applications\"\r\n }\r\n },\r\n {\r\n \"id\": 4,\r\n \"name\": \"Patricia Lebsack\",\r\n \"username\": \"Karianne\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Hoeger Mall\",\r\n \"suite\": \"Apt. 692\",\r\n \"city\": \"South Elvis\",\r\n \"zipcode\": \"53919-4257\",\r\n \"geo\": {\r\n \"lat\": \"29.4572\",\r\n \"lng\": \"-164.2990\"\r\n }\r\n },\r\n \"phone\": \"493-170-9623 x156\",\r\n \"website\": \"kale.biz\",\r\n \"company\": {\r\n \"name\": \"Robel-Corkery\",\r\n \"catchPhrase\": \"Multi-tiered zero tolerance productivity\",\r\n \"bs\": \"transition cutting-edge web services\"\r\n }\r\n },\r\n {\r\n \"id\": 5,\r\n \"name\": \"Chelsey Dietrich\",\r\n \"username\": \"Kamren\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Skiles Walks\",\r\n \"suite\": \"Suite 351\",\r\n \"city\": \"Roscoeview\",\r\n \"zipcode\": \"33263\",\r\n \"geo\": {\r\n \"lat\": \"-31.8129\",\r\n \"lng\": \"62.5342\"\r\n }\r\n },\r\n \"phone\": \"(254)954-1289\",\r\n \"website\": \"demarco.info\",\r\n \"company\": {\r\n \"name\": \"Keebler LLC\",\r\n \"catchPhrase\": \"User-centric fault-tolerant solution\",\r\n \"bs\": \"revolutionize end-to-end systems\"\r\n }\r\n },\r\n {\r\n \"id\": 6,\r\n \"name\": \"Mrs. Dennis Schulist\",\r\n \"username\": \"Leopoldo_Corkery\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Norberto Crossing\",\r\n \"suite\": \"Apt. 950\",\r\n \"city\": \"South Christy\",\r\n \"zipcode\": \"23505-1337\",\r\n \"geo\": {\r\n \"lat\": \"-71.4197\",\r\n \"lng\": \"71.7478\"\r\n }\r\n },\r\n \"phone\": \"1-477-935-8478 x6430\",\r\n \"website\": \"ola.org\",\r\n \"company\": {\r\n \"name\": \"Considine-Lockman\",\r\n \"catchPhrase\": \"Synchronised bottom-line interface\",\r\n \"bs\": \"e-enable innovative applications\"\r\n }\r\n },\r\n {\r\n \"id\": 7,\r\n \"name\": \"Kurtis Weissnat\",\r\n \"username\": \"Elwyn.Skiles\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Rex Trail\",\r\n \"suite\": \"Suite 280\",\r\n \"city\": \"Howemouth\",\r\n \"zipcode\": \"58804-1099\",\r\n \"geo\": {\r\n \"lat\": \"24.8918\",\r\n \"lng\": \"21.8984\"\r\n }\r\n },\r\n \"phone\": \"210.067.6132\",\r\n \"website\": \"elvis.io\",\r\n \"company\": {\r\n \"name\": \"Johns Group\",\r\n \"catchPhrase\": \"Configurable multimedia task-force\",\r\n \"bs\": \"generate enterprise e-tailers\"\r\n }\r\n },\r\n {\r\n \"id\": 8,\r\n \"name\": \"Nicholas Runolfsdottir V\",\r\n \"username\": \"Maxime_Nienow\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Ellsworth Summit\",\r\n \"suite\": \"Suite 729\",\r\n \"city\": \"Aliyaview\",\r\n \"zipcode\": \"45169\",\r\n \"geo\": {\r\n \"lat\": \"-14.3990\",\r\n \"lng\": \"-120.7677\"\r\n }\r\n },\r\n \"phone\": \"586.493.6943 x140\",\r\n \"website\": \"jacynthe.com\",\r\n \"company\": {\r\n \"name\": \"Abernathy Group\",\r\n \"catchPhrase\": \"Implemented secondary concept\",\r\n \"bs\": \"e-enable extensible e-tailers\"\r\n }\r\n },\r\n {\r\n \"id\": 9,\r\n \"name\": \"Glenna Reichert\",\r\n \"username\": \"Delphine\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Dayna Park\",\r\n \"suite\": \"Suite 449\",\r\n \"city\": \"Bartholomebury\",\r\n \"zipcode\": \"76495-3109\",\r\n \"geo\": {\r\n \"lat\": \"24.6463\",\r\n \"lng\": \"-168.8889\"\r\n }\r\n },\r\n \"phone\": \"(775)976-6794 x41206\",\r\n \"website\": \"conrad.com\",\r\n \"company\": {\r\n \"name\": \"Yost and Sons\",\r\n \"catchPhrase\": \"Switchable contextually-based project\",\r\n \"bs\": \"aggregate real-time technologies\"\r\n }\r\n },\r\n {\r\n \"id\": 10,\r\n \"name\": \"Clementina DuBuque\",\r\n \"username\": \"Moriah.Stanton\",\r\n \"email\": \"[email protected]\",\r\n \"address\": {\r\n \"street\": \"Kattie Turnpike\",\r\n \"suite\": \"Suite 198\",\r\n \"city\": \"Lebsackbury\",\r\n \"zipcode\": \"31428-2261\",\r\n \"geo\": {\r\n \"lat\": \"-38.2386\",\r\n \"lng\": \"57.2232\"\r\n }\r\n },\r\n \"phone\": \"024-648-3804\",\r\n \"website\": \"ambrose.net\",\r\n \"company\": {\r\n \"name\": \"Hoeger LLC\",\r\n \"catchPhrase\": \"Centralized empowering task-force\",\r\n \"bs\": \"target end-to-end models\"\r\n }\r\n }\r\n]".to_string()),
Expand All @@ -40,6 +41,7 @@ fn create_sample_collection() -> Collection {
RequestKind::Single(Arc::new(RwLock::new(Request {
id: "any_other_id".to_string(),
name: "testing".to_string(),
auth_method: None,
uri: "https://jsonplaceholder.typicode.com/users".to_string(),
method: RequestMethod::Get,
parent: None,
Expand Down
4 changes: 4 additions & 0 deletions hac-client/src/pages/collection_viewer/collection_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum CollectionViewerOverlay {
CreateDirectory,
HeadersHelp,
HeadersDelete,
ChangeAuthMethod,
HeadersForm(usize),
DeleteSidebarItem(String),
}
Expand Down Expand Up @@ -311,6 +312,9 @@ impl Renderable for CollectionViewer<'_> {
CollectionViewerOverlay::HeadersForm(_) => {
self.request_editor.draw_overlay(frame, overlay)?;
}
CollectionViewerOverlay::ChangeAuthMethod => {
self.request_editor.draw_overlay(frame, overlay)?;
}
CollectionViewerOverlay::None => {}
}

Expand Down
14 changes: 8 additions & 6 deletions hac-client/src/pages/collection_viewer/request_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod auth_editor;
mod body_editor;
mod headers_editor;

use auth_editor::AuthEditor;
use auth_editor::{AuthEditor, AuthEditorEvent};
use body_editor::{BodyEditor, BodyEditorEvent};
use hac_config::EditorMode;
use hac_core::collection::types::{Request, RequestMethod};
Expand Down Expand Up @@ -221,7 +221,7 @@ impl<'re> RequestEditor<'re> {
ReqEditorTabs::Body => todo!(),
ReqEditorTabs::Headers => self.headers_editor.draw_overlay(frame, overlay),
ReqEditorTabs::Query => todo!(),
ReqEditorTabs::Auth => todo!(),
ReqEditorTabs::Auth => self.auth_editor.draw_overlay(frame, overlay),
}
}
}
Expand Down Expand Up @@ -289,11 +289,13 @@ impl Eventful for RequestEditor<'_> {
None => {}
},
ReqEditorTabs::Query => {}
ReqEditorTabs::Auth => {
if (self.auth_editor.handle_key_event(key_event)?).is_some() {
todo!()
ReqEditorTabs::Auth => match self.auth_editor.handle_key_event(key_event)? {
Some(AuthEditorEvent::ChangeAuthMethod) => {
let mut store = self.collection_store.borrow_mut();
store.push_overlay(CollectionViewerOverlay::ChangeAuthMethod);
}
}
None => {}
},
}

Ok(None)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
use super::auth_kind_prompt::AuthKindPrompt;
use crate::pages::collection_viewer::collection_store::CollectionStore;
use crate::pages::collection_viewer::collection_viewer::CollectionViewerOverlay;
use crate::pages::{Eventful, Renderable};

use std::cell::RefCell;
use std::ops::Sub;
use std::ops::{Add, Sub};
use std::rc::Rc;

use crossterm::event::KeyEvent;
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::layout::Rect;
use ratatui::style::Stylize;
use ratatui::widgets::{Block, Borders, Paragraph};
use ratatui::Frame;

pub enum AuthEditorEvent {
ChangeAuthMethod,
}

#[derive(Debug)]
pub struct AuthEditor<'ae> {
colors: &'ae hac_colors::colors::Colors,
collection_store: Rc<RefCell<CollectionStore>>,
auth_kind_prompt: AuthKindPrompt<'ae>,
}

impl<'ae> AuthEditor<'ae> {
Expand All @@ -23,6 +30,7 @@ impl<'ae> AuthEditor<'ae> {
collection_store: Rc<RefCell<CollectionStore>>,
) -> Self {
AuthEditor {
auth_kind_prompt: AuthKindPrompt::new(colors, collection_store.clone()),
colors,
collection_store,
}
Expand All @@ -48,6 +56,20 @@ impl<'ae> AuthEditor<'ae> {
hint_size,
);
}

pub fn draw_overlay(
&mut self,
frame: &mut Frame,
overlay: CollectionViewerOverlay,
) -> anyhow::Result<()> {
match overlay {
CollectionViewerOverlay::ChangeAuthMethod => {
self.auth_kind_prompt.draw(frame, frame.size())?;
}
_ => {}
}
Ok(())
}
}

impl Renderable for AuthEditor<'_> {
Expand All @@ -70,7 +92,7 @@ impl Renderable for AuthEditor<'_> {
.borders(Borders::ALL),
);

let size = Rect::new(size.x, size.y, size.width.sub(10), 3);
let size = Rect::new(size.x.add(5), size.y, size.width.sub(10), 3);
frame.render_widget(no_request, size);
return Ok(());
}
Expand All @@ -80,9 +102,21 @@ impl Renderable for AuthEditor<'_> {
}

impl Eventful for AuthEditor<'_> {
type Result = ();
type Result = AuthEditorEvent;

fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Self::Result>> {
let overlay = self.collection_store.borrow().peek_overlay();

if let CollectionViewerOverlay::ChangeAuthMethod = overlay {
self.collection_store.borrow_mut().pop_overlay();
return Ok(None);
}

match key_event.code {
KeyCode::Char('e') => return Ok(Some(AuthEditorEvent::ChangeAuthMethod)),
_ => {}
}

fn handle_key_event(&mut self, _key_event: KeyEvent) -> anyhow::Result<Option<Self::Result>> {
Ok(None)
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
use crate::pages::{Eventful, Renderable};
use crate::pages::collection_viewer::collection_store::CollectionStore;
use crate::pages::{overlay::make_overlay, Eventful, Renderable};

use std::cell::RefCell;
use std::rc::Rc;

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::layout::Rect;
use ratatui::Frame;

pub enum AuthKindPromptEvent {
Placeholder,
Cancel,
}

pub struct AuthKindPrompt {}
#[derive(Debug)]
pub struct AuthKindPrompt<'akp> {
colors: &'akp hac_colors::Colors,
collection_store: Rc<RefCell<CollectionStore>>,
}

impl Renderable for AuthKindPrompt {
impl<'akp> AuthKindPrompt<'akp> {
pub fn new(
colors: &'akp hac_colors::Colors,
collection_store: Rc<RefCell<CollectionStore>>,
) -> AuthKindPrompt {
AuthKindPrompt {
colors,
collection_store,
}
}
}

impl Renderable for AuthKindPrompt<'_> {
fn draw(&mut self, frame: &mut Frame, _: Rect) -> anyhow::Result<()> {
make_overlay(self.colors, self.colors.normal.black, 0.1, frame);

Ok(())
}
}

impl Eventful for AuthKindPrompt {
impl Eventful for AuthKindPrompt<'_> {
type Result = AuthKindPromptEvent;

fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Self::Result>> {
match key_event.code {
KeyCode::Esc => return Ok(Some(AuthKindPromptEvent::Cancel)),
KeyCode::Enter => {}
KeyCode::Char('h') => {}
_ => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
mod auth_editor;
mod auth_kind_prompt;

pub use auth_editor::AuthEditor;
pub use auth_editor::{AuthEditor, AuthEditorEvent};

0 comments on commit 85c2fa6

Please sign in to comment.