feat: allow creating own http.RoundTripper for http.Client with HttpRoundTripperProvider when customizing pre-configured http.Transport with HttpTransportCustomizer is not sufficient (#31)
feat: allow creating own http.RoundTripper for http.Client with `HttpRoundTripperProvider` when customizing pre-configured http.Transport with `HttpTransportCustomizer` is not sufficient
This commit is contained in:
committed by
GitHub
parent
7c7ef98c03
commit
d3a3d5a342
@@ -53,25 +53,27 @@ Fields:
|
||||
Timeout: The maximum duration to wait for responses; default is no timeout.
|
||||
VerifySSL: When false, disables SSL certificate verification.
|
||||
Interceptors: A slice of ClientInterceptor implementations that can modify requests and responses.
|
||||
HttpTransportCustomizer:An optional function to customize the HTTP transport (e.g., for custom TLS settings).
|
||||
HttpTransportCustomizer: An optional function to customize the HTTP transport (e.g., for custom TLS settings).
|
||||
HttpRoundTripperProvider: A function that returns a http.RoundTripper for customizing the HTTP client. If both HttpTransportCustomizer and HttpRoundTripperProvider are provided, HttpRoundTripperProvider takes precedence.
|
||||
UserAgent: The User-Agent header string for outgoing HTTP requests.
|
||||
ErrorHandler: A custom handler for processing HTTP response errors.
|
||||
UseLocking: If true, enables internal locking for concurrent request processing.
|
||||
ValidationMode:The mode for validating request bodies. Can be "soft", "hard", or "disable".
|
||||
*/
|
||||
type ClientConfig struct {
|
||||
URL string `validate:"required,http_url"`
|
||||
APIKey string `validate:"required_without_all=User Password"`
|
||||
User string `validate:"excluded_with=APIKey,required_with=Password"`
|
||||
Password string `validate:"excluded_with=APIKey,required_with=User"`
|
||||
Timeout time.Duration // How long to wait for replies, default: forever.
|
||||
VerifySSL bool
|
||||
Interceptors []ClientInterceptor
|
||||
HttpTransportCustomizer HttpTransportCustomizer
|
||||
UserAgent string
|
||||
ErrorHandler ResponseErrorHandler
|
||||
UseLocking bool
|
||||
ValidationMode validationMode `validate:"omitempty,oneof=soft hard disable"`
|
||||
URL string `validate:"required,http_url"`
|
||||
APIKey string `validate:"required_without_all=User Password"`
|
||||
User string `validate:"excluded_with=APIKey,required_with=Password"`
|
||||
Password string `validate:"excluded_with=APIKey,required_with=User"`
|
||||
Timeout time.Duration // How long to wait for replies, default: forever.
|
||||
VerifySSL bool
|
||||
Interceptors []ClientInterceptor
|
||||
HttpTransportCustomizer HttpTransportCustomizer
|
||||
HttpRoundTripperProvider func() http.RoundTripper
|
||||
UserAgent string
|
||||
ErrorHandler ResponseErrorHandler
|
||||
UseLocking bool
|
||||
ValidationMode validationMode `validate:"omitempty,oneof=soft hard disable"`
|
||||
}
|
||||
|
||||
// Credentials abstracts authentication credentials.
|
||||
@@ -152,20 +154,27 @@ func parseBaseURL(base string) (*url.URL, error) {
|
||||
}
|
||||
|
||||
func newClientFromConfig(config *ClientConfig, v *validator) (*client, error) {
|
||||
var rt http.RoundTripper
|
||||
var err error
|
||||
config.URL = strings.TrimRight(config.URL, "/")
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: !config.VerifySSL},
|
||||
if config.HttpRoundTripperProvider != nil {
|
||||
rt = config.HttpRoundTripperProvider()
|
||||
}
|
||||
if config.HttpTransportCustomizer != nil {
|
||||
if transport, err = config.HttpTransportCustomizer(transport); err != nil {
|
||||
return nil, fmt.Errorf("failed customizing HTTP transport: %w", err)
|
||||
if rt == nil {
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: !config.VerifySSL},
|
||||
}
|
||||
if config.HttpTransportCustomizer != nil {
|
||||
if transport, err = config.HttpTransportCustomizer(transport); err != nil {
|
||||
return nil, fmt.Errorf("failed customizing HTTP transport: %w", err)
|
||||
}
|
||||
}
|
||||
rt = transport
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Timeout: config.Timeout,
|
||||
Transport: transport,
|
||||
Transport: rt,
|
||||
}
|
||||
if config.APIKey == "" {
|
||||
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||
|
||||
Reference in New Issue
Block a user