diff --git a/api/client/api.go b/api/client/api.go index fb388f03bd..b8625463c9 100644 --- a/api/client/api.go +++ b/api/client/api.go @@ -80,9 +80,36 @@ func NewClientWithConfig(ctx context.Context, input NewClientInput) (Client, err // ErrNoAuthCredential returns an error when no auth credentials have been provided such as cookies or tokens var ErrNoAuthCredential = errors.New("unable to create an API session with cookie nor token") -func (c *Client) getRequest(relPath string, data interface{}, response interface{}) error { +// getRequestConfig defines configuration for a GET request +type getRequestConfig struct { + retryCount uint +} + +// withRetryCount is a convenience function for setting the retry count +func withRetryCount(retryCount uint) func(*getRequestConfig) { + return func(o *getRequestConfig) { + o.retryCount = retryCount + } +} + +// getRequest is responsible for making a GET request to the API +func (c *Client) getRequest(relPath string, data interface{}, response interface{}, opts ...func(*getRequestConfig)) error { + config := &getRequestConfig{ + retryCount: 1, + } + + for _, opt := range opts { + opt(config) + } + + var httpErr *types.ExternalError + var err error + vals := make(map[string][]string) - err := schema.NewEncoder().Encode(data, vals) + err = schema.NewEncoder().Encode(data, vals) + if err != nil { + return err + } urlVals := url.Values(vals) encodedURLVals := urlVals.Encode() @@ -106,15 +133,27 @@ func (c *Client) getRequest(relPath string, data interface{}, response interface return err } - if httpErr, err := c.sendRequest(req, response, true); httpErr != nil || err != nil { - if httpErr != nil { - return fmt.Errorf("%v", httpErr.Error) + for i := 0; i < int(config.retryCount); i++ { + httpErr, err = c.sendRequest(req, response, true) + + if httpErr == nil && err == nil { + return nil } - return err + if i != int(config.retryCount)-1 { + if httpErr != nil { + fmt.Fprintf(os.Stderr, "Error: %s (status code %d), retrying request...\n", httpErr.Error, httpErr.Code) + } else { + fmt.Fprintf(os.Stderr, "Error: %v, retrying request...\n", err) + } + } } - return nil + if httpErr != nil { + return fmt.Errorf("%v", httpErr.Error) + } + + return err } type postRequestOpts struct { diff --git a/api/client/k8s.go b/api/client/k8s.go index bf50cb2673..67f32a00fc 100644 --- a/api/client/k8s.go +++ b/api/client/k8s.go @@ -182,6 +182,7 @@ func (c *Client) GetRelease( ), nil, resp, + withRetryCount(3), ) return resp, err