Skip to content
Open
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
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var (
version = "v0.0.6"
version = "v0.0.7"
)

var versionCmd = &cobra.Command{
Expand All @@ -29,7 +29,7 @@ var versionCmd = &cobra.Command{

func main() {
var data, method, apiKeyPath, header string
var versionFlag bool
var versionFlag, statusFlag, debugFlag bool
var id, secret string

cmd := &cobra.Command{
Expand All @@ -45,7 +45,10 @@ func main() {
return fmt.Errorf("URL is required unless using -v")
}

opts := []transport.Option{}
opts := []transport.Option{
transport.WithDebug(debugFlag),
}

if apiKeyPath != "" {
opts = append(opts, transport.WithAPIKeyLoaderOption(transport.WithPath(apiKeyPath)))
}
Expand Down Expand Up @@ -94,8 +97,12 @@ func main() {
return err
}

// Print HTTP status code and response body
fmt.Println(resp.Status)
// Print HTTP status code only if the status flag is set
if statusFlag {
fmt.Println(resp.Status)
}

// Always print the response body
fmt.Println(string(body))
return nil
},
Expand All @@ -108,6 +115,8 @@ func main() {
cmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Print the version number and exit")
cmd.Flags().StringVarP(&id, "id", "i", "", "API Key ID (only works with Ed25519 keys)")
cmd.Flags().StringVarP(&secret, "secret", "s", "", "API Key Secret (only works with Ed25519 keys)")
cmd.Flags().BoolVarP(&statusFlag, "status", "S", false, "Print the HTTP status code in addition to the response body")
cmd.Flags().BoolVarP(&debugFlag, "debug", "D", false, "Enable debug mode")

cmd.AddCommand(versionCmd)

Expand Down
14 changes: 14 additions & 0 deletions transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ type transport struct {
originalTransport http.RoundTripper
authenticator *auth.Authenticator
serviceName string
debug bool
}

type Option func(o *options)

type options struct {
apiKey *auth.APIKey
debug bool

apiKeyOptions []auth.LoadAPIKeyOption
}
Expand All @@ -43,6 +45,12 @@ func WithAPIKey(apiKey *auth.APIKey) Option {
}
}

func WithDebug(debug bool) Option {
return func(o *options) {
o.debug = debug
}
}

func New(service string, originalTransport http.RoundTripper, opts ...Option) (http.RoundTripper, error) {
o := &options{
apiKeyOptions: make([]auth.LoadAPIKeyOption, 0),
Expand All @@ -65,6 +73,7 @@ func New(service string, originalTransport http.RoundTripper, opts ...Option) (h
originalTransport: originalTransport,
authenticator: authenticator,
serviceName: service,
debug: o.debug,
}, nil
}

Expand All @@ -76,6 +85,11 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
if err != nil {
return nil, err
}

if t.debug {
fmt.Println(jwt)
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", jwt))
return t.originalTransport.RoundTrip(req)
}