Skip to content

Commit c077ffa

Browse files
authored
Merge pull request #150 from deploymenttheory/dev
refactored func HandleAPISuccessResponse
2 parents 8fd0f59 + c6c4c89 commit c077ffa

File tree

5 files changed

+32
-30
lines changed

5 files changed

+32
-30
lines changed

headers/headers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/deploymenttheory/go-api-http-client/apiintegrations/apihandler"
1010
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
1111
"github.com/deploymenttheory/go-api-http-client/headers/redact"
12+
"github.com/deploymenttheory/go-api-http-client/version"
1213

1314
"github.com/deploymenttheory/go-api-http-client/logger"
1415
"go.uber.org/zap"
@@ -97,6 +98,11 @@ func SetCustomHeader(req *http.Request, headerName, headerValue string) {
9798
req.Header.Set(headerName, headerValue)
9899
}
99100

101+
// SetUserAgentHeader sets the User-Agent header for an HTTP request.
102+
func SetUserAgentHeader() string {
103+
return fmt.Sprintf("%s/%s", version.UserAgentBase, version.SDKVersion)
104+
}
105+
100106
// SetRequestHeaders sets the necessary HTTP headers for a given request using the APIHandler to determine the required headers.
101107
func (h *HeaderHandler) SetRequestHeaders(endpoint string) {
102108
// Retrieve the standard headers required for the request

headers/headers_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
package headers
33

44
import (
5+
"fmt"
56
"net/http"
67
"net/http/httptest"
78
"testing"
89

910
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
11+
"github.com/deploymenttheory/go-api-http-client/version"
1012
"github.com/stretchr/testify/assert"
1113
)
1214

@@ -127,6 +129,14 @@ func TestSetCustomHeader(t *testing.T) {
127129
assert.Equal(t, headerValue, req.Header.Get(headerName), "Custom header should be correctly set")
128130
}
129131

132+
// TestSetUserAgentHeader verifies that the SetUserAgentHeader function returns the expected user agent string
133+
func TestSetUserAgentHeader(t *testing.T) {
134+
expectedUserAgent := fmt.Sprintf("%s/%s", version.UserAgentBase, version.SDKVersion)
135+
userAgent := SetUserAgentHeader()
136+
137+
assert.Equal(t, expectedUserAgent, userAgent, "User agent string should match expected format")
138+
}
139+
130140
// TestSetRequestHeaders verifies that standard headers, including a custom Authorization header,
131141
// are set correctly on the HTTP request based on headers provided by a mock APIHandler.
132142
// TODO need to implement MockAPIHandler

response/success.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ and unmarshals the response based on the content type (JSON or XML). */
44
package response
55

66
import (
7+
"bytes"
78
"encoding/json"
89
"encoding/xml"
910
"errors"
@@ -32,18 +33,26 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.L
3233
return handleDeleteRequest(resp, log)
3334
}
3435

35-
// No need to read the entire body into memory, pass resp.Body directly.
36-
logResponseDetails(resp, nil, log) // Updated to handle nil bodyBytes.
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)
3744

3845
mimeType, _ := ParseContentTypeHeader(resp.Header.Get("Content-Type"))
3946
contentDisposition := resp.Header.Get("Content-Disposition")
4047

4148
if handler, ok := responseUnmarshallers[mimeType]; ok {
42-
// Pass resp.Body directly to the handler for streaming.
43-
return handler(resp.Body, out, log, mimeType)
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
4452
} else if isBinaryData(mimeType, contentDisposition) {
45-
// For binary data, we still need to handle the body directly.
46-
return handleBinaryData(resp.Body, log, out, 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
4756
} else {
4857
errMsg := fmt.Sprintf("unexpected MIME type: %s", mimeType)
4958
log.Error("Unmarshal error", zap.String("content type", mimeType), zap.Error(errors.New(errMsg)))

version/version.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
// version.go
22
package version
33

4-
import "fmt"
5-
64
const (
7-
SDKVersion = "0.0.90"
5+
SDKVersion = "0.1.21"
86
UserAgentBase = "go-api-http-client"
97
)
10-
11-
func GetUserAgentHeader() string {
12-
return fmt.Sprintf("%s/%s", UserAgentBase, SDKVersion)
13-
}

version/version_test.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)