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

Function NewCmdApi

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

Source from the content-addressed store, hash-verified

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

Callers 2

Test_NewCmdApiFunction · 0.85

Calls 6

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

Tested by 2

Test_NewCmdApiFunction · 0.68