Skip to content

Commit 3b428e1

Browse files
committed
simplifying a lot of operations
package flattening
1 parent 275ac33 commit 3b428e1

File tree

9 files changed

+85
-385
lines changed

9 files changed

+85
-385
lines changed

headers/headers_test.go.bk

-194
This file was deleted.

headers/redact/redact.go

-18
This file was deleted.

headers/redact/redact_test.go

-31
This file was deleted.

helpers/helpers_test.go.bk

-46
This file was deleted.
File renamed without changes.

httpclient/request.go

+23-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/deploymenttheory/go-api-http-client/ratehandler"
1212
"github.com/deploymenttheory/go-api-http-client/response"
13-
"github.com/deploymenttheory/go-api-http-client/status"
1413
"go.uber.org/zap"
1514
)
1615

@@ -71,7 +70,7 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
7170
}
7271

7372
if IsIdempotentHTTPMethod(method) {
74-
return c.executeRequestWithRetries(method, endpoint, body, out)
73+
return c.executeRequestWithRetries(method, endpoint, body)
7574
} else if !IsIdempotentHTTPMethod(method) {
7675
return c.executeRequest(method, endpoint, body, out)
7776
} else {
@@ -113,8 +112,7 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
113112
// operations.
114113
// - The retry mechanism employs exponential backoff with jitter to mitigate the impact of retries on the server.
115114
// endregion
116-
func (c *Client) executeRequestWithRetries(method, endpoint string, body, out interface{}) (*http.Response, error) {
117-
// TODO review refactor executeRequestWithRetries
115+
func (c *Client) executeRequestWithRetries(method, endpoint string, body interface{}) (*http.Response, error) {
118116
ctx := context.Background()
119117
totalRetryDeadline := time.Now().Add(c.config.TotalRetryDuration)
120118

@@ -124,28 +122,38 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
124122

125123
c.Sugar.Debug("Executing request with retries", zap.String("method", method), zap.String("endpoint", endpoint))
126124

125+
// TODO removed the blocked comments
126+
// Timer
127127
for time.Now().Before(totalRetryDeadline) {
128-
res, requestErr := c.doRequest(ctx, method, endpoint, body)
128+
129+
// Resp
130+
resp, requestErr := c.doRequest(ctx, method, endpoint, body)
129131
if requestErr != nil {
130132
return nil, requestErr
131133
}
132-
resp = res
133134

135+
// Success
134136
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
135137
if resp.StatusCode >= 300 {
136138
c.Sugar.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
137139
}
138-
return resp, response.HandleAPISuccessResponse(resp, out, c.Sugar)
140+
c.Sugar.Infof("%s request successful at %v", resp.Request.Method, resp.Request.URL)
141+
return resp, nil
139142
}
140143

141-
statusMessage := status.TranslateStatusCode(resp)
144+
statusMessage := http.StatusText(resp.StatusCode)
145+
if statusMessage == "" {
146+
statusMessage = "unknown response code"
147+
}
142148

143-
if resp != nil && status.IsNonRetryableStatusCode(resp) {
149+
// Non Retry
150+
if IsNonRetryableStatusCode(resp) {
144151
c.Sugar.Warn("Non-retryable error received", zap.Int("status_code", resp.StatusCode), zap.String("status_message", statusMessage))
145152
return resp, response.HandleAPIErrorResponse(resp, c.Sugar)
146153
}
147154

148-
if status.IsRateLimitError(resp) {
155+
// Rate limited
156+
if resp.StatusCode == http.StatusTooManyRequests {
149157
waitDuration := ratehandler.ParseRateLimitHeaders(resp, c.Sugar)
150158
if waitDuration > 0 {
151159
c.Sugar.Warn("Rate limit encountered, waiting before retrying", zap.Duration("waitDuration", waitDuration))
@@ -154,7 +162,8 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
154162
}
155163
}
156164

157-
if status.IsTransientError(resp) {
165+
// Transient
166+
if IsTransientError(resp) {
158167
retryCount++
159168
if retryCount > c.config.MaxRetryAttempts {
160169
c.Sugar.Warn("Max retry attempts reached", zap.String("method", method), zap.String("endpoint", endpoint))
@@ -166,12 +175,14 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
166175
continue
167176
}
168177

169-
if !status.IsRetryableStatusCode(resp.StatusCode) {
178+
// Retryable
179+
if !IsRetryableStatusCode(resp.StatusCode) {
170180
if apiErr := response.HandleAPIErrorResponse(resp, c.Sugar); apiErr != nil {
171181
err = apiErr
172182
}
173183
break
174184
}
185+
175186
}
176187

177188
if err != nil {

0 commit comments

Comments
 (0)