Skip to content

Commit

Permalink
controller: Allow setting the http.Client when creating a controller.
Browse files Browse the repository at this point in the history
Adds a HTTPClient field to ControllerArgs and sets the client.HTTPClient
to the given value. If no http client is provided, the gomaasapi.Client
will instantiate it's own client per request.

Signed-off-by: Mark Laing <[email protected]>
  • Loading branch information
markylaing committed Jun 16, 2022
1 parent f8585cb commit 81a2b00
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ var (
// ControllerArgs is an argument struct for passing the required parameters
// to the NewController method.
type ControllerArgs struct {
BaseURL string
APIKey string
BaseURL string
APIKey string
HTTPClient *http.Client
}

// NewController creates an authenticated client to the MAAS API, and
Expand All @@ -59,7 +60,7 @@ func NewController(args ControllerArgs) (Controller, error) {
if !supportedVersion(apiVersion) {
return nil, NewUnsupportedVersionError("version %s", apiVersion)
}
return newControllerWithVersion(base, apiVersion, args.APIKey)
return newControllerWithVersion(base, apiVersion, args.APIKey, args.HTTPClient)
}
return newControllerUnknownVersion(args)
}
Expand All @@ -73,7 +74,7 @@ func supportedVersion(value string) bool {
return false
}

func newControllerWithVersion(baseURL, apiVersion, apiKey string) (Controller, error) {
func newControllerWithVersion(baseURL, apiVersion, apiKey string, httpClient *http.Client) (Controller, error) {
major, minor, err := version.ParseMajorMinor(apiVersion)
// We should not get an error here. See the test.
if err != nil {
Expand All @@ -89,6 +90,8 @@ func newControllerWithVersion(baseURL, apiVersion, apiKey string) (Controller, e
// is an unexpected error and return now.
return nil, NewUnexpectedError(err)
}

client.HTTPClient = httpClient
controllerVersion := version.Number{
Major: major,
Minor: minor,
Expand All @@ -111,7 +114,7 @@ func newControllerUnknownVersion(args ControllerArgs) (Controller, error) {
// some time in the future, we will try the most up to date version and then
// work our way backwards.
for _, apiVersion := range supportedAPIVersions {
controller, err := newControllerWithVersion(args.BaseURL, apiVersion, args.APIKey)
controller, err := newControllerWithVersion(args.BaseURL, apiVersion, args.APIKey, args.HTTPClient)
switch {
case err == nil:
return controller, nil
Expand Down

0 comments on commit 81a2b00

Please sign in to comment.