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.