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

Function processResponse

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

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