2015-03-29 21:17:23 +00:00
|
|
|
package httputils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
)
|
|
|
|
|
2015-06-16 09:51:27 +00:00
|
|
|
// Download requests a given URL and returns an io.Reader
|
2015-03-29 21:17:23 +00:00
|
|
|
func Download(url string) (resp *http.Response, err error) {
|
|
|
|
if resp, err = http.Get(url); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, fmt.Errorf("Got HTTP status code >= 400: %s", resp.Status)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 09:51:27 +00:00
|
|
|
// NewHTTPRequestError returns a JSON response error
|
2015-03-29 21:17:23 +00:00
|
|
|
func NewHTTPRequestError(msg string, res *http.Response) error {
|
|
|
|
return &jsonmessage.JSONError{
|
|
|
|
Message: msg,
|
|
|
|
Code: res.StatusCode,
|
|
|
|
}
|
|
|
|
}
|