-
Notifications
You must be signed in to change notification settings - Fork 0
/
roundtripper.go
74 lines (63 loc) · 1.5 KB
/
roundtripper.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
package log
import (
"net/http"
"time"
)
// MARK: Public Functions
func (rl *RequestLogger) RoundTripper(client *http.Client) http.RoundTripper {
var c *http.Client
if client != nil {
c = client
} else if rl.client != nil {
c = rl.client
} else {
c = http.DefaultClient
}
return roundTripper{
RequestLogger: rl,
Client: c,
}
}
func (rt roundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
log, err, _ := makeLog(req, RequestLoggerConfig{
Headers: rt.RequestLogger.logHeaders,
Params: rt.RequestLogger.logParams,
Body: rt.RequestLogger.logBody,
})
if err != nil {
rt.logger.Errord("error creating request log for "+req.URL.String()+":", err)
return rt.Client.Do(req)
}
start := time.Now()
res, err = rt.Client.Do(req)
log.latency = time.Since(start)
log.contextError = req.Context().Err()
rt.log(log)
return
}
func (rl *RequestLogger) RoundTrip(req *http.Request) (res *http.Response, err error) {
client := rl.client
if client == nil {
client = http.DefaultClient
}
log, err, _ := makeLog(req, RequestLoggerConfig{
Headers: rl.logHeaders,
Params: rl.logParams,
Body: rl.logBody,
})
if err != nil {
rl.logger.Errord("error creating request log for "+req.URL.String()+":", err)
return rl.client.Do(req)
}
start := time.Now()
res, err = rl.client.Do(req)
log.latency = time.Since(start)
log.contextError = req.Context().Err()
rl.log(log)
return
}
// MARK: Private Types
type roundTripper struct {
*RequestLogger
*http.Client
}