Skip to content

Commit

Permalink
change client to interface, allow override by other implementations (…
Browse files Browse the repository at this point in the history
…e.g. github.com/hashicorp/go-retryablehttp)

Signed-off-by: Roy Reshef <[email protected]>
  • Loading branch information
rreshef-densify committed Nov 22, 2023
1 parent 80d3f0b commit 24d59e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 24d59e9

Please sign in to comment.