@@ -372,6 +372,7 @@ type httpClientOptions struct {
372
372
keepAlivesEnabled bool
373
373
http2Enabled bool
374
374
idleConnTimeout time.Duration
375
+ userAgent string
375
376
}
376
377
377
378
// HTTPClientOption defines an option that can be applied to the HTTP client.
@@ -405,6 +406,13 @@ func WithIdleConnTimeout(timeout time.Duration) HTTPClientOption {
405
406
}
406
407
}
407
408
409
+ // WithUserAgent allows setting the user agent.
410
+ func WithUserAgent (ua string ) HTTPClientOption {
411
+ return func (opts * httpClientOptions ) {
412
+ opts .userAgent = ua
413
+ }
414
+ }
415
+
408
416
// NewClient returns a http.Client using the specified http.RoundTripper.
409
417
func newClient (rt http.RoundTripper ) * http.Client {
410
418
return & http.Client {Transport : rt }
@@ -497,8 +505,12 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
497
505
rt = NewBasicAuthRoundTripper (cfg .BasicAuth .Username , cfg .BasicAuth .Password , cfg .BasicAuth .PasswordFile , rt )
498
506
}
499
507
508
+ if opts .userAgent != "" {
509
+ rt = NewUserAgentRoundTripper (opts .userAgent , rt )
510
+ }
511
+
500
512
if cfg .OAuth2 != nil {
501
- rt = NewOAuth2RoundTripper (cfg .OAuth2 , rt )
513
+ rt = NewOAuth2RoundTripper (cfg .OAuth2 , rt , & opts )
502
514
}
503
515
// Return a new configured RoundTripper.
504
516
return rt , nil
@@ -619,12 +631,14 @@ type oauth2RoundTripper struct {
619
631
next http.RoundTripper
620
632
secret string
621
633
mtx sync.RWMutex
634
+ opts * httpClientOptions
622
635
}
623
636
624
- func NewOAuth2RoundTripper (config * OAuth2 , next http.RoundTripper ) http.RoundTripper {
637
+ func NewOAuth2RoundTripper (config * OAuth2 , next http.RoundTripper , opts * httpClientOptions ) http.RoundTripper {
625
638
return & oauth2RoundTripper {
626
639
config : config ,
627
640
next : next ,
641
+ opts : opts ,
628
642
}
629
643
}
630
644
@@ -681,6 +695,10 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
681
695
}
682
696
}
683
697
698
+ if rt .opts .userAgent != "" {
699
+ t = NewUserAgentRoundTripper (rt .opts .userAgent , t )
700
+ }
701
+
684
702
ctx := context .WithValue (context .Background (), oauth2 .HTTPClient , & http.Client {Transport : t })
685
703
tokenSource := config .TokenSource (ctx )
686
704
@@ -911,6 +929,28 @@ func (t *tlsRoundTripper) CloseIdleConnections() {
911
929
}
912
930
}
913
931
932
+ type userAgentRoundTripper struct {
933
+ userAgent string
934
+ rt http.RoundTripper
935
+ }
936
+
937
+ // NewUserAgentRoundTripper adds the user agent every request header.
938
+ func NewUserAgentRoundTripper (userAgent string , rt http.RoundTripper ) http.RoundTripper {
939
+ return & userAgentRoundTripper {userAgent , rt }
940
+ }
941
+
942
+ func (rt * userAgentRoundTripper ) RoundTrip (req * http.Request ) (* http.Response , error ) {
943
+ req = cloneRequest (req )
944
+ req .Header .Set ("User-Agent" , rt .userAgent )
945
+ return rt .rt .RoundTrip (req )
946
+ }
947
+
948
+ func (rt * userAgentRoundTripper ) CloseIdleConnections () {
949
+ if ci , ok := rt .rt .(closeIdler ); ok {
950
+ ci .CloseIdleConnections ()
951
+ }
952
+ }
953
+
914
954
func (c HTTPClientConfig ) String () string {
915
955
b , err := yaml .Marshal (c )
916
956
if err != nil {
0 commit comments