diff --git a/internal/cli/api.go b/internal/cli/api.go index 822f08ab2..c8f4a28e7 100644 --- a/internal/cli/api.go +++ b/internal/cli/api.go @@ -26,6 +26,14 @@ var apiFlags = apiCmdFlags{ IsRequired: false, AlwaysPrompt: false, }, + QueryParams: Flag{ + Name: "QueryParams", + LongForm: "query", + ShortForm: "q", + Help: "Query params to send with the request.", + IsRequired: false, + AlwaysPrompt: false, + }, } var apiValidMethods = []string{ @@ -38,16 +46,18 @@ var apiValidMethods = []string{ type ( apiCmdFlags struct { - Data Flag + Data Flag + QueryParams Flag } apiCmdInputs struct { - RawMethod string - RawURI string - RawData string - Method string - URL *url.URL - Data io.Reader + RawMethod string + RawURI string + RawData string + RawQueryParams map[string]string + Method string + URL *url.URL + Data io.Reader } ) @@ -69,7 +79,7 @@ The method argument is optional, and when you don’t specify it, the command de "Auth0 Management API Docs:\n", apiDocsURL, "Available Methods:\n", strings.Join(apiValidMethods, ", "), ), - Example: `auth0 api "/stats/daily?from=20221101&to=20221118" + Example: `auth0 api "/stats/daily" -q "from=20221101" -q "to=20221118" auth0 api get "/tenants/settings" auth0 api clients --data "{\"name\":\"ssoTest\",\"app_type\":\"sso_integration\"}" `, @@ -77,6 +87,7 @@ auth0 api clients --data "{\"name\":\"ssoTest\",\"app_type\":\"sso_integration\" } apiFlags.Data.RegisterString(cmd, &inputs.RawData, "") + apiFlags.QueryParams.RegisterStringMap(cmd, &inputs.RawQueryParams, nil) return cmd } @@ -87,6 +98,8 @@ func apiCmdRun(cli *cli, inputs *apiCmdInputs) func(cmd *cobra.Command, args []s return fmt.Errorf("failed to parse command inputs: %w", err) } + cli.renderer.Infof("URL: %s", inputs.URL.String()) + var response *http.Response if err := ansi.Waiting(func() error { request, err := http.NewRequestWithContext( @@ -172,6 +185,12 @@ func (i *apiCmdInputs) validateAndSetEndpoint(domain string) error { return fmt.Errorf("invalid uri given: %w", err) } + params := endpoint.Query() + for key, value := range i.RawQueryParams { + params.Set(key, value) + } + endpoint.RawQuery = params.Encode() + i.URL = endpoint return nil