MCPcopy Create free account
hub / github.com/cli/cli / processResponse

Function processResponse

pkg/cmd/api/api.go:477–573  ·  view source on GitHub ↗
(resp *http.Response, opts *ApiOptions, bodyWriter, headersWriter io.Writer, template *template.Template, isFirstPage, isLastPage bool)

Source from the content-addressed store, hash-verified

475var jsonContentTypeRE = regexp.MustCompile(`[/+]json(;|$)`)
476
477func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersWriter io.Writer, template *template.Template, isFirstPage, isLastPage bool) (endCursor string, err error) {
478 if opts.ShowResponseHeaders {
479 fmt.Fprintln(headersWriter, resp.Proto, resp.Status)
480 printHeaders(headersWriter, resp.Header, opts.IO.ColorEnabled())
481 fmt.Fprint(headersWriter, "\r\n")
482 }
483
484 if resp.StatusCode == 204 {
485 return
486 }
487 var responseBody io.Reader = resp.Body
488 defer resp.Body.Close()
489
490 isJSON := jsonContentTypeRE.MatchString(resp.Header.Get("Content-Type"))
491
492 var serverError string
493 if isJSON && (opts.RequestPath == "graphql" || resp.StatusCode >= 400) {
494 if !strings.EqualFold(opts.RequestMethod, "HEAD") {
495 responseBody, serverError, err = parseErrorResponse(responseBody, resp.StatusCode)
496 if err != nil {
497 return
498 }
499 }
500 }
501
502 var bodyCopy *bytes.Buffer
503 isGraphQLPaginate := isJSON && resp.StatusCode == 200 && opts.Paginate && opts.RequestPath == "graphql"
504 if isGraphQLPaginate {
505 bodyCopy = &bytes.Buffer{}
506 responseBody = io.TeeReader(responseBody, bodyCopy)
507 }
508
509 if opts.FilterOutput != "" && serverError == "" {
510 // TODO: reuse parsed query across pagination invocations
511 indent := ""
512 if opts.IO.IsStdoutTTY() {
513 indent = ttyIndent
514 }
515 err = jq.EvaluateFormatted(responseBody, bodyWriter, opts.FilterOutput, indent, opts.IO.ColorEnabled())
516 if err != nil {
517 return
518 }
519 } else if opts.Template != "" && serverError == "" {
520 err = template.Execute(responseBody)
521 if err != nil {
522 return
523 }
524 } else if isJSON && opts.IO.ColorEnabled() {
525 err = jsoncolor.Write(bodyWriter, responseBody, ttyIndent)
526 } else {
527 if isJSON && opts.Paginate && !opts.Slurp && !isGraphQLPaginate && !opts.ShowResponseHeaders {
528 responseBody = &paginatedArrayReader{
529 Reader: responseBody,
530 isFirstPage: isFirstPage,
531 isLastPage: isLastPage,
532 }
533 }
534 // A raw non-JSON body is the only response the transport does not sanitize.

Callers 2

apiRunFunction · 0.85

Calls 13

WriteFunction · 0.92
CopyGuardedContentFunction · 0.92
ScopesSuggestionFunction · 0.92
SSOURLFunction · 0.92
printHeadersFunction · 0.85
parseErrorResponseFunction · 0.85
findEndCursorFunction · 0.85
ColorEnabledMethod · 0.80
IsStdoutTTYMethod · 0.80
CloseMethod · 0.65
GetMethod · 0.65
ErrorfMethod · 0.65

Tested by 1