Skip to content

client: WithHTTPClient and WithTransport options#25

Merged
andrew merged 1 commit into
mainfrom
client-options-httpclient
May 12, 2026
Merged

client: WithHTTPClient and WithTransport options#25
andrew merged 1 commit into
mainfrom
client-options-httpclient

Conversation

@andrew
Copy link
Copy Markdown
Contributor

@andrew andrew commented May 12, 2026

Summary

Two new `Option` constructors for `client.NewClient`:

  • `WithHTTPClient(*http.Client)` swaps in a caller-supplied client. Useful when the application already maintains a shared connection pool with mTLS config or an auth-injecting transport that the existing `WithTimeout` / `WithMaxRetries` options can't express.
  • `WithTransport(http.RoundTripper)` replaces just the underlying transport while preserving timeout, cookie jar, and other settings. Useful for middleware stacks (auth headers, request logging, external retry, rate-limit).

Both options compose with each other and with the existing `WithUserAgent`, `WithTimeout`, `WithMaxRetries`, `WithRateLimiter`.

Why

Today the field-mutation pattern (`c := client.NewClient(); c.HTTPClient = ...`) works but isn't idiomatic — consumers either know to mutate after construction or copy the `DefaultClient()` shape by hand. The new options follow the existing `Option func(*Client)` pattern so configuration stays declarative in the `NewClient` call site.

Two new Option constructors so callers can plug their own HTTP client
or transport without mutating fields after NewClient returns.

WithHTTPClient swaps in a caller-supplied *http.Client. Useful when
the application already maintains a shared connection pool with mTLS
config or auth-injecting transport that the existing WithTimeout /
WithMaxRetries options can't express.

WithTransport replaces the underlying http.RoundTripper while
preserving the Client's other settings (timeout, cookie jar, ...).
Useful for stacking middleware — auth headers, request logging,
external retry, rate-limit — without rewriting the default Client.

Both options compose with each other and with WithUserAgent,
WithTimeout, WithMaxRetries, and WithRateLimiter.
@andrew andrew merged commit 4a7382d into main May 12, 2026
5 checks passed
@andrew andrew deleted the client-options-httpclient branch May 12, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant