mirror of
https://github.com/grafana/grafana.git
synced 2025-01-02 12:17:01 -06:00
348e76fc8e
Uses new httpclient package from grafana-plugin-sdk-go introduced via grafana/grafana-plugin-sdk-go#328. Replaces the GetHTTPClient, GetTransport, GetTLSConfig methods defined on DataSource model. Longer-term the goal is to migrate core HTTP backend data sources to use the SDK contracts and using httpclient.Provider for creating HTTP clients and such. Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
28 lines
902 B
Go
28 lines
902 B
Go
package httpclientprovider
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
|
)
|
|
|
|
// SetUserAgentMiddlewareName is the middleware name used by SetUserAgentMiddleware.
|
|
const SetUserAgentMiddlewareName = "user-agent"
|
|
|
|
// SetUserAgentMiddleware is middleware that sets the HTTP header User-Agent on the outgoing request.
|
|
// If User-Agent already set, it will not be overridden by this middleware.
|
|
func SetUserAgentMiddleware(userAgent string) httpclient.Middleware {
|
|
return httpclient.NamedMiddlewareFunc(SetUserAgentMiddlewareName, func(opts httpclient.Options, next http.RoundTripper) http.RoundTripper {
|
|
if userAgent == "" {
|
|
return next
|
|
}
|
|
|
|
return httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
if req.Header.Get("User-Agent") == "" {
|
|
req.Header.Set("User-Agent", userAgent)
|
|
}
|
|
return next.RoundTrip(req)
|
|
})
|
|
})
|
|
}
|