@@ -10,7 +10,6 @@ import (
10
10
11
11
"github.com/deploymenttheory/go-api-http-client/ratehandler"
12
12
"github.com/deploymenttheory/go-api-http-client/response"
13
- "github.com/deploymenttheory/go-api-http-client/status"
14
13
"go.uber.org/zap"
15
14
)
16
15
@@ -71,7 +70,7 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
71
70
}
72
71
73
72
if IsIdempotentHTTPMethod (method ) {
74
- return c .executeRequestWithRetries (method , endpoint , body , out )
73
+ return c .executeRequestWithRetries (method , endpoint , body )
75
74
} else if ! IsIdempotentHTTPMethod (method ) {
76
75
return c .executeRequest (method , endpoint , body , out )
77
76
} else {
@@ -113,8 +112,7 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
113
112
// operations.
114
113
// - The retry mechanism employs exponential backoff with jitter to mitigate the impact of retries on the server.
115
114
// 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 ) {
118
116
ctx := context .Background ()
119
117
totalRetryDeadline := time .Now ().Add (c .config .TotalRetryDuration )
120
118
@@ -124,28 +122,38 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
124
122
125
123
c .Sugar .Debug ("Executing request with retries" , zap .String ("method" , method ), zap .String ("endpoint" , endpoint ))
126
124
125
+ // TODO removed the blocked comments
126
+ // Timer
127
127
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 )
129
131
if requestErr != nil {
130
132
return nil , requestErr
131
133
}
132
- resp = res
133
134
135
+ // Success
134
136
if resp .StatusCode >= 200 && resp .StatusCode < 400 {
135
137
if resp .StatusCode >= 300 {
136
138
c .Sugar .Warn ("Redirect response received" , zap .Int ("status_code" , resp .StatusCode ), zap .String ("location" , resp .Header .Get ("Location" )))
137
139
}
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
139
142
}
140
143
141
- statusMessage := status .TranslateStatusCode (resp )
144
+ statusMessage := http .StatusText (resp .StatusCode )
145
+ if statusMessage == "" {
146
+ statusMessage = "unknown response code"
147
+ }
142
148
143
- if resp != nil && status .IsNonRetryableStatusCode (resp ) {
149
+ // Non Retry
150
+ if IsNonRetryableStatusCode (resp ) {
144
151
c .Sugar .Warn ("Non-retryable error received" , zap .Int ("status_code" , resp .StatusCode ), zap .String ("status_message" , statusMessage ))
145
152
return resp , response .HandleAPIErrorResponse (resp , c .Sugar )
146
153
}
147
154
148
- if status .IsRateLimitError (resp ) {
155
+ // Rate limited
156
+ if resp .StatusCode == http .StatusTooManyRequests {
149
157
waitDuration := ratehandler .ParseRateLimitHeaders (resp , c .Sugar )
150
158
if waitDuration > 0 {
151
159
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
154
162
}
155
163
}
156
164
157
- if status .IsTransientError (resp ) {
165
+ // Transient
166
+ if IsTransientError (resp ) {
158
167
retryCount ++
159
168
if retryCount > c .config .MaxRetryAttempts {
160
169
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
166
175
continue
167
176
}
168
177
169
- if ! status .IsRetryableStatusCode (resp .StatusCode ) {
178
+ // Retryable
179
+ if ! IsRetryableStatusCode (resp .StatusCode ) {
170
180
if apiErr := response .HandleAPIErrorResponse (resp , c .Sugar ); apiErr != nil {
171
181
err = apiErr
172
182
}
173
183
break
174
184
}
185
+
175
186
}
176
187
177
188
if err != nil {
0 commit comments