Skip to content

Allow injecting a custom HTTP client for Fleet API requests - #464

Open
a2105z wants to merge 1 commit into
teslamotors:mainfrom
a2105z:fix-23-configurable-http-client
Open

Allow injecting a custom HTTP client for Fleet API requests#464
a2105z wants to merge 1 commit into
teslamotors:mainfrom
a2105z:fix-23-configurable-http-client

Conversation

@a2105z

@a2105z a2105z commented Aug 9, 2026

Copy link
Copy Markdown

Description

Fixes #23.

Problem

Account and inet.Connection each owned an unexported http.Client with no public way to replace it. Apps that need request logging, custom timeouts, metrics, or test transports were stuck mutating http.DefaultClient, which is unsafe/shared in larger processes — exactly the alternative @andig called out when opening the issue.

Additionally, even if Account's internal client had been set somehow, GetVehicle always constructed a fresh inet.NewConnection with its own default client, so vehicle Fleet API traffic still would not have used it.

Solution

  • Add Account.SetHTTPClient(*http.Client) for Get / Post / Fleet API helpers
  • Add inet.Connection.SetHTTPClient(*http.Client) for direct connector users
  • Propagate the Account client into connections created by GetVehicle
  • nil restores a client with Go's default settings

Example:

acct, err := account.New(token, "my-app")
acct.SetHTTPClient(&http.Client{
    Transport: loggingRoundTripper{Base: http.DefaultTransport},
    Timeout:   30 * time.Second,
})

@sethterashima @patrickdemers6 — labeled good first issue / help wanted; this is the minimal public API to unblock custom transports without touching http.DefaultClient. Happy to adjust naming (SetClient vs SetHTTPClient) if you prefer.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update

Checklist:

Confirm you have completed the following steps:

  • My code follows the style of this project.
  • I have performed a self-review of my code.
  • I have made corresponding updates to the documentation.
  • I have added/updated unit tests to cover my changes.

Test plan

  • go test ./pkg/account/ ./pkg/connector/inet/ -count=1 -v
  • go test ./... -count=1
  • Custom RoundTripper used by Account.Get / Post
  • Custom client propagates through GetVehicleWakeup
  • SetHTTPClient(nil) restores a default client
  • inet.Connection.SetHTTPClient used by SendFleetAPICommand

Account and inet.Connection previously hardcoded an unexported
http.Client, so applications could not install a RoundTripper for
logging, metrics, or timeouts without mutating http.DefaultClient.
Add SetHTTPClient and propagate Account's client through GetVehicle.

Fixes teslamotors#23
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.

Proposal: configurable HTTP client

1 participant