From 24d59e9b9bec2e3e578c216bdc41ff8d6bf46e88 Mon Sep 17 00:00:00 2001 From: Roy Reshef Date: Wed, 22 Nov 2023 15:44:29 -0500 Subject: [PATCH] change client to interface, allow override by other implementations (e.g. github.com/hashicorp/go-retryablehttp) Signed-off-by: Roy Reshef --- api/client.go | 16 ++++++++++------ api/client_test.go | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/api/client.go b/api/client.go index 72a01309c..657705882 100644 --- a/api/client.go +++ b/api/client.go @@ -36,14 +36,18 @@ var DefaultRoundTripper http.RoundTripper = &http.Transport{ TLSHandshakeTimeout: 10 * time.Second, } +type HttpClient interface { + Do(req *http.Request) (*http.Response, error) +} + // Config defines configuration parameters for a new client. type Config struct { // The address of the Prometheus to connect to. Address string // Client is used by the Client to drive HTTP requests. If not provided, - // a new one based on the provided RoundTripper (or DefaultRoundTripper) will be used. - Client *http.Client + // a new http.Client based on the provided RoundTripper (or DefaultRoundTripper) will be used. + Client HttpClient // RoundTripper is used by the Client to drive HTTP requests. If not // provided, DefaultRoundTripper will be used. @@ -57,13 +61,13 @@ func (cfg *Config) roundTripper() http.RoundTripper { return cfg.RoundTripper } -func (cfg *Config) client() http.Client { +func (cfg *Config) client() HttpClient { if cfg.Client == nil { - return http.Client{ + return &http.Client{ Transport: cfg.roundTripper(), } } - return *cfg.Client + return cfg.Client } func (cfg *Config) validate() error { @@ -101,7 +105,7 @@ func NewClient(cfg Config) (Client, error) { type httpClient struct { endpoint *url.URL - client http.Client + client HttpClient } func (c *httpClient) URL(ep string, args map[string]string) *url.URL { diff --git a/api/client_test.go b/api/client_test.go index 874387868..ceb704708 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -105,7 +105,7 @@ func TestClientURL(t *testing.T) { hclient := &httpClient{ endpoint: ep, - client: http.Client{Transport: DefaultRoundTripper}, + client: &http.Client{Transport: DefaultRoundTripper}, } u := hclient.URL(test.endpoint, test.args)