Skip to content

Commit 8e28303

Browse files
committed
Remove unused code for authentication and rate handling
1 parent f75ee00 commit 8e28303

File tree

9 files changed

+112
-7
lines changed

9 files changed

+112
-7
lines changed
File renamed without changes.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/deploymenttheory/go-api-http-client/httpmethod"
1313
"github.com/deploymenttheory/go-api-http-client/logger"
1414
"github.com/deploymenttheory/go-api-http-client/ratehandler"
15+
"github.com/deploymenttheory/go-api-http-client/response"
1516
"github.com/deploymenttheory/go-api-http-client/status"
1617
"go.uber.org/zap"
1718
)
@@ -195,7 +196,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
195196
// Check for non-retryable errors
196197
if resp != nil && status.IsNonRetryableStatusCode(resp) {
197198
log.Warn("Non-retryable error received", zap.Int("status_code", resp.StatusCode), zap.String("status_message", statusMessage))
198-
return resp, handleAPIErrorResponse(resp, log)
199+
return resp, response.HandleAPIErrorResponse(resp, log)
199200
}
200201

201202
// Parsing rate limit headers if a rate-limit error is detected
@@ -223,7 +224,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
223224

224225
// Handle error responses
225226
if err != nil || !status.IsRetryableStatusCode(resp.StatusCode) {
226-
if apiErr := handleAPIErrorResponse(resp, log); apiErr != nil {
227+
if apiErr := response.HandleAPIErrorResponse(resp, log); apiErr != nil {
227228
err = apiErr
228229
}
229230
log.LogError("request_error", method, endpoint, resp.StatusCode, resp.Status, err, status.TranslateStatusCode(resp))
@@ -236,7 +237,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
236237
return nil, err
237238
}
238239

239-
return resp, handleAPIErrorResponse(resp, log)
240+
return resp, response.HandleAPIErrorResponse(resp, log)
240241
}
241242

242243
// executeRequest executes an HTTP request using the specified method, endpoint, and request body without implementing
File renamed without changes.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// httpclient_error_response.go
1+
// response/error.go
22
// This package provides utility functions and structures for handling and categorizing HTTP error responses.
3-
package httpclient
3+
package response
44

55
import (
66
"encoding/json"
@@ -39,8 +39,8 @@ func (e *APIError) Error() string {
3939
return string(data)
4040
}
4141

42-
// handleAPIErrorResponse attempts to parse the error response from the API and logs using the zap logger.
43-
func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
42+
// HandleAPIErrorResponse attempts to parse the error response from the API and logs using the zap logger.
43+
func HandleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
4444
apiError := &APIError{
4545
StatusCode: resp.StatusCode,
4646
Type: "APIError", // Default error type

response/error_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package response
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/deploymenttheory/go-api-http-client/mocklogger"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/mock"
11+
)
12+
13+
// MockLogger is a mock type for the Logger interface, useful for testing without needing a real logger implementation.
14+
type MockLogger struct {
15+
mock.Mock
16+
}
17+
18+
func (m *MockLogger) Error(msg string, fields ...interface{}) error {
19+
args := m.Called(msg, fields)
20+
return args.Error(0)
21+
}
22+
23+
// TestHandleAPIErrorResponse tests the handling of various API error responses.
24+
func TestHandleAPIErrorResponse(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
responseStatus int
28+
responseBody string
29+
expectedAPIError *APIError
30+
}{
31+
{
32+
name: "structured JSON error",
33+
responseStatus: http.StatusBadRequest,
34+
responseBody: `{"error": {"code": "400", "message": "Bad Request"}}`,
35+
expectedAPIError: &APIError{
36+
StatusCode: http.StatusBadRequest,
37+
Type: "APIError",
38+
Message: "An error occurred",
39+
Raw: `{"error": {"code": "400", "message": "Bad Request"}}`,
40+
},
41+
},
42+
{
43+
name: "generic JSON error",
44+
responseStatus: http.StatusInternalServerError,
45+
responseBody: `{"message": "Internal Server Error", "detail": "Server crashed"}`,
46+
expectedAPIError: &APIError{
47+
StatusCode: http.StatusInternalServerError,
48+
Type: "APIError",
49+
Message: "An error occurred",
50+
Raw: `{"message": "Internal Server Error", "detail": "Server crashed"}`,
51+
},
52+
},
53+
{
54+
name: "non-JSON error",
55+
responseStatus: http.StatusServiceUnavailable,
56+
responseBody: `<html><body>Service Unavailable</body></html>`,
57+
expectedAPIError: &APIError{
58+
StatusCode: http.StatusServiceUnavailable,
59+
Type: "APIError",
60+
Message: "An error occurred",
61+
Raw: `<html><body>Service Unavailable</body></html>`,
62+
},
63+
},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
// Mock HTTP response
69+
responseRecorder := httptest.NewRecorder()
70+
responseRecorder.WriteHeader(tt.responseStatus)
71+
responseRecorder.WriteString(tt.responseBody)
72+
73+
// Create a dummy request and associate it with the response
74+
dummyReq := httptest.NewRequest("GET", "http://example.com", nil)
75+
response := responseRecorder.Result()
76+
response.Request = dummyReq
77+
78+
// Use the centralized MockLogger from the mocklogger package
79+
mockLogger := mocklogger.NewMockLogger()
80+
81+
// Set up expectations for LogError
82+
mockLogger.On("LogError",
83+
mock.AnythingOfType("string"), // event
84+
mock.AnythingOfType("string"), // method
85+
mock.AnythingOfType("string"), // url
86+
mock.AnythingOfType("int"), // statusCode
87+
mock.AnythingOfType("string"), // status
88+
mock.Anything, // error
89+
mock.AnythingOfType("string"), // raw response
90+
).Return()
91+
92+
// Call HandleAPIErrorResponse
93+
result := HandleAPIErrorResponse(response, mockLogger)
94+
95+
// Assert
96+
assert.Equal(t, tt.expectedAPIError.StatusCode, result.StatusCode)
97+
assert.Equal(t, tt.expectedAPIError.Type, result.Type)
98+
assert.Equal(t, tt.expectedAPIError.Raw, result.Raw)
99+
100+
// Assert that all expectations were met
101+
mockLogger.AssertExpectations(t)
102+
})
103+
}
104+
}

0 commit comments

Comments
 (0)