forked from icodealot/noaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
56 lines (47 loc) · 1.36 KB
/
http.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
53
54
55
56
package noaa
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
// Make an HTTP GET request to the provided endpoint and then attempts
// to decode the HTTP response into the provided reference. The caller
// must ensure that the type reference provided matches the JSON
// returned by the provided endpoint uri
func decode(endpoint string, v any) error {
res, err := get(endpoint)
if err != nil {
return err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
if err = decoder.Decode(v); err != nil {
return err
}
return nil
}
// HTTP GET the noaa endpoint provided. We could just use http.Get() but
// this helps since we include some custom header values
func get(endpoint string) (res *http.Response, err error) {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", config.Accept)
req.Header.Add("User-Agent", config.UserAgent)
// enable quantitative values in forecast responses
req.Header.Add("feature-flags", "forecast_temperature_qv, forecast_wind_speed_qv")
// lazy-init client to http.DefaultClient.
if config.Client == nil {
config.Client = http.DefaultClient
}
res, err = config.Client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("%d %s", res.StatusCode, res.Status))
}
return res, nil
}