diff --git a/api/client.go b/api/client.go index a65cd3235..6477e5acd 100644 --- a/api/client.go +++ b/api/client.go @@ -76,35 +76,45 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) { // TokenIsValid calls the API to determine whether the token is valid. func (c *Client) TokenIsValid() (bool, error) { - url := fmt.Sprintf("%s/validate_token", c.APIBaseURL) - req, err := c.NewRequest("GET", url, nil) - if err != nil { - return false, err - } - resp, err := c.Do(req) + resp, err := c.MakeRequest("/validate_token", false) if err != nil { return false, err } defer resp.Body.Close() - return resp.StatusCode == http.StatusOK, nil } // IsPingable calls the API /ping to determine whether the API can be reached. func (c *Client) IsPingable() error { - url := fmt.Sprintf("%s/ping", c.APIBaseURL) - req, err := c.NewRequest("GET", url, nil) - if err != nil { - return err - } - resp, err := c.Do(req) + resp, err := c.MakeRequest("/ping", false) if err != nil { return err } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { return fmt.Errorf("API returned %s", resp.Status) } return nil } + +// MakeRequest makes a http request to the given path (which may be a full URL). +func (c *Client) MakeRequest(path string, isFullURL bool) (*http.Response, error) { + var url string + + if isFullURL { + url = path + } else { + url = fmt.Sprintf("%s%s", c.APIBaseURL, path) + } + + req, err := c.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + resp, err := c.Do(req) + if err != nil { + return nil, err + } + + return resp, nil +}