HTTPClient returns a new *http.Client with a default timeout and a configured transport.
(ctx context.Context, opts ...Option)
| 81 | |
| 82 | // HTTPClient returns a new *http.Client with a default timeout and a configured transport. |
| 83 | func (p *provider) HTTPClient(ctx context.Context, opts ...Option) (*http.Client, error) { |
| 84 | options := &httpClientOptions{} |
| 85 | for _, opt := range opts { |
| 86 | opt(options) |
| 87 | } |
| 88 | |
| 89 | if options.tlsConfig == nil { |
| 90 | tlsConfig, err := p.tlsConfigProvider.GetTLSClientConfig(ctx, options.tlsConfigOptions...) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | options.tlsConfig = tlsConfig |
| 95 | } |
| 96 | |
| 97 | transport := http.DefaultTransport.(*http.Transport).Clone() |
| 98 | transport.TLSClientConfig = options.tlsConfig |
| 99 | |
| 100 | var rt http.RoundTripper = transport |
| 101 | rt = gzhttp.Transport(rt) |
| 102 | rt = otelhttp.NewTransport( |
| 103 | rt, |
| 104 | otelhttp.WithTracerProvider(tracing.FromContext(ctx)), |
| 105 | // As pkg/metrics is currently Prometheus based, the OpenTelemetry metrics are unused. |
| 106 | // TODO: https://github.com/TheThingsNetwork/lorawan-stack/issues/5692. |
| 107 | otelhttp.WithMeterProvider(noop.MeterProvider{}), |
| 108 | ) |
| 109 | if options.cache { |
| 110 | rt = &httpcache.Transport{ |
| 111 | Transport: rt, |
| 112 | Cache: httpcache.NewMemoryCache(), |
| 113 | MarkCachedResponses: true, |
| 114 | } |
| 115 | } |
| 116 | rt = &roundTripperWithUserAgent{ |
| 117 | RoundTripper: rt, |
| 118 | UserAgent: fmt.Sprintf("TheThingsStack/%s (%s/%s)", version.TTN, runtime.GOOS, runtime.GOARCH), |
| 119 | } |
| 120 | |
| 121 | return &http.Client{ |
| 122 | Timeout: defaultHTTPClientTimeout, |
| 123 | Transport: rt, |
| 124 | }, nil |
| 125 | } |
| 126 | |
| 127 | // NewProvider constructs a Provider on top of the provided TLS configuration provider. |
| 128 | func NewProvider(tlsConfigProvider TLSClientConfigurationProvider) Provider { |
nothing calls this directly
no test coverage detected