From 16a8651571e7e489e1d22c5546db0153608bb48a Mon Sep 17 00:00:00 2001 From: siddharth ravikumar Date: Sun, 12 Jun 2022 19:41:06 -0400 Subject: weather: update NewWeather Change arguments to just `lat`, `lng`. Make this function call NWS directly. --- main.go | 12 ++---------- weather/weather.go | 29 +++++++++++++++++------------ 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 7cfe341..b9bd1b1 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,6 @@ import ( "strconv" "strings" - "ricketyspace.net/peach/nws" "ricketyspace.net/peach/photon" "ricketyspace.net/peach/version" "ricketyspace.net/peach/weather" @@ -93,17 +92,10 @@ func main() { } func showWeather(w http.ResponseWriter, lat, lng float32) { - forecastBundle, nwsErr := nws.GetForecastBundle(lat, lng) - if nwsErr != nil { - http.Error(w, nwsErr.Error(), nwsErr.Status) - } - // Make weather - weather, err := weather.NewWeather(forecastBundle.Point, - forecastBundle.Forecast, - forecastBundle.ForecastHourly) + weather, err, status := weather.NewWeather(lat, lng) if err != nil { - http.Error(w, err.Error(), 500) + http.Error(w, err.Error(), status) return } diff --git a/weather/weather.go b/weather/weather.go index 17e8884..9cc6ebf 100644 --- a/weather/weather.go +++ b/weather/weather.go @@ -41,32 +41,37 @@ type WeatherTimeline struct { Periods []WeatherPeriod } -func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) { +func NewWeather(lat, lng float32) (*Weather, error, int) { + fBundle, nwsErr := nws.GetForecastBundle(lat, lng) + if nwsErr != nil { + return nil, nwsErr, nwsErr.Status + } + w := new(Weather) w.Location = fmt.Sprintf("%s, %s", - strings.ToLower(point.Properties.RelativeLocation.Properties.City), - strings.ToLower(point.Properties.RelativeLocation.Properties.State), + strings.ToLower(fBundle.Point.Properties.RelativeLocation.Properties.City), + strings.ToLower(fBundle.Point.Properties.RelativeLocation.Properties.State), ) w.Title = w.Location w.Version = version.Version w.Now = WeatherNow{ - Temperature: fh.Properties.Periods[0].Temperature, - TemperatureUnit: fh.Properties.Periods[0].TemperatureUnit, - Forecast: fh.Properties.Periods[0].ShortForecast, - WindSpeed: fh.Properties.Periods[0].WindSpeed, - WindDirection: fh.Properties.Periods[0].WindDirection, + Temperature: fBundle.ForecastHourly.Properties.Periods[0].Temperature, + TemperatureUnit: fBundle.ForecastHourly.Properties.Periods[0].TemperatureUnit, + Forecast: fBundle.ForecastHourly.Properties.Periods[0].ShortForecast, + WindSpeed: fBundle.ForecastHourly.Properties.Periods[0].WindSpeed, + WindDirection: fBundle.ForecastHourly.Properties.Periods[0].WindDirection, } // Build Q2H timeline for the 12 hours. q2hPeriods := []WeatherPeriod{} max := 6 - for i, period := range fh.Properties.Periods { + for i, period := range fBundle.ForecastHourly.Properties.Periods { if i%2 != 0 { continue // Take every other period } t, err := time.Parse(time.RFC3339, period.StartTime) if err != nil { - return nil, err + return nil, err, 500 } p := WeatherPeriod{ Forecast: period.DetailedForecast, @@ -86,7 +91,7 @@ func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) { // Build BiDaily timeline for the next 3 days. bdPeriods := []WeatherPeriod{} max = 8 - for _, period := range f.Properties.Periods { + for _, period := range fBundle.Forecast.Properties.Periods { p := WeatherPeriod{ Name: period.Name, Forecast: period.DetailedForecast, @@ -102,5 +107,5 @@ func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) { Periods: bdPeriods, } - return w, nil + return w, nil, 200 } -- cgit v1.2.3