forked from buildkite/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_client.go
59 lines (48 loc) · 1.23 KB
/
api_client.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
package agent
import (
"net"
"net/http"
"net/url"
"runtime"
"time"
"golang.org/x/net/http2"
"github.com/buildkite/agent/api"
)
var debug = false
type APIClient struct {
Endpoint string
Token string
}
func APIClientEnableHTTPDebug() {
debug = true
}
func (a APIClient) Create() *api.Client {
httpTransport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: false,
DisableCompression: false,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 30 * time.Second,
}
http2.ConfigureTransport(httpTransport)
// Create the transport used when making the Buildkite Agent API calls
transport := &api.AuthenticatedTransport{
Token: a.Token,
Transport: httpTransport,
}
// From the transport, create the a http client
httpClient := transport.Client()
httpClient.Timeout = 60 * time.Second
// Create the Buildkite Agent API Client
client := api.NewClient(httpClient)
client.BaseURL, _ = url.Parse(a.Endpoint)
client.UserAgent = a.UserAgent()
client.DebugHTTP = debug
return client
}
func (a APIClient) UserAgent() string {
return "buildkite-agent/" + Version() + " (" + runtime.GOOS + "; " + runtime.GOARCH + ")"
}