Allow injecting a custom HTTP client for Fleet API requests - #464
Open
a2105z wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #23.
Problem
Accountandinet.Connectioneach owned an unexportedhttp.Clientwith no public way to replace it. Apps that need request logging, custom timeouts, metrics, or test transports were stuck mutatinghttp.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,GetVehiclealways constructed a freshinet.NewConnectionwith its own default client, so vehicle Fleet API traffic still would not have used it.Solution
Account.SetHTTPClient(*http.Client)forGet/Post/ Fleet API helpersinet.Connection.SetHTTPClient(*http.Client)for direct connector usersGetVehiclenilrestores a client with Go's default settingsExample:
@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 (SetClientvsSetHTTPClient) if you prefer.Type of change
Checklist:
Confirm you have completed the following steps:
Test plan
go test ./pkg/account/ ./pkg/connector/inet/ -count=1 -vgo test ./... -count=1Account.Get/PostGetVehicle→WakeupSetHTTPClient(nil)restores a default clientinet.Connection.SetHTTPClientused bySendFleetAPICommand