Skip to content

Commit 24d59e9

Browse files
change client to interface, allow override by other implementations (e.g. github.com/hashicorp/go-retryablehttp)
Signed-off-by: Roy Reshef <rreshef@densify.com>
1 parent 80d3f0b commit 24d59e9

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

api/client.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ var DefaultRoundTripper http.RoundTripper = &http.Transport{
3636
TLSHandshakeTimeout: 10 * time.Second,
3737
}
3838

39+
type HttpClient interface {
40+
Do(req *http.Request) (*http.Response, error)
41+
}
42+
3943
// Config defines configuration parameters for a new client.
4044
type Config struct {
4145
// The address of the Prometheus to connect to.
4246
Address string
4347

4448
// Client is used by the Client to drive HTTP requests. If not provided,
45-
// a new one based on the provided RoundTripper (or DefaultRoundTripper) will be used.
46-
Client *http.Client
49+
// a new http.Client based on the provided RoundTripper (or DefaultRoundTripper) will be used.
50+
Client HttpClient
4751

4852
// RoundTripper is used by the Client to drive HTTP requests. If not
4953
// provided, DefaultRoundTripper will be used.
@@ -57,13 +61,13 @@ func (cfg *Config) roundTripper() http.RoundTripper {
5761
return cfg.RoundTripper
5862
}
5963

60-
func (cfg *Config) client() http.Client {
64+
func (cfg *Config) client() HttpClient {
6165
if cfg.Client == nil {
62-
return http.Client{
66+
return &http.Client{
6367
Transport: cfg.roundTripper(),
6468
}
6569
}
66-
return *cfg.Client
70+
return cfg.Client
6771
}
6872

6973
func (cfg *Config) validate() error {
@@ -101,7 +105,7 @@ func NewClient(cfg Config) (Client, error) {
101105

102106
type httpClient struct {
103107
endpoint *url.URL
104-
client http.Client
108+
client HttpClient
105109
}
106110

107111
func (c *httpClient) URL(ep string, args map[string]string) *url.URL {

api/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestClientURL(t *testing.T) {
105105

106106
hclient := &httpClient{
107107
endpoint: ep,
108-
client: http.Client{Transport: DefaultRoundTripper},
108+
client: &http.Client{Transport: DefaultRoundTripper},
109109
}
110110

111111
u := hclient.URL(test.endpoint, test.args)

0 commit comments

Comments
 (0)