Skip to content

Commit

Permalink
feat: better handle headers
Browse files Browse the repository at this point in the history
  • Loading branch information
jackMort committed Nov 15, 2024
1 parent c9def8d commit 56a856a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
15 changes: 11 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,18 @@ func (a *App) GetResponse(call *Call) tea.Cmd {
},
// fetch response
func() tea.Msg {
headers := make(map[string]string)
for _, h := range call.Headers {
header := strings.Split(h, ":")
if len(header) > 1 {
headers[header[0]] = header[1]
}
}

params := utils.HTTPRequestParams{
Method: call.Method,
URL: call.GetUrl(),
Headers: map[string]string{
// "Content-Type": "application/json",
},
}
Headers: headers}

if call.Data != "" {
params.Body = strings.NewReader(call.Data)
Expand All @@ -276,6 +281,8 @@ func (a *App) GetResponse(call *Call) tea.Cmd {
if auth.Type == "basic_auth" {
params.Username = auth.Username
params.Password = auth.Password
} else if auth.Type == "bearer_token" {
params.Headers["Authorization"] = fmt.Sprintf("Bearer %s", auth.Token)
}
}

Expand Down
19 changes: 18 additions & 1 deletion cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"restman/components/request"
"restman/components/results"
"restman/components/url"
"strings"

tea "github.com/charmbracelet/bubbletea"
zone "github.com/lrstanley/bubblezone"
Expand Down Expand Up @@ -69,7 +70,23 @@ Restman is a CLI tool for RESTful API.`,

headers, _ := cmd.Flags().GetStringArray("header")
if headers != nil {
call.Headers = headers
// split headers into key-value pairs
// to check authorization for bearer token
processed_headers := []string{}
for _, h := range headers {
if h == "" {
continue
}
pair := strings.Split(h, ":")
if len(pair) == 2 {
if strings.ToLower(pair[0]) == "authorization" && strings.Contains(pair[1], "Bearer") {
call.Auth = &app.Auth{Type: "bearer_token", Token: strings.TrimSpace(strings.ReplaceAll(pair[1], "Bearer", ""))}
continue
}
}
processed_headers = append(processed_headers, h)
}
call.Headers = processed_headers
}

viper.SetConfigName("config") // name of config file (without extension)
Expand Down
42 changes: 31 additions & 11 deletions components/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ var styleBase = lipgloss.NewStyle().
Bold(false).
BorderForeground(config.COLOR_SUBTLE)

const (
view = iota
add
edit
)

type Model struct {
mode int
width int
height int
simpleTable table.Model
Expand Down Expand Up @@ -49,6 +56,7 @@ func New(call *app.Call, width int, height int) Model {
}

return Model{
mode: view,
call: call,
width: width,
height: height,
Expand All @@ -75,21 +83,29 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
{
switch msg.String() {
case "a":
if m.mode == view {
m.mode = add

}

case "x":
key := strings.TrimSpace(m.simpleTable.HighlightedRow().Data[columnKeyKey].(string))
headers := []string{}
for _, header := range m.call.Headers {
if key != strings.Split(header, ":")[0] {
headers = append(headers, header)
if m.call != nil && m.call.HeadersCount() > 0 {
key := strings.TrimSpace(m.simpleTable.HighlightedRow().Data[columnKeyKey].(string))
headers := []string{}
for _, header := range m.call.Headers {
if key != strings.Split(header, ":")[0] {
headers = append(headers, header)
}
}
}
m.call.Headers = headers
m.call.Headers = headers

cmd := func() tea.Msg {
return app.CallUpdatedMsg{Call: m.call}
cmd := func() tea.Msg {
return app.CallUpdatedMsg{Call: m.call}
}
m.simpleTable = m.simpleTable.WithRows(GetRows(m.call.Headers))
cmds = append(cmds, cmd)
}
m.simpleTable = m.simpleTable.WithRows(GetRows(m.call.Headers))
cmds = append(cmds, cmd)
}
}
case app.CallUpdatedMsg:
Expand All @@ -104,6 +120,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m Model) View() string {
if m.mode == add {
return config.BoxHeader.Render("Add Header")
}

content := config.EmptyMessageStyle.Padding(2, 2).Render("No headers defined.")
if m.call != nil && len(m.call.Headers) > 0 {
content = m.simpleTable.View()
Expand Down

0 comments on commit 56a856a

Please sign in to comment.