@@ -17,6 +17,7 @@ package api
1717import (
1818 "bytes"
1919 "context"
20+ "errors"
2021 "net"
2122 "net/http"
2223 "net/url"
@@ -40,6 +41,10 @@ type Config struct {
4041 // The address of the Prometheus to connect to.
4142 Address string
4243
44+ // 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
47+
4348 // RoundTripper is used by the Client to drive HTTP requests. If not
4449 // provided, DefaultRoundTripper will be used.
4550 RoundTripper http.RoundTripper
@@ -52,6 +57,22 @@ func (cfg *Config) roundTripper() http.RoundTripper {
5257 return cfg .RoundTripper
5358}
5459
60+ func (cfg * Config ) client () http.Client {
61+ if cfg .Client == nil {
62+ return http.Client {
63+ Transport : cfg .roundTripper (),
64+ }
65+ }
66+ return * cfg .Client
67+ }
68+
69+ func (cfg * Config ) validate () error {
70+ if cfg .Client != nil && cfg .RoundTripper != nil {
71+ return errors .New ("api.Config.RoundTripper and api.Config.Client are mutually exclusive" )
72+ }
73+ return nil
74+ }
75+
5576// Client is the interface for an API client.
5677type Client interface {
5778 URL (ep string , args map [string ]string ) * url.URL
@@ -68,9 +89,13 @@ func NewClient(cfg Config) (Client, error) {
6889 }
6990 u .Path = strings .TrimRight (u .Path , "/" )
7091
92+ if err := cfg .validate (); err != nil {
93+ return nil , err
94+ }
95+
7196 return & httpClient {
7297 endpoint : u ,
73- client : http. Client { Transport : cfg .roundTripper ()} ,
98+ client : cfg .client () ,
7499 }, nil
75100}
76101
0 commit comments