From 63a35560a1566b9f8ca8ddd2b436003b520c6876 Mon Sep 17 00:00:00 2001 From: siddharth Date: Mon, 16 May 2022 21:48:43 -0400 Subject: nws: add ForecastHourly --- nws/nws.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'nws/nws.go') diff --git a/nws/nws.go b/nws/nws.go index b939d2a..aebfed8 100644 --- a/nws/nws.go +++ b/nws/nws.go @@ -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 +} -- cgit v1.2.3