From 658f7e53f06eae4ab2dac3b47c0c56947ea42a86 Mon Sep 17 00:00:00 2001 From: wiru Date: Thu, 27 Jun 2024 20:35:55 -0300 Subject: [PATCH] feat: initial implementation of auth editor --- .../collection_viewer/collection_store.rs | 5 ++ .../pages/collection_viewer/request_editor.rs | 4 +- .../request_editor/auth_editor.rs | 31 ----------- .../request_editor/auth_editor/auth_editor.rs | 52 +++++++++++++++++++ .../auth_editor/auth_kind_prompt.rs | 31 +++++++++++ .../request_editor/auth_editor/mod.rs | 5 ++ .../{ => headers_editor}/headers_editor.rs | 0 .../headers_editor_delete_prompt.rs | 0 .../headers_editor_edit_form.rs | 0 .../request_editor/headers_editor/mod.rs | 6 +++ .../sidebar/create_request_form.rs | 1 + hac-core/src/collection/types.rs | 8 +++ 12 files changed, 109 insertions(+), 34 deletions(-) delete mode 100755 hac-client/src/pages/collection_viewer/request_editor/auth_editor.rs create mode 100755 hac-client/src/pages/collection_viewer/request_editor/auth_editor/auth_editor.rs create mode 100644 hac-client/src/pages/collection_viewer/request_editor/auth_editor/auth_kind_prompt.rs create mode 100644 hac-client/src/pages/collection_viewer/request_editor/auth_editor/mod.rs rename hac-client/src/pages/collection_viewer/request_editor/{ => headers_editor}/headers_editor.rs (100%) rename hac-client/src/pages/collection_viewer/request_editor/{ => headers_editor}/headers_editor_delete_prompt.rs (100%) rename hac-client/src/pages/collection_viewer/request_editor/{ => headers_editor}/headers_editor_edit_form.rs (100%) create mode 100644 hac-client/src/pages/collection_viewer/request_editor/headers_editor/mod.rs diff --git a/hac-client/src/pages/collection_viewer/collection_store.rs b/hac-client/src/pages/collection_viewer/collection_store.rs index bf643ee..551fb29 100755 --- a/hac-client/src/pages/collection_viewer/collection_store.rs +++ b/hac-client/src/pages/collection_viewer/collection_store.rs @@ -397,6 +397,7 @@ mod tests { id: "root".to_string(), method: RequestMethod::Get, name: "Root1".to_string(), + auth_method: None, parent: None, headers: None, uri: "/root1".to_string(), @@ -408,6 +409,7 @@ mod tests { fn create_child_one() -> RequestKind { RequestKind::Single(Arc::new(RwLock::new(Request { id: "child_one".to_string(), + auth_method: None, parent: Some(String::from("dir")), method: RequestMethod::Post, name: "Child1".to_string(), @@ -422,6 +424,7 @@ mod tests { RequestKind::Single(Arc::new(RwLock::new(Request { id: "child_two".to_string(), method: RequestMethod::Put, + auth_method: None, name: "Child2".to_string(), headers: None, parent: Some(String::from("dir")), @@ -437,6 +440,7 @@ mod tests { method: RequestMethod::Put, name: "NotUsed".to_string(), parent: None, + auth_method: None, headers: None, uri: "/not/used".to_string(), body_type: None, @@ -460,6 +464,7 @@ mod tests { RequestKind::Single(Arc::new(RwLock::new(Request { id: "root_two".to_string(), method: RequestMethod::Delete, + auth_method: None, headers: None, parent: None, name: "Root2".to_string(), diff --git a/hac-client/src/pages/collection_viewer/request_editor.rs b/hac-client/src/pages/collection_viewer/request_editor.rs index 87a08d5..7541b21 100755 --- a/hac-client/src/pages/collection_viewer/request_editor.rs +++ b/hac-client/src/pages/collection_viewer/request_editor.rs @@ -1,8 +1,6 @@ mod auth_editor; mod body_editor; mod headers_editor; -mod headers_editor_delete_prompt; -mod headers_editor_edit_form; use auth_editor::AuthEditor; use body_editor::{BodyEditor, BodyEditorEvent}; @@ -134,7 +132,7 @@ impl<'re> RequestEditor<'re> { collection_store.clone(), layout.content_pane, ), - auth_editor: AuthEditor::new(colors), + auth_editor: AuthEditor::new(colors, collection_store.clone()), layout, curr_tab, collection_store, diff --git a/hac-client/src/pages/collection_viewer/request_editor/auth_editor.rs b/hac-client/src/pages/collection_viewer/request_editor/auth_editor.rs deleted file mode 100755 index 297b58b..0000000 --- a/hac-client/src/pages/collection_viewer/request_editor/auth_editor.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crossterm::event::KeyEvent; -use ratatui::{layout::Rect, widgets::Paragraph, Frame}; - -use crate::pages::{Eventful, Renderable}; - -#[derive(Debug)] -pub struct AuthEditor<'ae> { - _colors: &'ae hac_colors::colors::Colors, -} - -impl<'ae> AuthEditor<'ae> { - pub fn new(colors: &'ae hac_colors::colors::Colors) -> Self { - AuthEditor { _colors: colors } - } -} - -impl Renderable for AuthEditor<'_> { - fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()> { - frame.render_widget(Paragraph::new("hello from auth editor").centered(), size); - - Ok(()) - } -} - -impl Eventful for AuthEditor<'_> { - type Result = (); - - fn handle_key_event(&mut self, _key_event: KeyEvent) -> anyhow::Result> { - Ok(None) - } -} diff --git a/hac-client/src/pages/collection_viewer/request_editor/auth_editor/auth_editor.rs b/hac-client/src/pages/collection_viewer/request_editor/auth_editor/auth_editor.rs new file mode 100755 index 0000000..0f7f2a6 --- /dev/null +++ b/hac-client/src/pages/collection_viewer/request_editor/auth_editor/auth_editor.rs @@ -0,0 +1,52 @@ +use crate::pages::collection_viewer::collection_store::CollectionStore; +use crate::pages::{Eventful, Renderable}; + +use std::cell::RefCell; +use std::rc::Rc; + +use crossterm::event::KeyEvent; +use ratatui::{layout::Rect, widgets::Paragraph, Frame}; + +#[derive(Debug)] +pub struct AuthEditor<'ae> { + _colors: &'ae hac_colors::colors::Colors, + collection_store: Rc>, +} + +impl<'ae> AuthEditor<'ae> { + pub fn new( + colors: &'ae hac_colors::colors::Colors, + collection_store: Rc>, + ) -> Self { + AuthEditor { + _colors: colors, + collection_store, + } + } +} + +impl Renderable for AuthEditor<'_> { + fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()> { + frame.render_widget(Paragraph::new("hello from auth editor").centered(), size); + let store = self.collection_store.borrow(); + + let Some(request) = store.get_selected_request() else { + return Ok(()); + }; + + let request = request.read().unwrap(); + if request.auth_method.is_none() { + return Ok(()); + } + + Ok(()) + } +} + +impl Eventful for AuthEditor<'_> { + type Result = (); + + fn handle_key_event(&mut self, _key_event: KeyEvent) -> anyhow::Result> { + Ok(None) + } +} diff --git a/hac-client/src/pages/collection_viewer/request_editor/auth_editor/auth_kind_prompt.rs b/hac-client/src/pages/collection_viewer/request_editor/auth_editor/auth_kind_prompt.rs new file mode 100644 index 0000000..2e06963 --- /dev/null +++ b/hac-client/src/pages/collection_viewer/request_editor/auth_editor/auth_kind_prompt.rs @@ -0,0 +1,31 @@ +use crate::pages::{Eventful, Renderable}; + +use crossterm::event::{KeyCode, KeyEvent}; +use ratatui::layout::Rect; +use ratatui::Frame; + +pub enum AuthKindPromptEvent { + Placeholder, +} + +pub struct AuthKindPrompt {} + +impl Renderable for AuthKindPrompt { + fn draw(&mut self, frame: &mut Frame, _: Rect) -> anyhow::Result<()> { + Ok(()) + } +} + +impl Eventful for AuthKindPrompt { + type Result = AuthKindPromptEvent; + + fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result> { + match key_event.code { + KeyCode::Enter => {} + KeyCode::Char('h') => {} + _ => {} + } + + Ok(None) + } +} diff --git a/hac-client/src/pages/collection_viewer/request_editor/auth_editor/mod.rs b/hac-client/src/pages/collection_viewer/request_editor/auth_editor/mod.rs new file mode 100644 index 0000000..da0e76f --- /dev/null +++ b/hac-client/src/pages/collection_viewer/request_editor/auth_editor/mod.rs @@ -0,0 +1,5 @@ +#[allow(clippy::module_inception)] +mod auth_editor; +mod auth_kind_prompt; + +pub use auth_editor::AuthEditor; diff --git a/hac-client/src/pages/collection_viewer/request_editor/headers_editor.rs b/hac-client/src/pages/collection_viewer/request_editor/headers_editor/headers_editor.rs similarity index 100% rename from hac-client/src/pages/collection_viewer/request_editor/headers_editor.rs rename to hac-client/src/pages/collection_viewer/request_editor/headers_editor/headers_editor.rs diff --git a/hac-client/src/pages/collection_viewer/request_editor/headers_editor_delete_prompt.rs b/hac-client/src/pages/collection_viewer/request_editor/headers_editor/headers_editor_delete_prompt.rs similarity index 100% rename from hac-client/src/pages/collection_viewer/request_editor/headers_editor_delete_prompt.rs rename to hac-client/src/pages/collection_viewer/request_editor/headers_editor/headers_editor_delete_prompt.rs diff --git a/hac-client/src/pages/collection_viewer/request_editor/headers_editor_edit_form.rs b/hac-client/src/pages/collection_viewer/request_editor/headers_editor/headers_editor_edit_form.rs similarity index 100% rename from hac-client/src/pages/collection_viewer/request_editor/headers_editor_edit_form.rs rename to hac-client/src/pages/collection_viewer/request_editor/headers_editor/headers_editor_edit_form.rs diff --git a/hac-client/src/pages/collection_viewer/request_editor/headers_editor/mod.rs b/hac-client/src/pages/collection_viewer/request_editor/headers_editor/mod.rs new file mode 100644 index 0000000..b073bb4 --- /dev/null +++ b/hac-client/src/pages/collection_viewer/request_editor/headers_editor/mod.rs @@ -0,0 +1,6 @@ +#[allow(clippy::module_inception)] +mod headers_editor; +mod headers_editor_delete_prompt; +mod headers_editor_edit_form; + +pub use headers_editor::*; diff --git a/hac-client/src/pages/collection_viewer/sidebar/create_request_form.rs b/hac-client/src/pages/collection_viewer/sidebar/create_request_form.rs index 4e0c6ca..cc5b46b 100644 --- a/hac-client/src/pages/collection_viewer/sidebar/create_request_form.rs +++ b/hac-client/src/pages/collection_viewer/sidebar/create_request_form.rs @@ -129,6 +129,7 @@ impl Eventful for RequestForm<'_, RequestFormCreate> { let request = RequestKind::Single(Arc::new(RwLock::new(Request { id: uuid::Uuid::new_v4().to_string(), + auth_method: None, body: None, body_type: None, parent: self.parent_dir.as_ref().map(|(id, _)| id.clone()), diff --git a/hac-core/src/collection/types.rs b/hac-core/src/collection/types.rs index dd22fb1..f5222d8 100755 --- a/hac-core/src/collection/types.rs +++ b/hac-core/src/collection/types.rs @@ -168,6 +168,8 @@ pub struct Request { /// all headers used on given request, sometimes, we may include additional /// headers if required to make a request pub headers: Option>, + /// auth method used by the request, eg: Bearer or basic auth + pub auth_method: Option, /// if this request lives as a children of a directory, the uuid of given /// directory will be stored here, this is mainly used to know where to /// insert or move the request @@ -181,6 +183,12 @@ pub struct Request { pub body_type: Option, } +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub enum AuthMethod { + Bearer, + Basic, +} + /// a collection of all available body types we support. #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] pub enum BodyType {