Skip to content

Commit

Permalink
🧹 simplify http client initialization (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Mar 28, 2023
1 parent 825a56b commit 12135c1
Showing 1 changed file with 25 additions and 36 deletions.
61 changes: 25 additions & 36 deletions httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@ var (
DefaultTLSHandshakeTimeout = 10 * time.Second
)

func newHttpTransport(proxy *url.URL) *http.Transport {
proxyFn := http.ProxyFromEnvironment
if proxy != nil {
proxyFn = http.ProxyURL(proxy)
// DefaultHttpClient will set up a basic client
// with default timeouts/proxies/etc.
func DefaultHttpClient() *http.Client {
return NewHttpClient()
}

type HttpClientOption func(c *http.Client)

func WithProxy(proxy *url.URL) HttpClientOption {
return func(c *http.Client) {
if proxy == nil {
return
}

switch t := c.Transport.(type) {
case *http.Transport:
t.Proxy = http.ProxyURL(proxy)
}
}
}

func NewHttpClient(opts ...HttpClientOption) *http.Client {
tr := &http.Transport{
Proxy: proxyFn,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: DefaultHttpTimeout,
KeepAlive: 30 * time.Second,
Expand All @@ -31,40 +47,13 @@ func newHttpTransport(proxy *url.URL) *http.Transport {
ExpectContinueTimeout: 1 * time.Second,
}

return tr
}

func newHttpClient(tr *http.Transport) *http.Client {
return &http.Client{
c := &http.Client{
Transport: tr,
Timeout: DefaultHttpTimeout,
}
}

// DefaultHttpClient will set up a basic client
// with default timeouts/proxies/etc.
func DefaultHttpClient() *http.Client {
tr := newHttpTransport(nil)

return newHttpClient(tr)
}

type HttpClientOpts struct {
// Proxy is the string representation of the proxy the client
// should use for connections.
Proxy string
}

func NewHttpClient(opts *HttpClientOpts) (*http.Client, error) {
var proxy *url.URL
if opts.Proxy != "" {
var err error
proxy, err = url.Parse(opts.Proxy)
if err != nil {
return nil, err
}
for i := range opts {
opts[i](c)
}
tr := newHttpTransport(proxy)

return newHttpClient(tr), nil
return c
}

0 comments on commit 12135c1

Please sign in to comment.