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

Function NewHTTPClient

api/http_client.go:35–89  ·  view source on GitHub ↗
(opts HTTPClientOptions)

Source from the content-addressed store, hash-verified

33}
34
35func NewHTTPClient(opts HTTPClientOptions) (*http.Client, error) {
36 // Provide invalid host, and token values so gh.HTTPClient will not automatically resolve them.
37 // The real host and token are inserted at request time.
38 clientOpts := ghAPI.ClientOptions{
39 Host: "none",
40 AuthToken: "none",
41 LogIgnoreEnv: true,
42 SkipDefaultHeaders: opts.SkipDefaultHeaders,
43 }
44
45 debugEnabled, debugValue := utils.IsDebugEnabled()
46 if strings.Contains(debugValue, "api") {
47 opts.LogVerboseHTTP = true
48 }
49
50 if opts.LogVerboseHTTP || debugEnabled {
51 clientOpts.Log = opts.Log
52 clientOpts.LogColorize = opts.LogColorize
53 clientOpts.LogVerboseHTTP = opts.LogVerboseHTTP
54 }
55
56 ua := fmt.Sprintf("GitHub CLI %s", opts.AppVersion)
57 if opts.InvokingAgent != "" {
58 ua = fmt.Sprintf("%s Agent/%s", ua, opts.InvokingAgent)
59 }
60
61 headers := map[string]string{
62 userAgent: ua,
63 apiVersion: apiVersionValue,
64 }
65 clientOpts.Headers = headers
66
67 if opts.EnableCache {
68 clientOpts.EnableCache = opts.EnableCache
69 clientOpts.CacheTTL = opts.CacheTTL
70 }
71
72 client, err := ghAPI.NewHTTPClient(clientOpts)
73 if err != nil {
74 return nil, err
75 }
76
77 if opts.Config != nil {
78 client.Transport = AddAuthTokenHeader(client.Transport, opts.Config)
79 }
80
81 if opts.TelemetryDisabler != nil {
82 client.Transport = telemetryDisablerTransport{
83 wrappedTransport: client.Transport,
84 telemetryDisabler: opts.TelemetryDisabler,
85 }
86 }
87
88 return client, nil
89}
90
91// ExternalHTTPClientOptions holds options for creating an external HTTP client.
92type ExternalHTTPClientOptions struct {

Calls 3

IsDebugEnabledFunction · 0.92
AddAuthTokenHeaderFunction · 0.85
ContainsMethod · 0.80