@@ -17,6 +17,7 @@ package api
17
17
import (
18
18
"bytes"
19
19
"context"
20
+ "errors"
20
21
"net"
21
22
"net/http"
22
23
"net/url"
@@ -40,6 +41,10 @@ type Config struct {
40
41
// The address of the Prometheus to connect to.
41
42
Address string
42
43
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
+
43
48
// RoundTripper is used by the Client to drive HTTP requests. If not
44
49
// provided, DefaultRoundTripper will be used.
45
50
RoundTripper http.RoundTripper
@@ -52,6 +57,22 @@ func (cfg *Config) roundTripper() http.RoundTripper {
52
57
return cfg .RoundTripper
53
58
}
54
59
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
+
55
76
// Client is the interface for an API client.
56
77
type Client interface {
57
78
URL (ep string , args map [string ]string ) * url.URL
@@ -68,9 +89,13 @@ func NewClient(cfg Config) (Client, error) {
68
89
}
69
90
u .Path = strings .TrimRight (u .Path , "/" )
70
91
92
+ if err := cfg .validate (); err != nil {
93
+ return nil , err
94
+ }
95
+
71
96
return & httpClient {
72
97
endpoint : u ,
73
- client : http. Client { Transport : cfg .roundTripper ()} ,
98
+ client : cfg .client () ,
74
99
}, nil
75
100
}
76
101
0 commit comments