-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Print the response's body on demand for debugging purposes (#276)
* Implement debugging functionality when reading a response's body * Remove the silliness :) December 28 has passed already * Don't use log.Print for writing to Stdout * Add important note * Update flake.lock * Add fmt.Fprint to the list of unhandled errors * End comment with a period * Remove the comment completely * Document a method as having its documentation pending * Remove TODO line * Activate printing the response body from envvars - Adds `BATON_DEBUG_PRINT_RESPONSE_BODY` * Remove `logBody` unexported function
- Loading branch information
Showing
5 changed files
with
80 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package uhttp | ||
|
||
// Implements a debugging facility for request responses. This changes | ||
// the behavior of `BaseHttpClient` with an unexported flag. | ||
// | ||
// IMPORTANT: This feature is intended for development and debugging purposes only. | ||
// Do not enable in production as it may expose sensitive information in logs. | ||
// | ||
// Usage: | ||
// client := uhttp.NewBaseHttpClient( | ||
// httpClient, | ||
// uhttp.WithPrintBody(true), // Enable response body printing | ||
// ) | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
type printReader struct { | ||
reader io.Reader | ||
} | ||
|
||
func (pr *printReader) Read(p []byte) (int, error) { | ||
n, err := pr.reader.Read(p) | ||
if n > 0 { | ||
_, merr := fmt.Fprint(os.Stdout, string(p[:n])) | ||
if merr != nil { | ||
return -1, errors.Join(err, merr) | ||
} | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
func wrapPrintBody(body io.Reader) io.Reader { | ||
return &printReader{reader: body} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters