Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: allow specifying a custom http client #175

Merged
merged 2 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions http/executor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package http