mirror of
https://github.com/Octops/quake-kube.git
synced 2026-04-06 01:40:33 +00:00
46 lines
768 B
Go
46 lines
768 B
Go
package http
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func GetBody(url string) ([]byte, error) {
|
|
client := http.Client{
|
|
Timeout: 30 * time.Second,
|
|
}
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, errors.Errorf("cannot get url %q: %v", url, http.StatusText(resp.StatusCode))
|
|
}
|
|
|
|
return ioutil.ReadAll(resp.Body)
|
|
}
|
|
|
|
func GetUntil(url string, stop <-chan struct{}) error {
|
|
client := http.Client{
|
|
Timeout: 1 * time.Second,
|
|
}
|
|
for {
|
|
select {
|
|
case <-stop:
|
|
return errors.Errorf("not available: %q", url)
|
|
default:
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
resp.Body.Close()
|
|
return nil
|
|
}
|
|
}
|
|
}
|