2018-07-04 15:43:52 +03:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
2021-05-14 22:09:01 +03:00
|
|
|
"encoding/json"
|
2018-07-04 15:43:52 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type serverVersionResponse struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchServerVersion reads the version from server.
|
2020-04-28 14:59:57 +03:00
|
|
|
func FetchServerVersion(endpoint string, client *http.Client) (version string, err error) {
|
|
|
|
response, err := client.Get(endpoint)
|
2018-07-04 15:43:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed making version api call")
|
|
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return "", nil
|
|
|
|
default:
|
2020-04-08 13:59:21 +03:00
|
|
|
return "", errors.Errorf("GET %s failed - [%d]", endpoint, response.StatusCode)
|
2018-07-04 15:43:52 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defer response.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "cannot read version api response")
|
|
|
|
}
|
|
|
|
var v serverVersionResponse
|
2021-05-14 22:09:01 +03:00
|
|
|
err = json.Unmarshal(data, &v)
|
2018-07-04 15:43:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to parse version api response")
|
|
|
|
}
|
|
|
|
return v.Version, nil
|
|
|
|
}
|
|
|
|
}
|