@@ -140,34 +140,63 @@ func (c *Client) getBaseURL() string {
140140 return fmt .Sprintf ("%s/%s" , c .config .baseURL , APIVersion )
141141}
142142
143- type configFn func (o * config )
143+ type ConfigFn func (o * config )
144144
145- func WithBaseURL (baseURL string ) configFn {
145+ // WithBaseURL modifies the client baseURL
146+ func WithBaseURL (baseURL string ) ConfigFn {
146147 return func (c * config ) {
147148 c .baseURL = baseURL
148149 }
149150}
150151
151- func WithHTTPClient (httpClient * http.Client ) configFn {
152+ // WithInsecureSkipVerify modifies the client's httpClient to skip verifying
153+ // the server's certificate chain and host name. This should be used only for testing.
154+ func WithInsecureSkipVerify () ConfigFn {
155+ return func (c * config ) {
156+ if c .httpClient != nil { // #nosec G402 // allow setting InsecureSkipVerify to true as explicitly requested
157+ if t , ok := c .httpClient .Transport .(* http.Transport ); ok {
158+ cfg := & tls.Config {InsecureSkipVerify : true }
159+ if t .TLSClientConfig == nil {
160+ t .TLSClientConfig = cfg
161+
162+ return
163+ }
164+
165+ t .TLSClientConfig .InsecureSkipVerify = cfg .InsecureSkipVerify
166+ }
167+ }
168+ }
169+ }
170+
171+ // WithHTTPClient replaces the client's default httpClient with the specified one
172+ func WithHTTPClient (httpClient * http.Client ) ConfigFn {
152173 return func (c * config ) {
153174 c .httpClient = httpClient
154175 }
155176}
156177
157- func WithTimeout (timeout time.Duration ) configFn {
178+ // WithTimeout modifies the client's httpClient timeout
179+ func WithTimeout (timeout time.Duration ) ConfigFn {
158180 return func (c * config ) {
159181 c .httpClient .Timeout = timeout
160182 }
161183}
162184
163- // New creates and returns a new client configured with the specified user and password
164- func New (username , password string , c ... configFn ) * Client {
185+ // New creates and returns a new client configured with the specified user and password and optional
186+ // config functions.
187+ func New (username , password string , c ... ConfigFn ) * Client {
165188 config := config {
166189 username : username ,
167190 password : password ,
168191 baseURL : clientBaseURL (os .Getenv (EnvDebugAPIBaseURL )),
169- httpClient : httpClient (),
192+ httpClient : cleanhttp .DefaultClient (),
193+ }
194+
195+ // If set, replace http client transport with one skipping tls verification
196+ if os .Getenv (EnvDebugSkipCertificateVerify ) == "1" {
197+ c = append (c , WithInsecureSkipVerify ())
170198 }
199+
171200 for _ , fn := range c {
172201 fn (& config )
173202 }
@@ -177,34 +206,20 @@ func New(username, password string, c ...configFn) *Client {
177206 }
178207}
179208
180- func httpClient () * http.Client {
181- var client * http.Client
182- if os .Getenv (EnvDebugSkipCertificateVerify ) == "1" {
183- client = & http.Client {
184- Transport : & http.Transport {
185- TLSClientConfig : & tls.Config {
186- InsecureSkipVerify : true , //nolint
187- },
188- },
189- }
190- } else {
191- client = cleanhttp .DefaultClient ()
192- }
193- return client
194- }
195-
196209func userAgent () string {
197210 return fmt .Sprintf ("upcloud-go-api/%s" , Version )
198211}
199212
200213func clientBaseURL (URL string ) string {
201- if URL != "" {
202- if u , err := url .Parse (URL ); err != nil || u .Scheme == "" || u .Host == "" {
203- return APIBaseURL
204- }
205- return URL
214+ if URL == "" {
215+ return APIBaseURL
206216 }
207- return APIBaseURL
217+
218+ if u , err := url .Parse (URL ); err != nil || u .Scheme == "" || u .Host == "" {
219+ return APIBaseURL
220+ }
221+
222+ return URL
208223}
209224
210225// Parses the response and returns either the response body or an error
0 commit comments