graphql-engine/cli/version/server.go

42 lines
1014 B
Go
Raw Normal View History

package version
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
type serverVersionResponse struct {
Version string `json:"version"`
}
// FetchServerVersion reads the version from server.
func FetchServerVersion(endpoint string, client *http.Client) (version string, err error) {
response, err := client.Get(endpoint)
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:
return "", errors.Errorf("GET %s failed - [%d]", endpoint, response.StatusCode)
}
} 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
err = json.Unmarshal(data, &v)
if err != nil {
return "", errors.Wrap(err, "failed to parse version api response")
}
return v.Version, nil
}
}