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

Function apiRun

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

Source from the content-addressed store, hash-verified

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

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