Skip to content

Commit

Permalink
impr(verbose): using package httpretty to log requests on DEBUG.
Browse files Browse the repository at this point in the history
  • Loading branch information
henvic committed Feb 21, 2020
1 parent b5d0b7c commit e7c88d0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
37 changes: 14 additions & 23 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"regexp"
"strings"

"github.com/henvic/httpretty"
)

// ClientOption represents an argument to NewClient
Expand Down Expand Up @@ -37,29 +39,18 @@ func AddHeader(name, value string) ClientOption {

// VerboseLog enables request/response logging within a RoundTripper
func VerboseLog(out io.Writer, logBodies bool) ClientOption {
return func(tr http.RoundTripper) http.RoundTripper {
return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
fmt.Fprintf(out, "> %s %s\n", req.Method, req.URL.RequestURI())
if logBodies && req.Body != nil && inspectableMIMEType(req.Header.Get("Content-type")) {
newBody := &bytes.Buffer{}
io.Copy(out, io.TeeReader(req.Body, newBody))
fmt.Fprintln(out)
req.Body = ioutil.NopCloser(newBody)
}
res, err := tr.RoundTrip(req)
if err == nil {
fmt.Fprintf(out, "< HTTP %s\n", res.Status)
if logBodies && res.Body != nil && inspectableMIMEType(res.Header.Get("Content-type")) {
newBody := &bytes.Buffer{}
// TODO: pretty-print response JSON
io.Copy(out, io.TeeReader(res.Body, newBody))
fmt.Fprintln(out)
res.Body = ioutil.NopCloser(newBody)
}
}
return res, err
}}
}
logger := &httpretty.Logger{
RequestHeader: true,
RequestBody: logBodies,
ResponseHeader: true,
ResponseBody: logBodies,
Formatters: []httpretty.Formatter{&httpretty.JSONFormatter{}},
}
logger.SetOutput(out)
logger.SetBodyFilter(func(h http.Header) (skip bool, err error) {
return !inspectableMIMEType(h.Get("Content-Type")), nil
})
return logger.RoundTripper
}

// ReplaceTripper substitutes the underlying RoundTripper with a custom one
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/hashicorp/go-version v1.2.0
github.com/henvic/httpretty v0.0.3
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mattn/go-colorable v0.1.4
github.com/mattn/go-isatty v0.0.12
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/henvic/httpretty v0.0.3 h1:oHTreVv2lcdRYUNm4h3cgbrGN0dTieO9H8UnxEZNlvw=
github.com/henvic/httpretty v0.0.3/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down

0 comments on commit e7c88d0

Please sign in to comment.