From e921f36a1a0c5d5752aa45b2f32ee58aea5cefdf Mon Sep 17 00:00:00 2001 From: Santhosh Nagaraj S Date: Mon, 11 Apr 2022 13:49:28 +0530 Subject: [PATCH] client: Allow configuration of http client Signed-off-by: yolossn --- api/client.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/api/client.go b/api/client.go index 1413f65fe..9cf462f63 100644 --- a/api/client.go +++ b/api/client.go @@ -40,6 +40,8 @@ type Config struct { // The address of the Prometheus to connect to. Address string + Client *http.Client + // RoundTripper is used by the Client to drive HTTP requests. If not // provided, DefaultRoundTripper will be used. RoundTripper http.RoundTripper @@ -52,6 +54,16 @@ func (cfg *Config) roundTripper() http.RoundTripper { return cfg.RoundTripper } +func (cfg *Config) client() *http.Client { + if cfg.Client == nil { + // Set the roundtripper for old code + return &http.Client{ + Transport: cfg.roundTripper(), + } + } + return cfg.Client +} + // Client is the interface for an API client. type Client interface { URL(ep string, args map[string]string) *url.URL @@ -70,7 +82,7 @@ func NewClient(cfg Config) (Client, error) { return &httpClient{ endpoint: u, - client: http.Client{Transport: cfg.roundTripper()}, + client: *cfg.client(), }, nil }