Browse code

Add an op func to override Client.scheme

Signed-off-by: Peter Kang <peter@spell.run>

Peter Kang authored on 2018/11/02 02:40:37
Showing 1 changed files
... ...
@@ -234,6 +234,14 @@ func WithHTTPHeaders(headers map[string]string) func(*Client) error {
234 234
 	}
235 235
 }
236 236
 
237
+// WithScheme overrides the client scheme with the specified one
238
+func WithScheme(scheme string) func(*Client) error {
239
+	return func(c *Client) error {
240
+		c.scheme = scheme
241
+		return nil
242
+	}
243
+}
244
+
237 245
 // NewClientWithOpts initializes a new API client with default values. It takes functors
238 246
 // to modify values when creating it, like `NewClientWithOpts(WithVersion(…))`
239 247
 // It also initializes the custom http headers to add to each request.
... ...
@@ -249,7 +257,6 @@ func NewClientWithOpts(ops ...func(*Client) error) (*Client, error) {
249 249
 	c := &Client{
250 250
 		host:    DefaultDockerHost,
251 251
 		version: api.DefaultVersion,
252
-		scheme:  "http",
253 252
 		client:  client,
254 253
 		proto:   defaultProto,
255 254
 		addr:    defaultAddr,
... ...
@@ -264,14 +271,18 @@ func NewClientWithOpts(ops ...func(*Client) error) (*Client, error) {
264 264
 	if _, ok := c.client.Transport.(http.RoundTripper); !ok {
265 265
 		return nil, fmt.Errorf("unable to verify TLS configuration, invalid transport %v", c.client.Transport)
266 266
 	}
267
-	tlsConfig := resolveTLSConfig(c.client.Transport)
268
-	if tlsConfig != nil {
269
-		// TODO(stevvooe): This isn't really the right way to write clients in Go.
270
-		// `NewClient` should probably only take an `*http.Client` and work from there.
271
-		// Unfortunately, the model of having a host-ish/url-thingy as the connection
272
-		// string has us confusing protocol and transport layers. We continue doing
273
-		// this to avoid breaking existing clients but this should be addressed.
274
-		c.scheme = "https"
267
+	if c.scheme == "" {
268
+		c.scheme = "http"
269
+
270
+		tlsConfig := resolveTLSConfig(c.client.Transport)
271
+		if tlsConfig != nil {
272
+			// TODO(stevvooe): This isn't really the right way to write clients in Go.
273
+			// `NewClient` should probably only take an `*http.Client` and work from there.
274
+			// Unfortunately, the model of having a host-ish/url-thingy as the connection
275
+			// string has us confusing protocol and transport layers. We continue doing
276
+			// this to avoid breaking existing clients but this should be addressed.
277
+			c.scheme = "https"
278
+		}
275 279
 	}
276 280
 
277 281
 	return c, nil