NewExternalHTTPClient creates an HTTP client for talking to non-GitHub hosts. It includes debug logging and a User-Agent header but does not attach any authentication tokens or GitHub-specific headers.
(opts ExternalHTTPClientOptions)
| 98 | // It includes debug logging and a User-Agent header but does not attach any |
| 99 | // authentication tokens or GitHub-specific headers. |
| 100 | func NewExternalHTTPClient(opts ExternalHTTPClientOptions) (*http.Client, error) { |
| 101 | clientOpts := ghAPI.ClientOptions{ |
| 102 | Host: "none", |
| 103 | AuthToken: "none", |
| 104 | LogIgnoreEnv: true, |
| 105 | SkipDefaultHeaders: true, |
| 106 | Transport: opts.Transport, |
| 107 | } |
| 108 | |
| 109 | debugEnabled, debugValue := utils.IsDebugEnabled() |
| 110 | logVerboseHTTP := false |
| 111 | if strings.Contains(debugValue, "api") { |
| 112 | logVerboseHTTP = true |
| 113 | } |
| 114 | |
| 115 | if logVerboseHTTP || debugEnabled { |
| 116 | clientOpts.Log = opts.Log |
| 117 | clientOpts.LogColorize = opts.LogColorize |
| 118 | clientOpts.LogVerboseHTTP = logVerboseHTTP |
| 119 | } |
| 120 | |
| 121 | clientOpts.Headers = map[string]string{ |
| 122 | userAgent: fmt.Sprintf("GitHub CLI %s", opts.AppVersion), |
| 123 | } |
| 124 | |
| 125 | client, err := ghAPI.NewHTTPClient(clientOpts) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | |
| 130 | return client, nil |
| 131 | } |
| 132 | |
| 133 | func NewCachedHTTPClient(httpClient *http.Client, ttl time.Duration) *http.Client { |
| 134 | newClient := *httpClient |