Skip to content

Commit 56a856a

Browse files
committed
feat: better handle headers
1 parent c9def8d commit 56a856a

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

app/app.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,18 @@ func (a *App) GetResponse(call *Call) tea.Cmd {
259259
},
260260
// fetch response
261261
func() tea.Msg {
262+
headers := make(map[string]string)
263+
for _, h := range call.Headers {
264+
header := strings.Split(h, ":")
265+
if len(header) > 1 {
266+
headers[header[0]] = header[1]
267+
}
268+
}
269+
262270
params := utils.HTTPRequestParams{
263271
Method: call.Method,
264272
URL: call.GetUrl(),
265-
Headers: map[string]string{
266-
// "Content-Type": "application/json",
267-
},
268-
}
273+
Headers: headers}
269274

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

cmds.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"restman/components/request"
1313
"restman/components/results"
1414
"restman/components/url"
15+
"strings"
1516

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

7071
headers, _ := cmd.Flags().GetStringArray("header")
7172
if headers != nil {
72-
call.Headers = headers
73+
// split headers into key-value pairs
74+
// to check authorization for bearer token
75+
processed_headers := []string{}
76+
for _, h := range headers {
77+
if h == "" {
78+
continue
79+
}
80+
pair := strings.Split(h, ":")
81+
if len(pair) == 2 {
82+
if strings.ToLower(pair[0]) == "authorization" && strings.Contains(pair[1], "Bearer") {
83+
call.Auth = &app.Auth{Type: "bearer_token", Token: strings.TrimSpace(strings.ReplaceAll(pair[1], "Bearer", ""))}
84+
continue
85+
}
86+
}
87+
processed_headers = append(processed_headers, h)
88+
}
89+
call.Headers = processed_headers
7390
}
7491

7592
viper.SetConfigName("config") // name of config file (without extension)

components/headers/headers.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ var styleBase = lipgloss.NewStyle().
2020
Bold(false).
2121
BorderForeground(config.COLOR_SUBTLE)
2222

23+
const (
24+
view = iota
25+
add
26+
edit
27+
)
28+
2329
type Model struct {
30+
mode int
2431
width int
2532
height int
2633
simpleTable table.Model
@@ -49,6 +56,7 @@ func New(call *app.Call, width int, height int) Model {
4956
}
5057

5158
return Model{
59+
mode: view,
5260
call: call,
5361
width: width,
5462
height: height,
@@ -75,21 +83,29 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7583
case tea.KeyMsg:
7684
{
7785
switch msg.String() {
86+
case "a":
87+
if m.mode == view {
88+
m.mode = add
89+
90+
}
91+
7892
case "x":
79-
key := strings.TrimSpace(m.simpleTable.HighlightedRow().Data[columnKeyKey].(string))
80-
headers := []string{}
81-
for _, header := range m.call.Headers {
82-
if key != strings.Split(header, ":")[0] {
83-
headers = append(headers, header)
93+
if m.call != nil && m.call.HeadersCount() > 0 {
94+
key := strings.TrimSpace(m.simpleTable.HighlightedRow().Data[columnKeyKey].(string))
95+
headers := []string{}
96+
for _, header := range m.call.Headers {
97+
if key != strings.Split(header, ":")[0] {
98+
headers = append(headers, header)
99+
}
84100
}
85-
}
86-
m.call.Headers = headers
101+
m.call.Headers = headers
87102

88-
cmd := func() tea.Msg {
89-
return app.CallUpdatedMsg{Call: m.call}
103+
cmd := func() tea.Msg {
104+
return app.CallUpdatedMsg{Call: m.call}
105+
}
106+
m.simpleTable = m.simpleTable.WithRows(GetRows(m.call.Headers))
107+
cmds = append(cmds, cmd)
90108
}
91-
m.simpleTable = m.simpleTable.WithRows(GetRows(m.call.Headers))
92-
cmds = append(cmds, cmd)
93109
}
94110
}
95111
case app.CallUpdatedMsg:
@@ -104,6 +120,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
104120
}
105121

106122
func (m Model) View() string {
123+
if m.mode == add {
124+
return config.BoxHeader.Render("Add Header")
125+
}
126+
107127
content := config.EmptyMessageStyle.Padding(2, 2).Render("No headers defined.")
108128
if m.call != nil && len(m.call.Headers) > 0 {
109129
content = m.simpleTable.View()

0 commit comments

Comments
 (0)