Skip to content

Commit

Permalink
Merge pull request #175 from ipfs/fix/174
Browse files Browse the repository at this point in the history
http: allow specifying a custom http client
  • Loading branch information
Stebalien authored Aug 27, 2019
2 parents ba86293 + 3b171a2 commit 1d203a2
Show file tree
Hide file tree
Showing 2 changed files with 13 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
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

0 comments on commit 1d203a2

Please sign in to comment.