Skip to content

Commit 9e28363

Browse files
authored
Merge pull request #898 from alliasgher/fix/NewOAuth2RoundTripper-exported-options
config: change NewOAuth2RoundTripper to accept variadic HTTPClientOption
2 parents 9a26ab2 + 5fa8f6b commit 9e28363

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

config/http_config.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon
717717
return nil, fmt.Errorf("unable to use client secret: %w", err)
718718
}
719719
}
720-
rt = NewOAuth2RoundTripper(oauthCredential, cfg.OAuth2, rt, &opts)
720+
rt = NewOAuth2RoundTripper(oauthCredential, cfg.OAuth2, rt, optFuncs...)
721721
}
722722

723723
if cfg.HTTPHeaders != nil {
@@ -942,16 +942,26 @@ type oauth2RoundTripper struct {
942942
client *http.Client
943943
}
944944

945-
func NewOAuth2RoundTripper(oauthCredential SecretReader, config *OAuth2, next http.RoundTripper, opts *httpClientOptions) http.RoundTripper {
945+
// NewOAuth2RoundTripper returns a round tripper that performs OAuth2
946+
// authentication. The opts variadic parameter accepts any HTTPClientOption
947+
// (e.g. WithDialContextFunc, WithKeepAlivesDisabled) so that callers outside
948+
// this package can fully configure the transport without needing access to the
949+
// unexported *httpClientOptions type.
950+
func NewOAuth2RoundTripper(oauthCredential SecretReader, config *OAuth2, next http.RoundTripper, optFuncs ...HTTPClientOption) http.RoundTripper {
946951
if oauthCredential == nil {
947952
oauthCredential = NewInlineSecret("")
948953
}
949954

955+
opts := defaultHTTPClientOptions
956+
for _, opt := range optFuncs {
957+
opt.applyToHTTPClientOptions(&opts)
958+
}
959+
950960
return &oauth2RoundTripper{
951961
config: config,
952962
// A correct tokenSource will be added later on.
953963
lastRT: &oauth2.Transport{Base: next},
954-
opts: opts,
964+
opts: &opts,
955965
oauthCredential: oauthCredential,
956966
}
957967
}

config/http_config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ endpoint_params:
15191519
require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig)
15201520

15211521
secret := NewInlineSecret(string(expectedConfig.ClientSecret))
1522-
rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions)
1522+
rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport)
15231523

15241524
client := http.Client{
15251525
Transport: rt,
@@ -1654,7 +1654,7 @@ endpoint_params:
16541654
require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig)
16551655

16561656
secret := NewFileSecret(expectedConfig.ClientSecretFile)
1657-
rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions)
1657+
rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport)
16581658

16591659
client := http.Client{
16601660
Transport: rt,
@@ -1768,7 +1768,7 @@ endpoint_params:
17681768
require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig)
17691769

17701770
clientCertificateKey := NewFileSecret(expectedConfig.ClientCertificateKeyFile)
1771-
rt := NewOAuth2RoundTripper(clientCertificateKey, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions)
1771+
rt := NewOAuth2RoundTripper(clientCertificateKey, &expectedConfig, http.DefaultTransport)
17721772

17731773
client := http.Client{
17741774
Transport: rt,

0 commit comments

Comments
 (0)