MCPcopy
hub / github.com/cli/cli / apiRun

Function apiRun

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

Source from the content-addressed store, hash-verified

302}
303
304func apiRun(opts *ApiOptions) error {
305 params, err := parseFields(opts)
306 if err != nil {
307 return err
308 }
309
310 isGraphQL := opts.RequestPath == "graphql"
311 requestPath, err := fillPlaceholders(opts.RequestPath, opts)
312 if err != nil {
313 return fmt.Errorf("unable to expand placeholder in path: %w", err)
314 }
315 method := opts.RequestMethod
316 requestHeaders := opts.RequestHeaders
317 var requestBody interface{}
318 if len(params) > 0 {
319 requestBody = params
320 }
321
322 if !opts.RequestMethodPassed && (len(params) > 0 || opts.RequestInputFile != "") {
323 method = "POST"
324 }
325
326 if !opts.Silent {
327 if err := opts.IO.StartPager(); err == nil {
328 defer opts.IO.StopPager()
329 } else {
330 fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
331 }
332 }
333
334 var bodyWriter io.Writer = opts.IO.Out
335 var headersWriter io.Writer = opts.IO.Out
336 if opts.Silent {
337 bodyWriter = io.Discard
338 }
339 if opts.Verbose {
340 // httpClient handles output when verbose flag is specified.
341 bodyWriter = io.Discard
342 headersWriter = io.Discard
343 }
344
345 if opts.Paginate && !isGraphQL {
346 requestPath = addPerPage(requestPath, 100, params)
347 }
348
349 // Similar to `jq --slurp`, write all pages JSON arrays or objects into a JSON array.
350 if opts.Paginate && opts.Slurp {
351 w := &jsonArrayWriter{
352 Writer: bodyWriter,
353 color: opts.IO.ColorEnabled(),
354 }
355 defer w.Close()
356
357 bodyWriter = w
358 }
359
360 if opts.RequestInputFile != "" {
361 file, size, err := openUserFile(opts.RequestInputFile, opts.IO.In)

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