-
Notifications
You must be signed in to change notification settings - Fork 5
/
options.go
86 lines (78 loc) · 2.04 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package httpclient
import (
"crypto/tls"
"github.com/ansel1/merry"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)
// NoRedirects configures the client to no perform any redirects.
func NoRedirects() Option {
return OptionFunc(func(client *http.Client) error {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
return nil
})
}
// MaxRedirects configures the max number of redirects the client will perform before
// giving up.
func MaxRedirects(max int) Option {
return OptionFunc(func(client *http.Client) error {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= max {
return merry.Errorf("stopped after max %d requests", len(via))
}
return nil
}
return nil
})
}
// CookieJar installs a cookie jar into the client, configured with the options argument.
//
// The argument will be nil.
func CookieJar(opts *cookiejar.Options) Option {
return OptionFunc(func(client *http.Client) error {
jar, err := cookiejar.New(opts)
if err != nil {
return merry.Wrap(err)
}
client.Jar = jar
return nil
})
}
// ProxyURL will proxy all calls through a single proxy URL.
func ProxyURL(proxyURL string) Option {
return TransportOption(func(t *http.Transport) error {
u, err := url.Parse(proxyURL)
if err != nil {
return merry.Wrap(err)
}
t.Proxy = func(request *http.Request) (*url.URL, error) {
return u, nil
}
return nil
})
}
// ProxyFunc configures the client's proxy function.
func ProxyFunc(f func(request *http.Request) (*url.URL, error)) Option {
return TransportOption(func(t *http.Transport) error {
t.Proxy = f
return nil
})
}
// Timeout configures the client's Timeout property.
func Timeout(d time.Duration) Option {
return OptionFunc(func(client *http.Client) error {
client.Timeout = d
return nil
})
}
// SkipVerify sets the TLS config's InsecureSkipVerify flag.
func SkipVerify(skip bool) Option {
return TLSOption(func(c *tls.Config) error {
c.InsecureSkipVerify = skip
return nil
})
}