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:
Mateusz Filipowicz
2025-02-19 01:28:11 +01:00
committed by GitHub
parent 7c7ef98c03
commit d3a3d5a342
2 changed files with 55 additions and 21 deletions

View File

@@ -58,7 +58,17 @@ if err != nil {
## Customizing the HTTP Client
You can provide your own HTTP client configuration using the `HttpTransportCustomizer` callback. This is useful if you need to tweak connection settings like timeouts, idle connection settings, or TLS configurations:
There are two ways to customize the HTTP client used by the UniFi client:
1. Using the `HttpTransportCustomizer`.
2. Using the `HttpRoundTripperProvider`.
Those methods are mutually exclusive, and only one can be used at a time. If both are provided, the `HttpRoundTripperProvider` takes precedence,
unless it returns `nil`, in which case the `HttpTransportCustomizer` is used if defined (or default transport is used).
### Using `HttpTransportCustomizer`
You can provide your own HTTP client transport configuration using the `HttpTransportCustomizer` callback. This is useful if you need to tweak connection settings like timeouts, idle connection settings,
or TLS configurations:
```go
c, err := unifi.NewClient(&unifi.ClientConfig{
@@ -76,6 +86,21 @@ if err != nil {
}
```
### Using `HttpRoundTripperProvider`
You can provide your own HTTP client configuration using the `HttpRoundTripperProvider` callback. This is useful if you need to create a custom round tripper, when `http.Transport` is not enough:
```go
c, err := unifi.NewClient(&unifi.ClientConfig{
BaseURL: "https://unifi.localdomain",
APIKey: "your-api-key",
HttpRoundTripperProvider: func() http.RoundTripper {
// Create a custom HTTP Round Tripper instance
return &http.Transport{}, nil
},
})
```
## Using Interceptors
Interceptors let you hook into the request and response flow. They can be used for logging, metrics, or modifying requests/responses.

View File

@@ -54,6 +54,7 @@ Fields:
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).
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.
@@ -68,6 +69,7 @@ type ClientConfig struct {
VerifySSL bool
Interceptors []ClientInterceptor
HttpTransportCustomizer HttpTransportCustomizer
HttpRoundTripperProvider func() http.RoundTripper
UserAgent string
ErrorHandler ResponseErrorHandler
UseLocking bool
@@ -152,8 +154,13 @@ 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, "/")
if config.HttpRoundTripperProvider != nil {
rt = config.HttpRoundTripperProvider()
}
if rt == nil {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: !config.VerifySSL},
@@ -163,9 +170,11 @@ func newClientFromConfig(config *ClientConfig, v *validator) (*client, error) {
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})