Skip to content

Commit f6bd8f3

Browse files
committed
Make HTTP client transport replacable
Closes: #705
1 parent 05df806 commit f6bd8f3

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ func NewVersionedClient(endpoint string, apiVersionString string) (*Client, erro
222222
return c, nil
223223
}
224224

225+
func (c *Client) WithTransport(tr *http.Transport) {
226+
c.initializeNativeClientTransport(tr)
227+
c.HTTPClient = &http.Client{Transport: tr}
228+
}
229+
225230
// NewVersionnedTLSClient has been DEPRECATED, please use NewVersionedTLSClient.
226231
func NewVersionnedTLSClient(endpoint string, cert, key, ca, apiVersionString string) (*Client, error) {
227232
return NewVersionedTLSClient(endpoint, cert, key, ca, apiVersionString)

client_unix.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package docker
99
import (
1010
"context"
1111
"net"
12+
"net/http"
1213
)
1314

1415
// initializeNativeClient initializes the native Unix domain socket client on
@@ -17,13 +18,17 @@ func (c *Client) initializeNativeClient() {
1718
if c.endpointURL.Scheme != unixProtocol {
1819
return
1920
}
20-
socketPath := c.endpointURL.Path
2121
tr := defaultTransport()
22+
c.initializeNativeClientTransport(tr)
23+
}
24+
25+
func (c *Client) initializeNativeClientTransport(tr *http.Transport) {
26+
sockPath := c.endpointURL.Path
2227
tr.Dial = func(network, addr string) (net.Conn, error) {
23-
return c.Dialer.Dial(unixProtocol, socketPath)
28+
return c.Dialer.Dial(unixProtocol, sockPath)
2429
}
2530
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
26-
return c.Dialer.Dial(unixProtocol, socketPath)
31+
return c.Dialer.Dial(unixProtocol, sockPath)
2732
}
2833
c.HTTPClient.Transport = tr
2934
}

0 commit comments

Comments
 (0)