2014-03-18 20:00:41 +04:00
|
|
|
package datasource
|
|
|
|
|
2014-04-23 02:36:07 +04:00
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
type Datasource interface {
|
|
|
|
Fetch() ([]byte, error)
|
|
|
|
Type() string
|
|
|
|
}
|
2014-04-23 02:36:07 +04:00
|
|
|
|
|
|
|
func fetchURL(url string) ([]byte, error) {
|
|
|
|
client := http.Client{}
|
|
|
|
resp, err := client.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode / 100 != 2 {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return respBytes, nil
|
|
|
|
}
|