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

Function apiRun

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

Source from the content-addressed store, hash-verified

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

Callers 14

NewCmdApiFunction · 0.85
Test_apiRunFunction · 0.85
Test_apiRun_DELETEFunction · 0.85
Test_apiRun_HEADFunction · 0.85
Test_apiRun_inputFileFunction · 0.85
Test_apiRun_cacheFunction · 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 13

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