mirror of
https://github.com/Luzifer/go_helpers.git
synced 2024-12-24 13:01:21 +00:00
Add http.LogRoundTripper
helper for request debugging
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
523cc2d8d5
commit
68572d1889
1 changed files with 61 additions and 0 deletions
61
http/logRoundTrip.go
Normal file
61
http/logRoundTrip.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type (
|
||||
// LogRoundTripper is a drop-in for the Transport of a http.Client
|
||||
// which then will log requests and responses to the given writer
|
||||
// (os.Stdout / os.Stderr / logrus writer / ...)
|
||||
LogRoundTripper struct {
|
||||
next http.RoundTripper
|
||||
output io.Writer
|
||||
}
|
||||
)
|
||||
|
||||
var _ http.RoundTripper = LogRoundTripper{}
|
||||
|
||||
// NewLogRoundTripper creates a new LogRoundTripper with the given
|
||||
// next transport and output writer. If no next transport is given
|
||||
// (next == nil) the http.DefaultTransport is used. If no writer
|
||||
// is given the io.Discard writer is used.
|
||||
func NewLogRoundTripper(next http.RoundTripper, out io.Writer) LogRoundTripper {
|
||||
if next == nil {
|
||||
next = http.DefaultTransport
|
||||
}
|
||||
|
||||
if out == nil {
|
||||
// Makes no sense but ensures we don't fail writing to nil
|
||||
out = io.Discard
|
||||
}
|
||||
|
||||
return LogRoundTripper{next, out}
|
||||
}
|
||||
|
||||
// RoundTrip implements http.RoundTripper interface
|
||||
func (l LogRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
dump, err := httputil.DumpRequest(req, true)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "dumping request")
|
||||
}
|
||||
|
||||
fmt.Fprintf(l.output, "---- 8< REQ >8 ----\n%s", dump)
|
||||
|
||||
resp, err := l.next.RoundTrip(req)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
if dump, err = httputil.DumpResponse(resp, true); err != nil {
|
||||
return nil, errors.Wrap(err, "dumping response")
|
||||
}
|
||||
|
||||
fmt.Fprintf(l.output, "---- 8< RES >8 ----\n%s---- 8< END >8 ----\n", dump)
|
||||
|
||||
return resp, err
|
||||
}
|
Loading…
Reference in a new issue