2021-09-04 21:21:13 +00:00
|
|
|
// Package web provides functionality for dealing with web operations.
|
2021-08-09 14:16:33 +00:00
|
|
|
package web
|
2021-07-28 20:10:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-09-04 21:18:34 +00:00
|
|
|
// Timeout is the time it takes before a web request bails out waiting for a
|
|
|
|
// request.
|
|
|
|
const Timeout = 10 * time.Second
|
|
|
|
|
2021-08-09 14:16:33 +00:00
|
|
|
// ReadJSON reads JSON and parses it into your chosen interface pointer
|
|
|
|
func ReadJSON(url string, target interface{}) error {
|
2021-09-04 21:18:34 +00:00
|
|
|
httpClient := &http.Client{Timeout: Timeout}
|
2021-07-28 20:10:13 +00:00
|
|
|
res, err := httpClient.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
return json.NewDecoder(res.Body).Decode(target)
|
|
|
|
}
|