-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.go
46 lines (40 loc) · 1.01 KB
/
new.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
package restyclient
import (
"net/http"
"net/http/cookiejar"
"github.com/admpub/log"
"github.com/admpub/resty/v2"
"github.com/webx-top/com"
"github.com/webx-top/com/httpClientOptions"
"golang.org/x/net/publicsuffix"
)
func NewCookiejar() (*cookiejar.Jar, error) {
return cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
}
func ProxyURL(proxy string) func(c *http.Client) {
return func(c *http.Client) {
if err := SetProxy(c, proxy); err != nil {
log.Error(err)
}
}
}
func NewWithOptions(options ...com.HTTPClientOptions) *resty.Client {
hclient := com.HTTPClientWithTimeout(
DefaultTimeout,
options...,
)
return resty.NewWithClient(hclient)
}
func New(proxy ...string) *resty.Client {
cookieJar, _ := NewCookiejar()
options := []com.HTTPClientOptions{
httpClientOptions.Timeout(DefaultTimeout),
httpClientOptions.CookieJar(cookieJar),
}
if len(proxy) > 0 && len(proxy[0]) > 0 {
options = append(options, ProxyURL(proxy[0]))
}
return NewWithOptions(options...)
}