-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
72 lines (60 loc) · 1.55 KB
/
transport.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
package gh
import (
"fmt"
"log/slog"
"net/http"
"net/http/httputil"
)
// Printf is a (fmt.Printf|log.Printf|testing.T.Logf)-like function.
type Printf func(format string, args ...any)
// NoopPrintf is a Printf function that does nothing.
func NoopPrintf(string, ...any) {}
// SLogPrintf is a Printf function that uses given slog.Logger with Debug level.
func SLogPrintf(l *slog.Logger) Printf {
return func(format string, args ...any) {
l.Debug(fmt.Sprintf(format, args...))
}
}
// transport is an http.RoundTripper with debug logging.
type transport struct {
t http.RoundTripper
debugf Printf
}
// NewTransport returns a new http.RoundTripper that wraps the source with debug logging.
// Both should be set.
func NewTransport(source http.RoundTripper, debugf Printf) http.RoundTripper {
if source == nil {
panic("source is nil")
}
if debugf == nil {
panic("debugf is nil")
}
return &transport{
t: source,
debugf: debugf,
}
}
// RoundTrip implements the http.RoundTripper interface.
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
b, dumpErr := httputil.DumpRequestOut(req, true)
if dumpErr == nil {
t.debugf("Request:\n%s", b)
} else {
t.debugf("DumpRequestOut failed: %s", dumpErr)
}
resp, err := t.t.RoundTrip(req)
if resp == nil {
return nil, err
}
b, dumpErr = httputil.DumpResponse(resp, true)
if dumpErr == nil {
t.debugf("Response:\n%s", b)
} else {
t.debugf("DumpResponse failed: %s", dumpErr)
}
return resp, err
}
// check interfaces
var (
_ http.RoundTripper = (*transport)(nil)
)