Skip to content

Commit

Permalink
Activate printing the response body from envvars
Browse files Browse the repository at this point in the history
- Adds `BATON_DEBUG_PRINT_RESPONSE_BODY`
  • Loading branch information
shackra committed Jan 10, 2025
1 parent 267b1f1 commit 0017a64
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
12 changes: 0 additions & 12 deletions pkg/uhttp/body_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,3 @@ func (pr *printReader) Read(p []byte) (int, error) {
func wrapPrintBody(body io.Reader) io.Reader {
return &printReader{reader: body}
}

type printBodyOption struct {
debugPrintBody bool
}

func (o printBodyOption) Apply(c *BaseHttpClient) {
c.debugPrintBody = o.debugPrintBody
}

func WithPrintBody(shouldPrint bool) WrapperOption {
return printBodyOption{debugPrintBody: shouldPrint}
}
11 changes: 6 additions & 5 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"net/url"
"os"
"syscall"
"time"

Expand Down Expand Up @@ -91,10 +92,9 @@ type (
NewRequest(ctx context.Context, method string, url *url.URL, options ...RequestOption) (*http.Request, error)
}
BaseHttpClient struct {
HttpClient *http.Client
rateLimiter uRateLimit.Limiter
baseHttpCache icache
debugPrintBody bool
HttpClient *http.Client
rateLimiter uRateLimit.Limiter
baseHttpCache icache
}

DoOption func(resp *WrapperResponse) error
Expand Down Expand Up @@ -342,7 +342,8 @@ func (c *BaseHttpClient) Do(req *http.Request, options ...DoOption) (*http.Respo
}

// Replace resp.Body with a no-op closer so nobody has to worry about closing the reader.
if c.debugPrintBody {
shouldPrint := os.Getenv("BATON_DEBUG_PRINT_RESPONSE_BODY")
if shouldPrint != "" {
resp.Body = io.NopCloser(wrapPrintBody(bytes.NewBuffer(body)))
} else {
resp.Body = io.NopCloser(bytes.NewBuffer(body))
Expand Down

0 comments on commit 0017a64

Please sign in to comment.