Add api command

This commit is contained in:
Mislav Marohnić 2020-05-12 18:50:08 +02:00
parent 45dec1b3e0
commit 1609afe993
2 changed files with 210 additions and 0 deletions

View file

@ -207,6 +207,45 @@ func (c Client) REST(method string, p string, body io.Reader, data interface{})
return nil
}
// DirectRequest is a low-level interface to making generic API requests
func (c Client) DirectRequest(method string, p string, params interface{}, headers []string) (*http.Response, error) {
url := "https://api.github.com/" + p
var body io.Reader
var bodyIsJSON bool
switch pp := params.(type) {
case map[string]interface{}:
b, err := json.Marshal(pp)
if err != nil {
return nil, fmt.Errorf("error serializing parameters: %w", err)
}
body = bytes.NewBuffer(b)
bodyIsJSON = true
case io.Reader:
body = pp
default:
return nil, fmt.Errorf("unrecognized parameters type: %v", params)
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if bodyIsJSON {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
}
for _, h := range headers {
idx := strings.IndexRune(h, ':')
if idx == -1 {
return nil, fmt.Errorf("header %q requires a value separated by ':'", h)
}
req.Header.Set(h[0:idx], strings.TrimSpace(h[idx+1:]))
}
return c.http.Do(req)
}
func handleResponse(resp *http.Response, data interface{}) error {
success := resp.StatusCode >= 200 && resp.StatusCode < 300

171
command/api.go Normal file
View file

@ -0,0 +1,171 @@
package command
import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/cli/cli/api"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(makeApiCommand())
}
type ApiOptions struct {
RequestMethod string
RequestMethodPassed bool
RequestPath string
MagicFields []string
RawFields []string
RequestHeaders []string
ShowResponseHeaders bool
}
func makeApiCommand() *cobra.Command {
opts := ApiOptions{}
cmd := &cobra.Command{
Use: "api <endpoint>",
Short: "Make an authenticated GitHub API request",
Long: `Makes an authenticated HTTP request to the GitHub API and prints the response.
The <endpoint> argument should either be a path of a GitHub API v3 endpoint, or
"graphql" to access the GitHub API v4.
The default HTTP request method is "GET" normally and "POST" if any parameters
were added. Override the method with '--method'.
Pass one or more '--raw-field' values in "<key>=<value>" format to add
JSON-encoded string parameters to the POST body.
The '--field' flag behaves like '--raw-field' with magic type conversion based
on the format of the value:
- literal values "true", "false", "null", and integer numbers get converted to
appropriate JSON types;
- if the value starts with "@", the rest of the value is interpreted as a
filename to read the value from. Pass "-" to read from standard input.
`,
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
opts.RequestPath = args[0]
opts.RequestMethodPassed = c.Flags().Changed("method")
ctx := contextForCommand(c)
client, err := apiClientForContext(ctx)
if err != nil {
return err
}
return apiRun(&opts, client)
},
}
cmd.Flags().StringVarP(&opts.RequestMethod, "method", "X", "GET", "The HTTP method for the request")
cmd.Flags().StringArrayVarP(&opts.MagicFields, "field", "F", nil, "Add a parameter of inferred type")
cmd.Flags().StringArrayVarP(&opts.RawFields, "raw-field", "f", nil, "Add a string parameter")
cmd.Flags().StringArrayVarP(&opts.RequestHeaders, "header", "H", nil, "Add an additional HTTP request header")
cmd.Flags().BoolVarP(&opts.ShowResponseHeaders, "include", "i", false, "Include HTTP response headers in the output")
return cmd
}
func apiRun(opts *ApiOptions, client *api.Client) error {
params := make(map[string]interface{})
for _, f := range opts.RawFields {
key, value, err := parseField(f)
if err != nil {
return err
}
params[key] = value
}
for _, f := range opts.MagicFields {
key, strValue, err := parseField(f)
if err != nil {
return err
}
value, err := magicFieldValue(strValue)
if err != nil {
return fmt.Errorf("error parsing %q value: %w", key, err)
}
params[key] = value
}
method := opts.RequestMethod
if len(params) > 0 && !opts.RequestMethodPassed {
method = "POST"
}
resp, err := client.DirectRequest(method, opts.RequestPath, params, opts.RequestHeaders)
if err != nil {
return err
}
if opts.ShowResponseHeaders {
for name, vals := range resp.Header {
fmt.Printf("%s: %s\r\n", name, strings.Join(vals, ", "))
}
fmt.Print("\r\n")
}
if resp.StatusCode == 204 {
return nil
}
defer resp.Body.Close()
// TODO: make stdout configurable for tests
_, err = io.Copy(os.Stdout, resp.Body)
if err != nil {
return err
}
return nil
}
func parseField(f string) (string, string, error) {
idx := strings.IndexRune(f, '=')
if idx == -1 {
return f, "", fmt.Errorf("field %q requires a value separated by an '=' sign", f)
}
return f[0:idx], f[idx+1:], nil
}
func magicFieldValue(v string) (interface{}, error) {
if strings.HasPrefix(v, "@") {
return readUserFile(v[1:])
}
if n, err := strconv.Atoi(v); err != nil {
return n, nil
}
switch v {
case "true":
return true, nil
case "false":
return false, nil
case "null":
return nil, nil
default:
return v, nil
}
}
func readUserFile(fn string) ([]byte, error) {
var r io.ReadCloser
if fn == "-" {
// TODO: make stdin configurable for tests
r = os.Stdin
} else {
var err error
r, err = os.Open(fn)
if err != nil {
return nil, err
}
defer r.Close()
}
return ioutil.ReadAll(r)
}