-
Notifications
You must be signed in to change notification settings - Fork 105
feat: add api command #1457
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
base: main
Are you sure you want to change the base?
feat: add api command #1457
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "net/url" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/hetznercloud/cli/internal/cmd/base" | ||
| "github.com/hetznercloud/cli/internal/hcapi2" | ||
| "github.com/hetznercloud/cli/internal/state" | ||
| ) | ||
|
|
||
| var APICmd = base.Cmd{ | ||
| BaseCobraCommand: func(_ hcapi2.Client) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "api [options] <path>", | ||
| Short: "Make API call", | ||
| } | ||
|
|
||
| cmd.Flags().StringP("method", "X", "GET", "HTTP method to use for API call") | ||
| cmd.Flags().StringP("data", "d", "", "HTTP request body content (use - to read from stdin)") | ||
| cmd.Flags().StringToStringP("value", "v", nil, "key=value pairs to pass as query parameters") | ||
| return cmd | ||
| }, | ||
| Run: func(s state.State, cmd *cobra.Command, args []string) error { | ||
| path := args[0] | ||
| method, _ := cmd.Flags().GetString("method") | ||
| data, _ := cmd.Flags().GetString("data") | ||
| values, _ := cmd.Flags().GetStringToString("value") | ||
|
|
||
| var body io.Reader | ||
| switch data { | ||
| case "": | ||
| body = nil | ||
| case "-": | ||
| body = os.Stdin | ||
| default: | ||
| body = strings.NewReader(data) | ||
| } | ||
|
|
||
| u, err := url.Parse(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| params := u.Query() | ||
| for k, v := range values { | ||
| params.Set(k, v) | ||
| } | ||
| u.RawQuery = params.Encode() | ||
|
|
||
| req, err := s.Client().NewRequest(s, method, u.String(), body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var respBody json.RawMessage | ||
| _, err = s.Client().Do(req, &respBody) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cmd.Print(string(respBody)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we do pretty print with indents, or have it as an option?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could pipe the output to |
||
| return nil | ||
| }, | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is relevant for the first iteration, but what about setting
Content-Typeand defaulting to json?edit: we already set this in hcloud-go, so we would not need to default here.