Skip to content

Commit

Permalink
Make HTTP client transport replacable
Browse files Browse the repository at this point in the history
Closes: #705
  • Loading branch information
denismakogon committed Jan 21, 2018
1 parent 05df806 commit f6bd8f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ func NewVersionedClient(endpoint string, apiVersionString string) (*Client, erro
return c, nil
}

func (c *Client) WithTransport(tr *http.Transport) {
c.initializeNativeClientTransport(tr)
c.HTTPClient = &http.Client{Transport: tr}
}

// NewVersionnedTLSClient has been DEPRECATED, please use NewVersionedTLSClient.
func NewVersionnedTLSClient(endpoint string, cert, key, ca, apiVersionString string) (*Client, error) {
return NewVersionedTLSClient(endpoint, cert, key, ca, apiVersionString)
Expand Down
11 changes: 8 additions & 3 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package docker
import (
"context"
"net"
"net/http"
)

// initializeNativeClient initializes the native Unix domain socket client on
Expand All @@ -17,13 +18,17 @@ func (c *Client) initializeNativeClient() {
if c.endpointURL.Scheme != unixProtocol {
return
}
socketPath := c.endpointURL.Path
tr := defaultTransport()
c.initializeNativeClientTransport(tr)
}

func (c *Client) initializeNativeClientTransport(tr *http.Transport) {
sockPath := c.endpointURL.Path
tr.Dial = func(network, addr string) (net.Conn, error) {
return c.Dialer.Dial(unixProtocol, socketPath)
return c.Dialer.Dial(unixProtocol, sockPath)
}
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return c.Dialer.Dial(unixProtocol, socketPath)
return c.Dialer.Dial(unixProtocol, sockPath)
}
c.HTTPClient.Transport = tr
}

0 comments on commit f6bd8f3

Please sign in to comment.