Files
kapow/internal/client/get.go
pancho horrillo 23128026c7 Rework directory structure
Co-authored-by: Héctor Hurtado <hector.hurtado@bbva.com>
2019-10-03 12:05:53 +02:00

31 lines
574 B
Go

package client
import (
"errors"
"io"
"net/http"
"strings"
)
func getReason(r *http.Response) string {
return strings.Join(strings.Split(r.Status, " ")[1:], " ")
}
//GetData will perform the request and write the results on the provided writer
func GetData(host, id, path string, wr io.Writer) error {
url := host + "/handlers/" + id + path
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
return errors.New(getReason(res))
}
_, err = io.Copy(wr, res.Body)
return err
}