mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: update Open Weather Map to use Geocoding API
This commit is contained in:
parent
ed8d89a7cc
commit
608ae1ef2e
|
@ -161,7 +161,7 @@ func TestOauthResult(t *testing.T) {
|
||||||
env.On("Cache").Return(cache)
|
env.On("Cache").Return(cache)
|
||||||
env.On("HTTPRequest", url).Return([]byte(tc.JSONResponse), tc.Error)
|
env.On("HTTPRequest", url).Return([]byte(tc.JSONResponse), tc.Error)
|
||||||
env.On("HTTPRequest", tokenURL).Return([]byte(tc.TokenResponse), 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{
|
oauth := &OAuthRequest{
|
||||||
AccessTokenKey: accessTokenKey,
|
AccessTokenKey: accessTokenKey,
|
||||||
|
|
|
@ -59,7 +59,13 @@ type geoLocation struct {
|
||||||
|
|
||||||
func (d *Owm) Enabled() bool {
|
func (d *Owm) Enabled() bool {
|
||||||
err := d.setStatus()
|
err := d.setStatus()
|
||||||
return err == nil
|
|
||||||
|
if err != nil {
|
||||||
|
d.env.Error(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Owm) Template() string {
|
func (d *Owm) Template() string {
|
||||||
|
@ -85,23 +91,36 @@ func (d *Owm) getResult() (*owmDataResponse, error) {
|
||||||
|
|
||||||
apikey := d.props.GetString(APIKey, ".")
|
apikey := d.props.GetString(APIKey, ".")
|
||||||
location := d.props.GetString(Location, "De Bilt,NL")
|
location := d.props.GetString(Location, "De Bilt,NL")
|
||||||
latitude := d.props.GetFloat64(Latitude, 91)
|
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)
|
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")
|
units := d.props.GetString(Units, "standard")
|
||||||
httpTimeout := d.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)
|
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
|
var geoResponse []geoLocation
|
||||||
geocodingURL := fmt.Sprintf("http://api.openweathermap.org/geo/1.0/direct?q=%s&limit=1&appid=%s", location, apikey)
|
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)
|
body, err := d.env.HTTPRequest(geocodingURL, nil, httpTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return new(owmDataResponse), err
|
return new(owmDataResponse), err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &geoResponse)
|
err = json.Unmarshal(body, &geoResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return new(owmDataResponse), err
|
return new(owmDataResponse), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(geoResponse) == 0 {
|
||||||
|
return new(owmDataResponse), fmt.Errorf("no coordinates found for %s", location)
|
||||||
|
}
|
||||||
|
|
||||||
latitude = geoResponse[0].Lat
|
latitude = geoResponse[0].Lat
|
||||||
longitude = geoResponse[0].Lon
|
longitude = geoResponse[0].Lon
|
||||||
}
|
}
|
||||||
|
@ -127,13 +146,16 @@ func (d *Owm) getResult() (*owmDataResponse, error) {
|
||||||
|
|
||||||
func (d *Owm) setStatus() error {
|
func (d *Owm) setStatus() error {
|
||||||
units := d.props.GetString(Units, "standard")
|
units := d.props.GetString(Units, "standard")
|
||||||
|
|
||||||
q, err := d.getResult()
|
q, err := d.getResult()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(q.Data) == 0 {
|
if len(q.Data) == 0 {
|
||||||
return errors.New("No data found")
|
return errors.New("No data found")
|
||||||
}
|
}
|
||||||
|
|
||||||
id := q.Data[0].TypeID
|
id := q.Data[0].TypeID
|
||||||
|
|
||||||
d.Temperature = int(math.Round(q.temperature.Value))
|
d.Temperature = int(math.Round(q.temperature.Value))
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
mock2 "github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -113,6 +114,7 @@ func TestOWMSegmentSingle(t *testing.T) {
|
||||||
|
|
||||||
env.On("HTTPRequest", OWMGEOAPIURL).Return([]byte(tc.GeoJSONResponse), tc.Error)
|
env.On("HTTPRequest", OWMGEOAPIURL).Return([]byte(tc.GeoJSONResponse), tc.Error)
|
||||||
env.On("HTTPRequest", OWMWEATHERAPIURL).Return([]byte(tc.WeatherJSONResponse), tc.Error)
|
env.On("HTTPRequest", OWMWEATHERAPIURL).Return([]byte(tc.WeatherJSONResponse), tc.Error)
|
||||||
|
env.On("Error", mock2.Anything)
|
||||||
|
|
||||||
o := &Owm{
|
o := &Owm{
|
||||||
props: props,
|
props: props,
|
||||||
|
|
Loading…
Reference in a new issue