Skip to content

Commit

Permalink
Push client options down into GetHttpClient
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <[email protected]>
  • Loading branch information
dereknola committed Feb 7, 2024
1 parent 10e69dc commit 112dbd5
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pkg/clientaccess/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func parseToken(token string) (*Info, error) {
// If the CA bundle is not empty but does not contain any valid certs, it validates using
// an empty CA bundle (which will always fail).
// If valid cert+key paths can be loaded from the provided paths, they are used for client cert auth.
func GetHTTPClient(cacerts []byte, certFile, keyFile string) *http.Client {
func GetHTTPClient(cacerts []byte, certFile, keyFile string, option ...ClientOption) *http.Client {
if len(cacerts) == 0 {
return defaultClient
}
Expand All @@ -253,13 +253,18 @@ func GetHTTPClient(cacerts []byte, certFile, keyFile string) *http.Client {
if err == nil {
tlsConfig.Certificates = []tls.Certificate{cert}
}
return &http.Client{
client := &http.Client{
Timeout: defaultClientTimeout,
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}

for _, o := range option {
o(client)
}
return client
}

func WithTimeout(d time.Duration) ClientOption {
Expand All @@ -275,7 +280,7 @@ func WithHeaderTimeout(d time.Duration) ClientOption {
}

// Get makes a request to a subpath of info's BaseURL
func (i *Info) Get(path string) ([]byte, error) {
func (i *Info) Get(path string, option ...ClientOption) ([]byte, error) {
u, err := url.Parse(i.BaseURL)
if err != nil {
return nil, err
Expand All @@ -286,7 +291,7 @@ func (i *Info) Get(path string) ([]byte, error) {
}
p.Scheme = u.Scheme
p.Host = u.Host
return get(p.String(), GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile), i.Username, i.Password, i.Token())
return get(p.String(), GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile, option...), i.Username, i.Password, i.Token())
}

// Put makes a request to a subpath of info's BaseURL, with a 10 second timeout
Expand All @@ -303,7 +308,7 @@ func (i *Info) Put(path string, body []byte, option ...ClientOption) error {
p.Scheme = u.Scheme
p.Host = u.Host

client := GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile)
client := GetHTTPClient(i.CACerts, i.CertFile, i.KeyFile, option...)
for _, o := range option {
o(client)
}
Expand Down

0 comments on commit 112dbd5

Please sign in to comment.