Skip to content

Commit

Permalink
http: allow specifying a custom http client
Browse files Browse the repository at this point in the history
fixes #174
  • Loading branch information
Stebalien committed Aug 26, 2019
1 parent 6b2c4ae commit 3b171a2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,25 @@ type client struct {
fallback cmds.Executor
}

// ClientOpt is an option that can be passed to the HTTP client constructor.
type ClientOpt func(*client)

// ClientWithUserAgent specifies the HTTP user agent for the client.
func ClientWithUserAgent(ua string) ClientOpt {
return func(c *client) {
c.ua = ua
}
}

// ClientWithHTTPClient specifies a custom http.Client. Defaults to
// http.DefaultClient.
func ClientWithHTTPClient(hc *http.Client) ClientOpt {
return func(c *client) {
c.httpClient = hc
}
}

// ClientWithAPIPrefix specifies an API URL prefix.
func ClientWithAPIPrefix(apiPrefix string) ClientOpt {
return func(c *client) {
c.apiPrefix = apiPrefix
Expand All @@ -53,6 +64,7 @@ func ClientWithFallback(exe cmds.Executor) ClientOpt {
}
}

// NewClient constructs a new HTTP-backed command executor.
func NewClient(address string, opts ...ClientOpt) cmds.Executor {
if !strings.HasPrefix(address, "http://") {
address = "http://" + address
Expand Down

0 comments on commit 3b171a2

Please sign in to comment.