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