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

Function apiRun

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

Source from the content-addressed store, hash-verified

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