Skip to content

Commit

Permalink
client: Allow configuration of http client
Browse files Browse the repository at this point in the history
Signed-off-by: yolossn <nssvlr@gmail.com>
  • Loading branch information
yolossn committed Apr 18, 2022
1 parent 29e8191 commit b5f1e9e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion api/client.go
Expand Up @@ -40,6 +40,10 @@ 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 is created.
Client *http.Client

// RoundTripper is used by the Client to drive HTTP requests. If not
// provided, DefaultRoundTripper will be used.
RoundTripper http.RoundTripper
Expand All @@ -52,6 +56,15 @@ func (cfg *Config) roundTripper() http.RoundTripper {
return cfg.RoundTripper
}

func (cfg *Config) client() http.Client {
if cfg.Client == nil {
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
Expand All @@ -70,7 +83,7 @@ func NewClient(cfg Config) (Client, error) {

return &httpClient{
endpoint: u,
client: http.Client{Transport: cfg.roundTripper()},
client: cfg.client(),
}, nil
}

Expand Down

0 comments on commit b5f1e9e

Please sign in to comment.