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

Function NewCmdApi

pkg/cmd/api/api.go:68–309  ·  view source on GitHub ↗
(f *cmdutil.Factory, runF func(*ApiOptions) error)

Source from the content-addressed store, hash-verified

66}
67
68func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command {
69 opts := ApiOptions{
70 AppVersion: f.AppVersion,
71 InvokingAgent: f.InvokingAgent,
72 BaseRepo: f.BaseRepo,
73 Branch: f.Branch,
74 Config: f.Config,
75 HttpClient: api.NewHTTPClient,
76 IO: f.IOStreams,
77 TelemetryDisabler: f.TelemetryDisabler,
78 }
79
80 cmd := &cobra.Command{
81 Use: "api <endpoint>",
82 Short: "Make an authenticated GitHub API request",
83 Long: heredoc.Docf(`
84 Makes an authenticated HTTP request to the GitHub API and prints the response.
85
86 The endpoint argument should either be a path of a GitHub API v3 endpoint, or
87 %[1]sgraphql%[1]s to access the GitHub API v4.
88
89 Placeholder values %[1]s{owner}%[1]s, %[1]s{repo}%[1]s, and %[1]s{branch}%[1]s in the endpoint
90 argument will get replaced with values from the repository of the current
91 directory or the repository specified in the %[1]sGH_REPO%[1]s environment variable.
92 Note that in some shells, for example PowerShell, you may need to enclose
93 any value that contains %[1]s{...}%[1]s in quotes to prevent the shell from
94 applying special meaning to curly braces.
95
96 The %[1]s-p/--preview%[1]s flag enables opting into previews, which are feature-flagged,
97 experimental API endpoints or behaviors. The API expects opt-in via the %[1]sAccept%[1]s
98 header with format %[1]sapplication/vnd.github.<preview-name>-preview+json%[1]s and this
99 command facilitates that via %[1]s--preview <preview-name>%[1]s. To send a request for
100 the corsair and scarlet witch previews, you could use %[1]s-p corsair,scarlet-witch%[1]s
101 or %[1]s--preview corsair --preview scarlet-witch%[1]s.
102
103 The default HTTP request method is %[1]sGET%[1]s normally and %[1]sPOST%[1]s if any parameters
104 were added. Override the method with %[1]s--method%[1]s.
105
106 Pass one or more %[1]s-f/--raw-field%[1]s values in %[1]skey=value%[1]s format to add static string
107 parameters to the request payload. To add non-string or placeholder-determined values, see
108 %[1]s-F/--field%[1]s below. Note that adding request parameters will automatically switch the
109 request method to %[1]sPOST%[1]s. To send the parameters as a %[1]sGET%[1]s query string instead, use
110 %[1]s--method GET%[1]s.
111
112 The %[1]s-F/--field%[1]s flag has magic type conversion based on the format of the value:
113
114 - literal values %[1]strue%[1]s, %[1]sfalse%[1]s, %[1]snull%[1]s, and integer numbers get converted to
115 appropriate JSON types;
116 - placeholder values %[1]s{owner}%[1]s, %[1]s{repo}%[1]s, and %[1]s{branch}%[1]s get populated with values
117 from the repository of the current directory;
118 - if the value starts with %[1]s@%[1]s, the rest of the value is interpreted as a
119 filename to read the value from. Pass %[1]s-%[1]s to read from standard input.
120
121 For GraphQL requests, all fields other than %[1]squery%[1]s and %[1]soperationName%[1]s are
122 interpreted as GraphQL variables.
123
124 To pass nested parameters in the request payload, use %[1]skey[subkey]=value%[1]s syntax when
125 declaring fields. To pass nested values as arrays, declare multiple fields with the

Callers 3

Test_NewCmdApiFunction · 0.85
TestNewCmdApiTelemetryFunction · 0.85

Calls 7

OverrideBaseRepoFuncFunction · 0.92
HostnameValidatorFunction · 0.92
FlagErrorfFunction · 0.92
MutuallyExclusiveFunction · 0.92
apiRunFunction · 0.85
ChangedMethod · 0.80
ErrorfMethod · 0.65

Tested by 3

Test_NewCmdApiFunction · 0.68
TestNewCmdApiTelemetryFunction · 0.68