-
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.
Implement debugging functionality when reading a response's body
- Loading branch information
Showing
2 changed files
with
50 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package uhttp | ||
|
||
// Implements a debugging facility for request responses. This changes | ||
// the behavior of `BaseHttpClient` with an unexported flag. | ||
// | ||
// If you always wanted to see the actual body of your response, now | ||
// you can 👁️👄👁️undress it🫦 to uncover all its... data! | ||
|
||
import ( | ||
"io" | ||
"log" | ||
) | ||
|
||
type printReader struct { | ||
reader io.Reader | ||
} | ||
|
||
func (pr *printReader) Read(p []byte) (int, error) { | ||
n, err := pr.reader.Read(p) | ||
if n > 0 { | ||
log.Print(string(p[:n])) | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
func wrapBodyToUndress(body io.Reader) io.Reader { | ||
return &printReader{reader: body} | ||
} | ||
|
||
type undressOption struct { | ||
undress bool | ||
} | ||
|
||
func (o undressOption) Apply(c *BaseHttpClient) { | ||
c.debugPrintBody = o.undress | ||
} | ||
|
||
func WithUndressBody(undress bool) WrapperOption { | ||
return undressOption{undress: undress} | ||
} |
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