diff --git a/api.go b/api.go index 01f8f09..130910b 100644 --- a/api.go +++ b/api.go @@ -72,42 +72,42 @@ type ClientPersistent struct { // GetVersion returns version func (c *Client) GetVersion() (string, error) { - resp, err := c.api.DoAndGetResponseBody( - context.Background(), http.MethodGet, "/api/version", nil, nil, c.configConnect.Version) - if err != nil { - return "", err - } - defer func() { - if err := resp.Body.Close(); err != nil { - doLog(log.WithError(err).Error, "") - } - }() - // parse the response - switch { - case resp == nil: - return "", errNilReponse - case resp.StatusCode == http.StatusUnauthorized: - // Authenticate then try again - if _, err = c.Authenticate(c.configConnect); err != nil { - return "", err - } - resp, err = c.api.DoAndGetResponseBody( - context.Background(), http.MethodGet, "/api/version", nil, nil, c.configConnect.Version) - if err != nil { - return "", err - } - case resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusUnauthorized: - return "", c.api.ParseJSONError(resp) - } - version, err := extractString(resp) - if err != nil { - return "", err - } - versionRX := regexp.MustCompile(`^(\d+?\.\d+?).*$`) - if m := versionRX.FindStringSubmatch(version); len(m) > 0 { - return m[1], nil - } - return version, nil + resp, err := c.api.DoAndGetResponseBody( + context.Background(), http.MethodGet, "/api/version", nil, nil, c.configConnect.Version) + if err != nil { + return "", err + } + defer func() { + if err := resp.Body.Close(); err != nil { + doLog(log.WithError(err).Error, "") + } + }() + // parse the response + switch { + case resp == nil: + return "", errNilReponse + case resp.StatusCode == http.StatusUnauthorized: + // Authenticate then try again + if _, err = c.Authenticate(c.configConnect); err != nil { + return "", err + } + resp, err = c.api.DoAndGetResponseBody( + context.Background(), http.MethodGet, "/api/version", nil, nil, c.configConnect.Version) + if err != nil { + return "", err + } + case resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusUnauthorized: + return "", c.api.ParseJSONError(resp) + } + version, err := extractString(resp) + if err != nil { + return "", err + } + versionRX := regexp.MustCompile(`^(\d+?\.\d+?).*$`) + if m := versionRX.FindStringSubmatch(version); len(m) > 0 { + return m[1], nil + } + return version, nil } // updateVersion updates version