From 041c5d4d2d7c89badd87bfcc08eacff2fb97b592 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Thu, 30 May 2024 14:12:12 -0400 Subject: [PATCH] fix: owm allow location city name with spaces - Escape the location parameter of the owm segment to allow for city names with spaces to function correctly. - Update the owm api url to use https. --- src/segments/owm.go | 5 ++++- src/segments/owm_test.go | 17 ++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/segments/owm.go b/src/segments/owm.go index e614ee03..0d4d51e2 100644 --- a/src/segments/owm.go +++ b/src/segments/owm.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math" + "net/url" "github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/properties" @@ -89,6 +90,8 @@ func (d *Owm) getResult() (*owmDataResponse, error) { location := d.props.GetString(Location, "De Bilt,NL") + location = url.QueryEscape(location) + if len(apikey) == 0 || len(location) == 0 { return nil, errors.New("no api key or location found") } @@ -96,7 +99,7 @@ func (d *Owm) getResult() (*owmDataResponse, error) { units := d.props.GetString(Units, "standard") httpTimeout := d.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout) - d.URL = fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", location, units, apikey) + d.URL = fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", location, units, apikey) body, err := d.env.HTTPRequest(d.URL, nil, httpTimeout) if err != nil { diff --git a/src/segments/owm_test.go b/src/segments/owm_test.go index 01a96051..42ccb492 100644 --- a/src/segments/owm_test.go +++ b/src/segments/owm_test.go @@ -3,6 +3,7 @@ package segments import ( "errors" "fmt" + "net/url" "testing" "github.com/jandedobbeleer/oh-my-posh/src/mock" @@ -13,7 +14,7 @@ import ( ) const ( - OWMWEATHERAPIURL = "http://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=key" + OWMWEATHERAPIURL = "https://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=key" ) func TestOWMSegmentSingle(t *testing.T) { @@ -79,8 +80,9 @@ func TestOWMSegmentSingle(t *testing.T) { properties.CacheTimeout: 0, } - url := fmt.Sprintf(OWMWEATHERAPIURL, tc.Location) - env.On("HTTPRequest", url).Return([]byte(tc.WeatherJSONResponse), tc.Error) + location := url.QueryEscape(tc.Location) + testURL := fmt.Sprintf(OWMWEATHERAPIURL, location) + env.On("HTTPRequest", testURL).Return([]byte(tc.WeatherJSONResponse), tc.Error) env.On("Error", mock2.Anything) o := &Owm{ @@ -200,7 +202,8 @@ func TestOWMSegmentIcons(t *testing.T) { }, } - url := fmt.Sprintf(OWMWEATHERAPIURL, "AMSTERDAM,NL") + location := url.QueryEscape("AMSTERDAM,NL") + testURL := fmt.Sprintf(OWMWEATHERAPIURL, location) for _, tc := range cases { env := &mock.MockedEnvironment{} @@ -208,7 +211,7 @@ func TestOWMSegmentIcons(t *testing.T) { weatherResponse := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20.3}}`, tc.IconID) expectedString := fmt.Sprintf("%s (20°C)", tc.ExpectedIconString) - env.On("HTTPRequest", url).Return([]byte(weatherResponse), nil) + env.On("HTTPRequest", testURL).Return([]byte(weatherResponse), nil) o := &Owm{ props: properties.Map{ @@ -229,9 +232,9 @@ func TestOWMSegmentIcons(t *testing.T) { env := &mock.MockedEnvironment{} weatherResponse := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20.3}}`, tc.IconID) - expectedString := fmt.Sprintf("«%s (20°C)»(%s)", tc.ExpectedIconString, url) + expectedString := fmt.Sprintf("«%s (20°C)»(%s)", tc.ExpectedIconString, testURL) - env.On("HTTPRequest", url).Return([]byte(weatherResponse), nil) + env.On("HTTPRequest", testURL).Return([]byte(weatherResponse), nil) o := &Owm{ props: properties.Map{