From 608ae1ef2ecc2fb915c43e42dd55ac6770267b65 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Sat, 20 May 2023 15:10:49 -0400 Subject: [PATCH] feat: update Open Weather Map to use Geocoding API --- src/http/oauth_test.go | 2 +- src/segments/owm.go | 30 ++++++++++++++++++++++++++---- src/segments/owm_test.go | 2 ++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/http/oauth_test.go b/src/http/oauth_test.go index b4dc1460..201007bb 100644 --- a/src/http/oauth_test.go +++ b/src/http/oauth_test.go @@ -161,7 +161,7 @@ func TestOauthResult(t *testing.T) { env.On("Cache").Return(cache) env.On("HTTPRequest", url).Return([]byte(tc.JSONResponse), tc.Error) env.On("HTTPRequest", tokenURL).Return([]byte(tc.TokenResponse), tc.Error) - env.On("Error", mock2.Anything).Return() + env.On("Error", mock2.Anything) oauth := &OAuthRequest{ AccessTokenKey: accessTokenKey, diff --git a/src/segments/owm.go b/src/segments/owm.go index ae4db4c7..7a2967c1 100644 --- a/src/segments/owm.go +++ b/src/segments/owm.go @@ -59,7 +59,13 @@ type geoLocation struct { func (d *Owm) Enabled() bool { err := d.setStatus() - return err == nil + + if err != nil { + d.env.Error(err) + return false + } + + return true } func (d *Owm) Template() string { @@ -85,23 +91,36 @@ func (d *Owm) getResult() (*owmDataResponse, error) { apikey := d.props.GetString(APIKey, ".") location := d.props.GetString(Location, "De Bilt,NL") - latitude := d.props.GetFloat64(Latitude, 91) - longitude := d.props.GetFloat64(Longitude, 181) + latitude := d.props.GetFloat64(Latitude, 91) // This default value is intentionally invalid since there should not be a default for this and 0 is a valid value + longitude := d.props.GetFloat64(Longitude, 181) // This default value is intentionally invalid since there should not be a default for this and 0 is a valid value units := d.props.GetString(Units, "standard") httpTimeout := d.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout) - if latitude > 90 || latitude < -90 || longitude > 180 && longitude < -180 { + validCoordinates := func(latitude, longitude float64) bool { + // Latitude values are only valid if they are between -90 and 90 + // Longitude values are only valid if they are between -180 and 180 + // https://gisgeography.com/latitude-longitude-coordinates/ + return latitude <= 90 && latitude >= -90 && longitude <= 180 && longitude >= -180 + } + + if !validCoordinates(latitude, longitude) { var geoResponse []geoLocation geocodingURL := fmt.Sprintf("http://api.openweathermap.org/geo/1.0/direct?q=%s&limit=1&appid=%s", location, apikey) + body, err := d.env.HTTPRequest(geocodingURL, nil, httpTimeout) if err != nil { return new(owmDataResponse), err } + err = json.Unmarshal(body, &geoResponse) if err != nil { return new(owmDataResponse), err } + if len(geoResponse) == 0 { + return new(owmDataResponse), fmt.Errorf("no coordinates found for %s", location) + } + latitude = geoResponse[0].Lat longitude = geoResponse[0].Lon } @@ -127,13 +146,16 @@ func (d *Owm) getResult() (*owmDataResponse, error) { func (d *Owm) setStatus() error { units := d.props.GetString(Units, "standard") + q, err := d.getResult() if err != nil { return err } + if len(q.Data) == 0 { return errors.New("No data found") } + id := q.Data[0].TypeID d.Temperature = int(math.Round(q.temperature.Value)) diff --git a/src/segments/owm_test.go b/src/segments/owm_test.go index 512a4d7f..fed85fd2 100644 --- a/src/segments/owm_test.go +++ b/src/segments/owm_test.go @@ -9,6 +9,7 @@ import ( "github.com/jandedobbeleer/oh-my-posh/src/properties" "github.com/stretchr/testify/assert" + mock2 "github.com/stretchr/testify/mock" ) const ( @@ -113,6 +114,7 @@ func TestOWMSegmentSingle(t *testing.T) { env.On("HTTPRequest", OWMGEOAPIURL).Return([]byte(tc.GeoJSONResponse), tc.Error) env.On("HTTPRequest", OWMWEATHERAPIURL).Return([]byte(tc.WeatherJSONResponse), tc.Error) + env.On("Error", mock2.Anything) o := &Owm{ props: props,