summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nws/nws.go43
-rw-r--r--nws/nws_test.go47
2 files changed, 90 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
+}
diff --git a/nws/nws_test.go b/nws/nws_test.go
index ea47188..7dd5780 100644
--- a/nws/nws_test.go
+++ b/nws/nws_test.go
@@ -85,3 +85,50 @@ func TestForecast(t *testing.T) {
}
}
}
+
+func TestForecastHourly(t *testing.T) {
+ // Get point.
+ np, err := Points(41.115, -83.177)
+ if err != nil {
+ t.Errorf("error: %v", err)
+ return
+ }
+
+ // Get forecast hourly.
+ fc, err := ForecastHourly(np)
+ if err != nil {
+ t.Errorf("error: %v", err)
+ return
+ }
+
+ // Verify periods.
+ for i, period := range fc.Properties.Periods {
+ if period.Number < 1 {
+ t.Errorf("period: %d: number invalid: %v", i, period.Number)
+ }
+ if len(period.StartTime) < 1 {
+ t.Errorf("period: %d: start time invalid: %v", i,
+ period.StartTime)
+ }
+ if len(period.EndTime) < 1 {
+ t.Errorf("period: %d: end time invalid: %v", i,
+ period.EndTime)
+ }
+ if len(period.TemperatureUnit) < 1 {
+ t.Errorf("period: %d: temperature unit invalid: %v",
+ i, period.TemperatureUnit)
+ }
+ if len(period.WindSpeed) < 1 {
+ t.Errorf("period: %d: wind speed invalid: %v",
+ i, period.WindSpeed)
+ }
+ if len(period.WindDirection) < 1 {
+ t.Errorf("period: %d: wind direction invalid: %v",
+ i, period.WindDirection)
+ }
+ if len(period.ShortForecast) < 1 {
+ t.Errorf("period: %d: short forecast invalid: %v",
+ i, period.ShortForecast)
+ }
+ }
+}