-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
59 lines (48 loc) · 1.08 KB
/
conn.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 client
import (
"fmt"
"net/http"
"net/http/httputil"
)
// DialConfig is the configuration to create a api-server connection
type DialConfig struct {
BaseURL string
Token string
Debug bool
UserAgent string
}
func (d *DialConfig) HttpClient() *http.Client {
return &http.Client{
Transport: &AddHeaderTransport{
debug: d.Debug,
T: http.DefaultTransport,
Token: d.Token,
},
}
}
type AddHeaderTransport struct {
debug bool
Token string
T http.RoundTripper
}
func (a *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer "+a.Token)
if a.debug {
reqDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
fmt.Printf("DEBUG ERROR: %s\n", err)
} else {
fmt.Printf("DEBUG REQUEST:\n%s\n", string(reqDump))
}
}
resp, err := a.T.RoundTrip(req)
if a.debug && resp != nil {
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
fmt.Printf("DEBUG ERROR: %s\n", err)
} else {
fmt.Printf("DEBUG RESPONSE:\n%s\n", string(respDump))
}
}
return resp, err
}