diff --git a/http_client_wrapper.go b/http_client_wrapper.go index f2d192c..e908ea9 100644 --- a/http_client_wrapper.go +++ b/http_client_wrapper.go @@ -20,7 +20,7 @@ type httpClientWrapper struct { baseURL string headers map[string]string torProxyURL string - retry int + retry uint } type HTTPResponseError struct { @@ -32,7 +32,7 @@ func BuildResponseError(statusCode int, err error) error { return HTTPResponseError{StatusCode: statusCode, error: err} } -func NewHTTPClientWrapper(baseURL, torProxyURL string, timeout time.Duration, headers map[string]string, addJSONHeaders bool, opts ...HttpClientWrapperOption) (HTTPClientWrapper, error) { +func NewHTTPClientWrapper(baseURL, torProxyURL string, timeout time.Duration, headers map[string]string, addJSONHeaders bool, opts ...HTTPClientWrapperOption) (HTTPClientWrapper, error) { if headers == nil { headers = map[string]string{} } @@ -51,7 +51,7 @@ func NewHTTPClientWrapper(baseURL, torProxyURL string, timeout time.Duration, he client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)} } - params := defaultHttpClientWrapperOptions + params := defaultHTTPClientWrapperOptions for _, opt := range opts { opt(¶ms) } @@ -65,19 +65,23 @@ func NewHTTPClientWrapper(baseURL, torProxyURL string, timeout time.Duration, he }, nil } -type HttpClientWrapperOptions struct { - Retry int +type HTTPClientWrapperOptions struct { + Retry uint } -var defaultHttpClientWrapperOptions = HttpClientWrapperOptions{ +var defaultHTTPClientWrapperOptions = HTTPClientWrapperOptions{ Retry: 5, } -type HttpClientWrapperOption func(*HttpClientWrapperOptions) +type HTTPClientWrapperOption func(*HTTPClientWrapperOptions) -func WithRetry(retry int) HttpClientWrapperOption { - return func(opts *HttpClientWrapperOptions) { - opts.Retry = retry +func WithRetry(retry uint) HTTPClientWrapperOption { + return func(opts *HTTPClientWrapperOptions) { + if retry >= 0 { + opts.Retry = retry + } else { + opts.Retry = defaultHTTPClientWrapperOptions.Retry + } } } @@ -113,7 +117,7 @@ func (h httpClientWrapper) ExecuteRequest(path, method string, body []byte) (*ht return errResp } return err - }, retry.Attempts(uint(h.retry)), retry.Delay(500*time.Millisecond), retry.MaxDelay(9*time.Second)) + }, retry.Attempts(h.retry), retry.Delay(500*time.Millisecond), retry.MaxDelay(9*time.Second)) if res == nil { return nil, err diff --git a/http_client_wrapper_test.go b/http_client_wrapper_test.go index 908b657..6510c74 100644 --- a/http_client_wrapper_test.go +++ b/http_client_wrapper_test.go @@ -10,7 +10,7 @@ import ( func Test_httpClientWrapper_ExecuteRequest_failsTooManyRetries(t *testing.T) { const baseURL = "http://test.com" - retryCount := 5 + retryCount := uint(5) hcw, _ := NewHTTPClientWrapper(baseURL, "", 1, nil, true, WithRetry(retryCount)) httpmock.Activate() @@ -21,7 +21,7 @@ func Test_httpClientWrapper_ExecuteRequest_failsTooManyRetries(t *testing.T) { countInfo := httpmock.GetCallCountInfo() c := countInfo["GET "+baseURL+path] - assert.Equal(t, retryCount, c, "expected five retries") + assert.Equal(t, int(retryCount), c, "expected five retries") assert.Error(t, err, "expected error") assert.ErrorIs(t, err, err.(HTTPResponseError), "expected HTTPResponseError") assert.Equal(t, 503, err.(HTTPResponseError).StatusCode, "expected 409")