more accurate naming; for now don't actually change how wings works

This commit is contained in:
Dane Everitt 2021-03-03 20:53:40 -08:00
parent 8897be661b
commit 33f5cb7df4
2 changed files with 10 additions and 10 deletions

View File

@ -35,7 +35,7 @@ type client struct {
baseUrl string baseUrl string
tokenId string tokenId string
token string token string
retries int attempts int
} }
// New returns a new HTTP request client that is used for making authenticated // New returns a new HTTP request client that is used for making authenticated
@ -46,7 +46,7 @@ func New(base string, opts ...ClientOption) Client {
httpClient: &http.Client{ httpClient: &http.Client{
Timeout: time.Second * 15, Timeout: time.Second * 15,
}, },
retries: 3, attempts: 1,
} }
for _, opt := range opts { for _, opt := range opts {
opt(&c) opt(&c)
@ -96,10 +96,10 @@ func (c *client) requestOnce(ctx context.Context, method, path string, body io.R
return &Response{res}, err return &Response{res}, err
} }
// request executes a http request and retries when errors occur. // request executes a http request and attempts when errors occur.
// It appends the path to the endpoint of the client and adds the authentication token to the request. // It appends the path to the endpoint of the client and adds the authentication token to the request.
func (c *client) request(ctx context.Context, method, path string, body io.Reader, opts ...func(r *http.Request)) (res *Response, err error) { func (c *client) request(ctx context.Context, method, path string, body io.Reader, opts ...func(r *http.Request)) (res *Response, err error) {
for i := 0; i < c.retries; i++ { for i := 0; i < c.attempts; i++ {
res, err = c.requestOnce(ctx, method, path, body, opts...) res, err = c.requestOnce(ctx, method, path, body, opts...)
if err == nil && if err == nil &&
res.StatusCode < http.StatusInternalServerError && res.StatusCode < http.StatusInternalServerError &&

View File

@ -15,9 +15,9 @@ func createTestClient(h http.HandlerFunc) (*client, *httptest.Server) {
httpClient: s.Client(), httpClient: s.Client(),
baseUrl: s.URL, baseUrl: s.URL,
retries: 1, attempts: 1,
tokenId: "testid", tokenId: "testid",
token: "testtoken", token: "testtoken",
} }
return c, s return c, s
} }
@ -37,7 +37,7 @@ func TestRequest(t *testing.T) {
} }
func TestRequestRetry(t *testing.T) { func TestRequestRetry(t *testing.T) {
// Test if the client retries failed requests // Test if the client attempts failed requests
i := 0 i := 0
c, _ := createTestClient(func(rw http.ResponseWriter, r *http.Request) { c, _ := createTestClient(func(rw http.ResponseWriter, r *http.Request) {
if i < 1 { if i < 1 {
@ -47,7 +47,7 @@ func TestRequestRetry(t *testing.T) {
} }
i++ i++
}) })
c.retries = 2 c.attempts = 2
r, err := c.request(context.Background(), "", "", nil) r, err := c.request(context.Background(), "", "", nil)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, r) assert.NotNil(t, r)
@ -60,7 +60,7 @@ func TestRequestRetry(t *testing.T) {
rw.WriteHeader(http.StatusInternalServerError) rw.WriteHeader(http.StatusInternalServerError)
i++ i++
}) })
c.retries = 2 c.attempts = 2
r, err = c.request(context.Background(), "get", "", nil) r, err = c.request(context.Background(), "get", "", nil)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, r) assert.NotNil(t, r)