diff --git a/internal/auth0/auth0.go b/internal/auth0/auth0.go index 17b60787a..537d840ee 100644 --- a/internal/auth0/auth0.go +++ b/internal/auth0/auth0.go @@ -27,6 +27,8 @@ type API struct { Tenant TenantAPI User UserAPI Jobs JobsAPI + + HTTPClient HTTPClientAPI } func NewAPI(m *management.Management) *API { @@ -50,6 +52,7 @@ func NewAPI(m *management.Management) *API { Tenant: m.Tenant, User: m.User, Jobs: m.Job, + HTTPClient: m, } } diff --git a/internal/auth0/http_client.go b/internal/auth0/http_client.go new file mode 100644 index 000000000..cb08b1527 --- /dev/null +++ b/internal/auth0/http_client.go @@ -0,0 +1,17 @@ +package auth0 + +import ( + "net/http" + + "github.com/auth0/go-auth0/management" +) + +type HTTPClientAPI interface { + // NewRequest returns a new HTTP request. + // If the payload is not nil it will be encoded as JSON. + NewRequest(method, uri string, payload interface{}, options ...management.RequestOption) (*http.Request, error) + + // Do triggers an HTTP request and returns an HTTP response, + // handling any context cancellations or timeouts. + Do(req *http.Request) (*http.Response, error) +} diff --git a/internal/cli/api.go b/internal/cli/api.go index 41a59896b..cae7ac4db 100644 --- a/internal/cli/api.go +++ b/internal/cli/api.go @@ -9,10 +9,10 @@ import ( "net/url" "strings" + "github.com/auth0/go-auth0/management" "github.com/spf13/cobra" "github.com/auth0/auth0-cli/internal/ansi" - "github.com/auth0/auth0-cli/internal/buildinfo" "github.com/auth0/auth0-cli/internal/display" "github.com/auth0/auth0-cli/internal/iostream" "github.com/auth0/auth0-cli/internal/prompt" @@ -62,7 +62,7 @@ type ( RawQueryParams map[string]string Method string URL *url.URL - Data io.Reader + Data []byte } ) @@ -131,26 +131,32 @@ func apiCmdRun(cli *cli, inputs *apiCmdInputs) func(cmd *cobra.Command, args []s var response *http.Response if err := ansi.Waiting(func() error { - request, err := http.NewRequestWithContext( - cmd.Context(), + var data interface{} + if string(inputs.Data) != "" { + if err := json.Unmarshal(inputs.Data, &data); err != nil { + return fmt.Errorf("invalid json provided: %w", err) + } + } + + request, err := cli.api.HTTPClient.NewRequest( inputs.Method, inputs.URL.String(), - inputs.Data, + data, + management.Context(cmd.Context()), ) if err != nil { return err } - accessToken := getAccessToken(cli.config.Tenants[cli.tenant]) - request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken)) - request.Header.Set("Content-Type", "application/json") - request.Header.Set("User-Agent", fmt.Sprintf("%s/%s", userAgent, strings.TrimPrefix(buildinfo.Version, "v"))) - if cli.debug { - cli.renderer.Infof("[%s]: %s", request.Method, request.URL.String()) + cli.renderer.Infof("Sending the following request: %+v", map[string]interface{}{ + "method": request.Method, + "url": request.URL.String(), + "payload": data, + }) } - response, err = http.DefaultClient.Do(request) + response, err = cli.api.HTTPClient.Do(request) return err }); err != nil { return fmt.Errorf("failed to send request: %w", err) @@ -236,7 +242,7 @@ func (i *apiCmdInputs) validateAndSetData() error { return fmt.Errorf("invalid json data given: %s", data) } - i.Data = bytes.NewReader(data) + i.Data = data return nil }