-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwporg.go
57 lines (48 loc) · 1.15 KB
/
wporg.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
package wporg
import (
"net"
"net/http"
"runtime"
"time"
)
// Client contains data required for making requests to the API
type Client struct {
UserAgent string
HTTPClient *http.Client
}
// NewClient returns a new client for accessing the WordPress.org APIs
func NewClient(options ...func(c *Client)) *Client {
c := &Client{}
for _, option := range options {
option(c)
}
// Set default user-agent if not set
if c.UserAgent == "" {
c.UserAgent = "wporg/1.0"
}
// Set default client if not set
if c.HTTPClient == nil {
c.HTTPClient = getDefaultClient()
}
return c
}
func getDefaultClient() *http.Client {
var netTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
}
httpClient := &http.Client{
Timeout: time.Second * time.Duration(30),
Transport: netTransport,
}
return httpClient
}