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

Function NewCmdApi

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

Source from the content-addressed store, hash-verified

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

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