Skip to content

Commit

Permalink
feat: update Open Weather Map to use Geocoding API
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Miller authored and JanDeDobbeleer committed May 21, 2023
1 parent ed8d89a commit 608ae1e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/http/oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 26 additions & 4 deletions src/segments/owm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions src/segments/owm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 608ae1e

Please sign in to comment.