Skip to content

Commit 1405fe7

Browse files
committed
appeasing the static check monster
1 parent c9cd26f commit 1405fe7

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

httpclient/request.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ import (
6666

6767
func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*http.Response, error) {
6868
if !c.config.RetryEligiableRequests {
69-
return c.executeRequest(method, endpoint, body, out)
69+
return c.request(method, endpoint, body, out)
7070
}
7171

7272
if IsIdempotentHTTPMethod(method) {
73-
return c.executeRequestWithRetries(method, endpoint, body)
73+
return c.requestWithRetries(method, endpoint, body)
7474
} else if !IsIdempotentHTTPMethod(method) {
75-
return c.executeRequest(method, endpoint, body, out)
75+
return c.request(method, endpoint, body, out)
7676
} else {
7777
return nil, fmt.Errorf("unsupported http method: %s", method)
7878
}
@@ -112,7 +112,7 @@ func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*htt
112112
// operations.
113113
// - The retry mechanism employs exponential backoff with jitter to mitigate the impact of retries on the server.
114114
// endregion
115-
func (c *Client) executeRequestWithRetries(method, endpoint string, body interface{}) (*http.Response, error) {
115+
func (c *Client) requestWithRetries(method, endpoint string, body interface{}) (*http.Response, error) {
116116
var resp *http.Response
117117
var err error
118118
var retryCount int
@@ -226,26 +226,27 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body interfa
226226
// any errors encountered.
227227
//
228228
// endregion
229-
func (c *Client) executeRequest(method, endpoint string, body, out interface{}) (*http.Response, error) {
229+
func (c *Client) request(method, endpoint string, body, out interface{}) (*http.Response, error) {
230230
// TODO review refactor execute Request
231231

232232
ctx := context.Background()
233233

234234
c.Sugar.Debug("Executing request without retries", zap.String("method", method), zap.String("endpoint", endpoint))
235235

236-
res, err := c.doRequest(ctx, method, endpoint, body)
236+
resp, err := c.doRequest(ctx, method, endpoint, body)
237237
if err != nil {
238238
return nil, err
239239
}
240240

241-
if res.StatusCode >= 200 && res.StatusCode < 400 {
242-
if res.StatusCode >= 300 {
243-
c.Sugar.Warn("Redirect response received", zap.Int("status_code", res.StatusCode), zap.String("location", res.Header.Get("Location")))
241+
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusBadRequest {
242+
if resp.StatusCode == http.StatusPermanentRedirect || resp.StatusCode == http.StatusTemporaryRedirect {
243+
c.Sugar.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
244244
}
245-
return res, response.HandleAPISuccessResponse(res, out, c.Sugar)
245+
c.Sugar.Infof("%s request successful at %v", resp.Request.Method, resp.Request.URL)
246+
return resp, nil
246247
}
247248

248-
return nil, response.HandleAPIErrorResponse(res, c.Sugar)
249+
return nil, response.HandleAPIErrorResponse(resp, c.Sugar)
249250
}
250251

251252
func (c *Client) doRequest(ctx context.Context, method, endpoint string, body interface{}) (*http.Response, error) {

response/error.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ func HandleAPIErrorResponse(resp *http.Response, sugar *zap.SugaredLogger) *APIE
6060
mimeType, _ := ParseContentTypeHeader(resp.Header.Get("Content-Type"))
6161
switch mimeType {
6262
case "application/json":
63-
parseJSONResponse(bodyBytes, apiError, resp, sugar)
63+
parseJSONResponse(bodyBytes, apiError)
6464
case "application/xml", "text/xml":
65-
parseXMLResponse(bodyBytes, apiError, resp, sugar)
65+
parseXMLResponse(bodyBytes, apiError)
6666
case "text/html":
67-
parseHTMLResponse(bodyBytes, apiError, resp, sugar)
67+
parseHTMLResponse(bodyBytes, apiError)
6868
case "text/plain":
69-
parseTextResponse(bodyBytes, apiError, resp, sugar)
69+
parseTextResponse(bodyBytes, apiError)
7070
default:
7171
apiError.RawResponse = string(bodyBytes)
7272
apiError.Message = "Unknown content type error"
@@ -76,7 +76,7 @@ func HandleAPIErrorResponse(resp *http.Response, sugar *zap.SugaredLogger) *APIE
7676
}
7777

7878
// parseJSONResponse attempts to parse the JSON error response and update the APIError structure.
79-
func parseJSONResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, sugar *zap.SugaredLogger) {
79+
func parseJSONResponse(bodyBytes []byte, apiError *APIError) {
8080
if err := json.Unmarshal(bodyBytes, apiError); err != nil {
8181
apiError.RawResponse = string(bodyBytes)
8282
} else {
@@ -88,7 +88,7 @@ func parseJSONResponse(bodyBytes []byte, apiError *APIError, resp *http.Response
8888
}
8989

9090
// parseXMLResponse dynamically parses XML error responses and accumulates potential error messages.
91-
func parseXMLResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, sugar *zap.SugaredLogger) {
91+
func parseXMLResponse(bodyBytes []byte, apiError *APIError) {
9292
// Always set the Raw field to the entire XML content for debugging purposes.
9393
apiError.RawResponse = string(bodyBytes)
9494

@@ -121,7 +121,7 @@ func parseXMLResponse(bodyBytes []byte, apiError *APIError, resp *http.Response,
121121
}
122122

123123
// parseTextResponse updates the APIError structure based on a plain text error response and logs it.
124-
func parseTextResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, sugar *zap.SugaredLogger) {
124+
func parseTextResponse(bodyBytes []byte, apiError *APIError) {
125125
// Convert the body bytes to a string and assign it to both the message and RawResponse fields of APIError.
126126
bodyText := string(bodyBytes)
127127
apiError.RawResponse = bodyText
@@ -133,7 +133,7 @@ func parseTextResponse(bodyBytes []byte, apiError *APIError, resp *http.Response
133133

134134
// parseHTMLResponse extracts meaningful information from an HTML error response,
135135
// concatenating all text within <p> tags and links found within them.
136-
func parseHTMLResponse(bodyBytes []byte, apiError *APIError, resp *http.Response, sugar *zap.SugaredLogger) {
136+
func parseHTMLResponse(bodyBytes []byte, apiError *APIError) {
137137
// Set the entire HTML content as the RawResponse for debugging purposes.
138138
apiError.RawResponse = string(bodyBytes)
139139

response/success.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, sugar *zap.S
5252
if handler, ok := responseUnmarshallers[mimeType]; ok {
5353
return handler(bodyReader, out, sugar, mimeType)
5454
} else if isBinaryData(mimeType, contentDisposition) {
55-
return handleBinaryData(bodyReader, sugar, out, mimeType, contentDisposition)
55+
return handleBinaryData(bodyReader, sugar, out, contentDisposition)
5656
} else {
5757
errMsg := fmt.Sprintf("unexpected MIME type: %s", mimeType)
5858
sugar.Error("Unmarshal error", zap.String("content type", mimeType), zap.Error(errors.New(errMsg)))
@@ -97,7 +97,7 @@ func isBinaryData(contentType, contentDisposition string) bool {
9797
}
9898

9999
// handleBinaryData reads binary data from an io.Reader and stores it in *[]byte or streams it to an io.Writer.
100-
func handleBinaryData(reader io.Reader, sugar *zap.SugaredLogger, out interface{}, mimeType, contentDisposition string) error {
100+
func handleBinaryData(reader io.Reader, sugar *zap.SugaredLogger, out interface{}, contentDisposition string) error {
101101
// Check if the output interface is either *[]byte or io.Writer
102102
switch out := out.(type) {
103103
case *[]byte:

0 commit comments

Comments
 (0)