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

Function apiRun

pkg/cmd/api/api.go:317–484  ·  view source on GitHub ↗
(opts *ApiOptions)

Source from the content-addressed store, hash-verified

315}
316
317func apiRun(opts *ApiOptions) error {
318 params, err := parseFields(opts)
319 if err != nil {
320 return err
321 }
322
323 isGraphQL := opts.RequestPath == "graphql"
324 requestPath, err := fillPlaceholders(opts.RequestPath, opts)
325 if err != nil {
326 return fmt.Errorf("unable to expand placeholder in path: %w", err)
327 }
328 endpoint := requestPath
329 method := opts.RequestMethod
330 requestHeaders := opts.RequestHeaders
331 var requestBody any
332 if len(params) > 0 {
333 requestBody = params
334 }
335
336 if !opts.RequestMethodPassed && (len(params) > 0 || opts.RequestInputFile != "") {
337 method = "POST"
338 }
339
340 if !opts.Silent {
341 if err := opts.IO.StartPager(); err == nil {
342 defer opts.IO.StopPager()
343 } else {
344 fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
345 }
346 }
347
348 // Response content funnels through ContentOut. It stays in passthrough here:
349 // JSON is sanitized by the transport and the jq/template/jsoncolor paths emit
350 // our own formatting, so only a raw non-JSON body needs neutralizing, done at
351 // its copy below.
352 opts.IO.SetContentSanitization(false)
353
354 var bodyWriter io.Writer = opts.IO.ContentOut
355 var headersWriter io.Writer = opts.IO.Out
356 if opts.Silent {
357 bodyWriter = io.Discard
358 }
359 if opts.Verbose {
360 // httpClient handles output when verbose flag is specified.
361 bodyWriter = io.Discard
362 headersWriter = io.Discard
363 }
364
365 if opts.Paginate && !isGraphQL {
366 requestPath = addPerPage(requestPath, 100, params)
367 }
368
369 // Similar to `jq --slurp`, write all pages JSON arrays or objects into a JSON array.
370 if opts.Paginate && opts.Slurp {
371 w := &jsonArrayWriter{
372 Writer: bodyWriter,
373 color: opts.IO.ColorEnabled(),
374 }

Callers 15

NewCmdApiFunction · 0.85
Test_apiRunFunction · 0.85
Test_apiRun_DELETEFunction · 0.85
Test_apiRun_HEADFunction · 0.85
Test_apiRun_inputFileFunction · 0.85

Calls 15

CloseMethod · 0.95
fillPlaceholdersFunction · 0.85
addPerPageFunction · 0.85
openUserFileFunction · 0.85
addQueryFunction · 0.85
previewNamesToMIMETypesFunction · 0.85
httpRequestFunction · 0.85
startPageFunction · 0.85
processResponseFunction · 0.85
StartPagerMethod · 0.80
StopPagerMethod · 0.80

Tested by 14

Test_apiRunFunction · 0.68
Test_apiRun_DELETEFunction · 0.68
Test_apiRun_HEADFunction · 0.68
Test_apiRun_inputFileFunction · 0.68
Test_apiRun_cacheFunction · 0.68