-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
66 lines (55 loc) · 1.57 KB
/
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
60
61
62
63
64
65
66
package client
import (
"context"
"crypto/tls"
"net"
"net/http"
"strings"
"time"
"github.com/axiomhq/axiom-go/axiom"
"github.com/klauspost/compress/gzhttp"
"github.com/axiomhq/pkg/version"
)
// New returns a new Axiom client.
func New(ctx context.Context, baseURL, accessToken, orgID string, insecure bool) (*axiom.Client, error) {
if baseURL != "" && !strings.HasPrefix(baseURL, "http://") && !strings.HasPrefix(baseURL, "https://") {
baseURL = "https://" + baseURL
}
httpTransport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Second * 30,
KeepAlive: time.Second * 30,
}).DialContext,
IdleConnTimeout: time.Second * 90,
ResponseHeaderTimeout: time.Second * 10,
TLSHandshakeTimeout: time.Second * 10,
ExpectContinueTimeout: time.Second * 1,
ForceAttemptHTTP2: true,
}
if insecure {
httpTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec // This is intended behaviour.
}
httpClient := &http.Client{
Transport: gzhttp.Transport(httpTransport),
}
options := []axiom.Option{
axiom.SetNoEnv(),
axiom.SetUserAgent("axiom-cli/" + version.Release()),
axiom.SetClient(httpClient),
}
if baseURL != "" {
options = append(options, axiom.SetURL(baseURL))
}
if accessToken != "" {
options = append(options, axiom.SetToken(accessToken))
}
if orgID != "" {
options = append(options, axiom.SetOrganizationID(orgID))
}
client, err := axiom.NewClient(options...)
if err != nil {
return nil, err
}
return client, client.ValidateCredentials(ctx)
}