Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(http): revert to use http header callback for Authorization #193

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/.codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
informational: true
patch:
default:
informational: true
25 changes: 2 additions & 23 deletions pkg/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -49,21 +48,8 @@ type ApiOptions struct {
HTTPClient func() (*http.Client, error)
}

var logger *slog.Logger

func init() {
var lvl = new(slog.LevelVar)
if os.Getenv("DEBUG") != "" {
lvl.Set(slog.LevelDebug)
} else {
lvl.Set(slog.LevelError + 1)
}
logger = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: lvl,
}))
}

func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command {
// NewCmdAPI creates a new command
func NewCmdAPI(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command {
opts := ApiOptions{
IO: f.IOStreams,
Config: f.Config,
Expand Down Expand Up @@ -244,13 +230,6 @@ func apiRun(opts *ApiOptions) error {
defer opts.IO.StopPager()
}

if host.AccessToken != "" {
requestHeaders = append(requestHeaders, "Authorization: Bearer "+host.AccessToken)
}

logger.Debug("api request", "host", host.APIHostname, "path", requestPath)

// http request & output
template := export.NewTemplate(opts.IO, opts.Template)
resp, err := httpRequest(httpClient, host.APIHostname, method, requestPath, requestBody, requestHeaders)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Test_NewCmdApi(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var opts *ApiOptions
cmd := NewCmdApi(f, func(o *ApiOptions) error {
cmd := NewCmdAPI(f, func(o *ApiOptions) error {
opts = o
return nil
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/factory/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewHTTPClient(io *iostreams.IOStreams, cfg configHTTPClient, appVersion str
api.AddHeaderFunc("Authorization", func(req *http.Request) (string, error) {
hostname := getHost(req)
if accessToken, err := cfg.Get(hostname, "access_token"); err == nil && accessToken != "" {
// Refresh access token everytime
// Refresh access token every time
if accessToken, err = oauth2.RefreshToken(cfg, hostname); err == nil && accessToken != "" {
return fmt.Sprintf("bearer %s", accessToken), nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
bareHTTPCmdFactory := *f
bareHTTPCmdFactory.HTTPClient = bareHTTPClient(f, version)

cmd.AddCommand(apiCmd.NewCmdApi(&bareHTTPCmdFactory, nil))
cmd.AddCommand(apiCmd.NewCmdAPI(&bareHTTPCmdFactory, nil))

// Help topics
cmd.AddCommand(NewHelpTopic("environment"))
Expand Down