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

Function processResponse

pkg/cmd/api/api.go:484–580  ·  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

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