Skip to content

Commit

Permalink
add MakeRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
baduker committed Oct 5, 2023
1 parent 0e017aa commit c74e05d
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c74e05d

Please sign in to comment.