diff options
author | siddharth <s@ricketyspace.net> | 2022-05-16 21:48:43 -0400 |
---|---|---|
committer | siddharth <s@ricketyspace.net> | 2022-05-16 21:48:43 -0400 |
commit | 63a35560a1566b9f8ca8ddd2b436003b520c6876 (patch) | |
tree | 07dda36ba86763717223b28075b6494baafd436e /nws/nws.go | |
parent | cbc777005e2d344151ea5c1984a33ff7b262b79a (diff) |
nws: add ForecastHourly
Diffstat (limited to 'nws/nws.go')
-rw-r--r-- | nws/nws.go | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -139,3 +139,46 @@ func Forecast(point *NWSPoint) (*NWSForecast, error) { } return forecast, nil } + +// NWS forecast hourly endpoint. +func ForecastHourly(point *NWSPoint) (*NWSForecast, error) { + if point == nil { + return nil, fmt.Errorf("forecast hourly: point nil") + } + if len(point.Properties.ForecastHourly) == 0 { + return nil, fmt.Errorf("forecast hourly: link empty") + } + + // Get the forecast + resp, err := client.Get(point.Properties.ForecastHourly) + if err != nil { + return nil, fmt.Errorf("forecast hourly: get: %v", err) + } + + // Parse response body. + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("forecast hourly: body: %v", err) + } + + // Check if the request failed. + if resp.StatusCode != 200 { + perr := new(NWSError) + err := json.Unmarshal(body, perr) + if err != nil { + return nil, fmt.Errorf("forecast hourly: json: %v", err) + } + return nil, fmt.Errorf("forecast: %v", perr) + } + + // Unmarshal. + forecast := new(NWSForecast) + err = json.Unmarshal(body, forecast) + if err != nil { + return nil, fmt.Errorf("forecast hourly: decode: %v", err) + } + if len(forecast.Properties.Periods) == 0 { + return nil, fmt.Errorf("forecast hourly: periods empty") + } + return forecast, nil +} |