summaryrefslogtreecommitdiffstats
path: root/nws/nws.go
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2022-05-16 21:48:43 -0400
committersiddharth <s@ricketyspace.net>2022-05-16 21:48:43 -0400
commit63a35560a1566b9f8ca8ddd2b436003b520c6876 (patch)
tree07dda36ba86763717223b28075b6494baafd436e /nws/nws.go
parentcbc777005e2d344151ea5c1984a33ff7b262b79a (diff)
nws: add ForecastHourly
Diffstat (limited to 'nws/nws.go')
-rw-r--r--nws/nws.go43
1 files changed, 43 insertions, 0 deletions
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
+}