-
Notifications
You must be signed in to change notification settings - Fork 20
/
client.go
63 lines (49 loc) · 1.38 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
package cmd
import (
"net/http"
"github.com/exoscale/egoscale"
)
// roundTripper implements the http.RoundTripper interface and allows client
// request customization, such as HTTP headers injection. If provided with a
// non-nil rt parameter, it will wrap around it when performing requests.
type roundTripper struct {
reqHeaders http.Header
rt http.RoundTripper
}
func newRoundTripper(rt http.RoundTripper, headers map[string]string) roundTripper {
var roundTripper = roundTripper{
rt: http.DefaultTransport,
reqHeaders: http.Header{},
}
if rt != nil {
roundTripper.rt = rt
}
for k, v := range headers {
roundTripper.reqHeaders.Add(k, v)
}
return roundTripper
}
func (rt roundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header = rt.reqHeaders
return rt.rt.RoundTrip(r)
}
func buildClient() {
if ignoreClientBuild {
return
}
if cs != nil {
return
}
cs = egoscale.NewClient(gCurrentAccount.Endpoint,
gCurrentAccount.Key,
gCurrentAccount.APISecret())
if gCurrentAccount.CustomHeaders != nil {
cs.HTTPClient.Transport = newRoundTripper(cs.HTTPClient.Transport, gCurrentAccount.CustomHeaders)
}
csDNS = egoscale.NewClient(gCurrentAccount.DNSEndpoint,
gCurrentAccount.Key,
gCurrentAccount.APISecret())
csRunstatus = egoscale.NewClient(gCurrentAccount.RunstatusEndpoint,
gCurrentAccount.Key,
gCurrentAccount.APISecret())
}