Skip to content

Commit

Permalink
feat: initial implementation of auth editor
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed Jun 27, 2024
1 parent a747fd1 commit 658f7e5
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 34 deletions.
5 changes: 5 additions & 0 deletions hac-client/src/pages/collection_viewer/collection_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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")),
Expand All @@ -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,
Expand All @@ -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(),
Expand Down
4 changes: 1 addition & 3 deletions hac-client/src/pages/collection_viewer/request_editor.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<RefCell<CollectionStore>>,
}

impl<'ae> AuthEditor<'ae> {
pub fn new(
colors: &'ae hac_colors::colors::Colors,
collection_store: Rc<RefCell<CollectionStore>>,
) -> 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<Option<Self::Result>> {
Ok(None)
}
}
Original file line number Diff line number Diff line change
@@ -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<Option<Self::Result>> {
match key_event.code {
KeyCode::Enter => {}
KeyCode::Char('h') => {}
_ => {}
}

Ok(None)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[allow(clippy::module_inception)]
mod auth_editor;
mod auth_kind_prompt;

pub use auth_editor::AuthEditor;
Original file line number Diff line number Diff line change
@@ -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::*;
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
8 changes: 8 additions & 0 deletions hac-core/src/collection/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<HeaderMap>>,
/// auth method used by the request, eg: Bearer or basic auth
pub auth_method: Option<AuthMethod>,
/// 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
Expand All @@ -181,6 +183,12 @@ pub struct Request {
pub body_type: Option<BodyType>,
}

#[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 {
Expand Down

0 comments on commit 658f7e5

Please sign in to comment.