forked from cloud66-oss/starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
52 lines (46 loc) · 1.02 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/cloud66/starter/common"
)
func fetch(url string, mod *time.Time) (io.ReadCloser, error) {
common.PrintlnL2("Downloading from %s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if mod != nil {
req.Header.Add("If-Modified-Since", mod.Format(http.TimeFormat))
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if mod != nil && resp.StatusCode == 304 {
return nil, errors.New("item moved")
}
if resp.StatusCode != 200 {
err := fmt.Errorf("bad http status from %s: %v", url, resp.Status)
return nil, err
}
if s := resp.Header.Get("Last-Modified"); mod != nil && s != "" {
t, err := time.Parse(http.TimeFormat, s)
if err == nil {
*mod = t
}
}
return resp.Body, nil
}
func fetchJSON(url string, mod *time.Time, v interface{}) error {
r, err := fetch(url, mod)
if err != nil {
return err
}
defer r.Close()
return json.NewDecoder(r).Decode(v)
}