Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
17 changes: 12 additions & 5 deletions pkg/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ApiOptions struct {
RequestHeaders []string
ShowResponseHeaders bool
Paginate bool
Silent bool

HttpClient func() (*http.Client, error)
BaseRepo func() (ghrepo.Interface, error)
Expand Down Expand Up @@ -134,6 +135,7 @@ original query accepts an '$endCursor: String' variable and that it fetches the
cmd.Flags().BoolVarP(&opts.ShowResponseHeaders, "include", "i", false, "Include HTTP response headers in the output")
cmd.Flags().BoolVar(&opts.Paginate, "paginate", false, "Make additional HTTP requests to fetch all pages of results")
cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The file to use as body for the HTTP request")
cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Do not print the response body")
return cmd
}

Expand Down Expand Up @@ -178,14 +180,19 @@ func apiRun(opts *ApiOptions) error {
return err
}

headersOutputStream := opts.IO.Out
if opts.Silent {
opts.IO.Out = ioutil.Discard
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a neat trick! But, since parsing response JSON will take some processing time and will ultimately be discarded, could you instead skip the body processing logic in "silent" mode and exit from the command early?

Copy link
Contributor

@mislav mislav Jun 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, I just realized that the response body needs to be first consumed in order for our response error processing logic to work. So, maybe discard my above comment please, as following my suggestion might lead to too many code changes. I'll leave it to you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this Discard option is better than some complex logic. I agree with you on the error handling part. Even if I put the custom logic and all, it will become too bloated, and will cause problems down the line also, in case we need to add/change some feature/working.

}

hasNextPage := true
for hasNextPage {
resp, err := httpRequest(httpClient, method, requestPath, requestBody, requestHeaders)
if err != nil {
return err
}

endCursor, err := processResponse(resp, opts)
endCursor, err := processResponse(resp, opts, headersOutputStream)
if err != nil {
return err
}
Expand All @@ -211,11 +218,11 @@ func apiRun(opts *ApiOptions) error {
return nil
}

func processResponse(resp *http.Response, opts *ApiOptions) (endCursor string, err error) {
func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream io.Writer) (endCursor string, err error) {
if opts.ShowResponseHeaders {
fmt.Fprintln(opts.IO.Out, resp.Proto, resp.Status)
printHeaders(opts.IO.Out, resp.Header, opts.IO.ColorEnabled())
fmt.Fprint(opts.IO.Out, "\r\n")
fmt.Fprintln(headersOutputStream, resp.Proto, resp.Status)
printHeaders(headersOutputStream, resp.Header, opts.IO.ColorEnabled())
fmt.Fprint(headersOutputStream, "\r\n")
}

if resp.StatusCode == 204 {
Expand Down
54 changes: 54 additions & 0 deletions pkg/cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Test_NewCmdApi(t *testing.T) {
RequestHeaders: []string(nil),
ShowResponseHeaders: false,
Paginate: false,
Silent: false,
},
wantsErr: false,
},
Expand All @@ -55,6 +56,7 @@ func Test_NewCmdApi(t *testing.T) {
RequestHeaders: []string(nil),
ShowResponseHeaders: false,
Paginate: false,
Silent: false,
},
wantsErr: false,
},
Expand All @@ -71,6 +73,7 @@ func Test_NewCmdApi(t *testing.T) {
RequestHeaders: []string(nil),
ShowResponseHeaders: false,
Paginate: false,
Silent: false,
},
wantsErr: false,
},
Expand All @@ -87,6 +90,7 @@ func Test_NewCmdApi(t *testing.T) {
RequestHeaders: []string{"accept: text/plain"},
ShowResponseHeaders: true,
Paginate: false,
Silent: false,
},
wantsErr: false,
},
Expand All @@ -103,6 +107,24 @@ func Test_NewCmdApi(t *testing.T) {
RequestHeaders: []string(nil),
ShowResponseHeaders: false,
Paginate: true,
Silent: false,
},
wantsErr: false,
},
{
name: "with silenced output",
cli: "repos/OWNER/REPO/issues --silent",
wants: ApiOptions{
RequestMethod: "GET",
RequestMethodPassed: false,
RequestPath: "repos/OWNER/REPO/issues",
RequestInputFile: "",
RawFields: []string(nil),
MagicFields: []string(nil),
RequestHeaders: []string(nil),
ShowResponseHeaders: false,
Paginate: false,
Silent: true,
},
wantsErr: false,
},
Expand All @@ -124,6 +146,7 @@ func Test_NewCmdApi(t *testing.T) {
RequestHeaders: []string(nil),
ShowResponseHeaders: false,
Paginate: true,
Silent: false,
},
wantsErr: false,
},
Expand All @@ -145,6 +168,7 @@ func Test_NewCmdApi(t *testing.T) {
RequestHeaders: []string(nil),
ShowResponseHeaders: false,
Paginate: false,
Silent: false,
},
wantsErr: false,
},
Expand Down Expand Up @@ -264,6 +288,36 @@ func Test_apiRun(t *testing.T) {
stdout: `gateway timeout`,
stderr: "gh: HTTP 502\n",
},
{
name: "silent",
options: ApiOptions{
Silent: true,
},
httpResponse: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`body`)),
},
err: nil,
stdout: ``,
stderr: ``,
},
{
name: "show response headers even when silent",
options: ApiOptions{
ShowResponseHeaders: true,
Silent: true,
},
httpResponse: &http.Response{
Proto: "HTTP/1.1",
Status: "200 Okey-dokey",
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`body`)),
Header: http.Header{"Content-Type": []string{"text/plain"}},
},
err: nil,
stdout: "HTTP/1.1 200 Okey-dokey\nContent-Type: text/plain\r\n\r\n",
stderr: ``,
},
}

for _, tt := range tests {
Expand Down