Skip to content

Commit 73e3f3a

Browse files
authored
Merge pull request #151 from deploymenttheory/dev
Refactor response/success.go to improve response body logging
2 parents c077ffa + 31522bb commit 73e3f3a

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ module github.com/deploymenttheory/go-api-http-client
33
go 1.21
44

55
require (
6-
github.com/antchfx/xmlquery v1.3.18
6+
github.com/antchfx/xmlquery v1.4.0
77
github.com/google/uuid v1.6.0
88
github.com/stretchr/testify v1.9.0
99
go.uber.org/zap v1.27.0
1010
golang.org/x/net v0.24.0
1111
)
1212

1313
require (
14-
github.com/antchfx/xpath v1.2.4 // indirect
14+
github.com/antchfx/xpath v1.3.0 // indirect
1515
github.com/davecgh/go-spew v1.1.1 // indirect
1616
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
1717
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
github.com/antchfx/xmlquery v1.3.18 h1:FSQ3wMuphnPPGJOFhvc+cRQ2CT/rUj4cyQXkJcjOwz0=
2-
github.com/antchfx/xmlquery v1.3.18/go.mod h1:Afkq4JIeXut75taLSuI31ISJ/zeq+3jG7TunF7noreA=
3-
github.com/antchfx/xpath v1.2.4 h1:dW1HB/JxKvGtJ9WyVGJ0sIoEcqftV3SqIstujI+B9XY=
4-
github.com/antchfx/xpath v1.2.4/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
1+
github.com/antchfx/xmlquery v1.4.0 h1:xg2HkfcRK2TeTbdb0m1jxCYnvsPaGY/oeZWTGqX/0hA=
2+
github.com/antchfx/xmlquery v1.4.0/go.mod h1:Ax2aeaeDjfIw3CwXKDQ0GkwZ6QlxoChlIBP+mGnDFjI=
3+
github.com/antchfx/xpath v1.3.0 h1:nTMlzGAK3IJ0bPpME2urTuFL76o4A96iYvoKFHRXJgc=
4+
github.com/antchfx/xpath v1.3.0/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
77
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=

response/success.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,17 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.L
3333
return handleDeleteRequest(resp, log)
3434
}
3535

36-
// Create a buffer to hold a copy of the response body
37-
var bodyBuffer bytes.Buffer
38-
39-
// Use TeeReader to read the response body and simultaneously write it to the buffer
40-
teeReader := io.TeeReader(resp.Body, &bodyBuffer)
41-
42-
// log the response body
43-
logResponseDetails(resp, bodyBuffer.Bytes(), log)
36+
// Log headers and set up deferred body logging.
37+
deferBodyLog := logResponseDetails(resp, log)
38+
defer deferBodyLog() // Ensure body logging happens at the end of this function.
4439

4540
mimeType, _ := ParseContentTypeHeader(resp.Header.Get("Content-Type"))
4641
contentDisposition := resp.Header.Get("Content-Disposition")
4742

4843
if handler, ok := responseUnmarshallers[mimeType]; ok {
49-
// Replace the response body with a new reader that reads from the buffer, allowing it to be read again
50-
resp.Body = io.NopCloser(&bodyBuffer)
51-
return handler(teeReader, out, log, mimeType) // Use teeReader here to unmarshal from the original response body
44+
return handler(resp.Body, out, log, mimeType)
5245
} else if isBinaryData(mimeType, contentDisposition) {
53-
// Replace the response body with a new reader that reads from the buffer, allowing it to be read again
54-
resp.Body = io.NopCloser(&bodyBuffer)
55-
return handleBinaryData(teeReader, log, out, mimeType, contentDisposition) // Use teeReader here to handle binary data
46+
return handleBinaryData(resp.Body, log, out, mimeType, contentDisposition)
5647
} else {
5748
errMsg := fmt.Sprintf("unexpected MIME type: %s", mimeType)
5849
log.Error("Unmarshal error", zap.String("content type", mimeType), zap.Error(errors.New(errMsg)))
@@ -74,14 +65,22 @@ func handleDeleteRequest(resp *http.Response, log logger.Logger) error {
7465
return fmt.Errorf("DELETE request failed, status code: %d", resp.StatusCode)
7566
}
7667

77-
// Adjusted logResponseDetails to handle a potential nil bodyBytes.
78-
func logResponseDetails(resp *http.Response, bodyBytes []byte, log logger.Logger) {
79-
// Conditional logging if bodyBytes is not nil.
80-
if bodyBytes != nil {
68+
// logResponseDetails logs the raw response details and returns a deferred function to log the response body.
69+
func logResponseDetails(resp *http.Response, log logger.Logger) func() {
70+
log.Debug("HTTP Response Headers", zap.Any("Headers", resp.Header))
71+
72+
// Returning a deferred function to log the body.
73+
return func() {
74+
bodyBytes, err := io.ReadAll(resp.Body)
75+
if err != nil {
76+
log.Error("Error reading response body for logging", zap.Error(err))
77+
return
78+
}
8179
log.Debug("Raw HTTP Response", zap.String("Body", string(bodyBytes)))
80+
81+
// After logging, reset resp.Body so it can be read again.
82+
resp.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
8283
}
83-
// Logging headers remains unchanged.
84-
log.Debug("HTTP Response Headers", zap.Any("Headers", resp.Header))
8584
}
8685

8786
// unmarshalJSON unmarshals JSON content from an io.Reader into the provided output structure.

0 commit comments

Comments
 (0)