-
Notifications
You must be signed in to change notification settings - Fork 39
/
api.go
98 lines (77 loc) · 2.67 KB
/
api.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
87
88
89
90
91
92
93
94
95
96
97
98
package api
import (
"fmt"
"strings"
paramscmd "github.com/wakatime/wakatime-cli/cmd/params"
"github.com/wakatime/wakatime-cli/pkg/api"
"github.com/wakatime/wakatime-cli/pkg/log"
tz "github.com/gandarez/go-olson-timezone"
)
// NewClient initializes a new api client with all options following the
// passed in parameters.
func NewClient(params paramscmd.API) (*api.Client, error) {
withAuth, err := api.WithAuth(api.BasicAuth{
Secret: params.Key,
})
if err != nil {
return nil, fmt.Errorf("failed to set up auth option on api client: %w", err)
}
return newClient(params, withAuth)
}
// NewClientWithoutAuth initializes a new api client with all options following the
// passed in parameters and disabled authentication.
func NewClientWithoutAuth(params paramscmd.API) (*api.Client, error) {
return newClient(params)
}
// newClient contains the logic of client initialization, except auth initialization.
func newClient(params paramscmd.API, opts ...api.Option) (*api.Client, error) {
opts = append(opts, api.WithTimeout(params.Timeout))
opts = append(opts, api.WithHostname(strings.TrimSpace(params.Hostname)))
tz, err := timezone()
if err != nil {
log.Debugf("failed to detect local timezone: %s", err)
} else {
opts = append(opts, api.WithTimezone(strings.TrimSpace(tz)))
}
if params.DisableSSLVerify {
opts = append(opts, api.WithDisableSSLVerify())
}
if !params.DisableSSLVerify && params.SSLCertFilepath != "" {
withSSLCert, err := api.WithSSLCertFile(params.SSLCertFilepath)
if err != nil {
return nil, fmt.Errorf("failed to set up ssl cert file option on api client: %s", err)
}
opts = append(opts, withSSLCert)
} else if !params.DisableSSLVerify {
withSSLCert, err := api.WithSSLCertPool(api.CACerts())
if err != nil {
return nil, fmt.Errorf("failed to set up ssl cert pool option on api client: %s", err)
}
opts = append(opts, withSSLCert)
}
if params.ProxyURL != "" {
withProxy, err := api.WithProxy(params.ProxyURL)
if err != nil {
return nil, fmt.Errorf("failed to set up proxy option on api client: %w", err)
}
opts = append(opts, withProxy)
if strings.Contains(params.ProxyURL, `\\`) {
withNTLMRetry, err := api.WithNTLMRequestRetry(params.ProxyURL)
if err != nil {
return nil, fmt.Errorf("failed to set up ntlm request retry option on api client: %w", err)
}
opts = append(opts, withNTLMRetry)
}
}
opts = append(opts, api.WithUserAgent(params.Plugin))
return api.NewClient(params.URL, opts...), nil
}
func timezone() (name string, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("panicked when detecting timezone: %s", e)
}
}()
name, err = tz.Name()
return name, err
}