mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
fix(owm): cache also the url used to get the data
The url is used after do generate the hyperlink
This commit is contained in:
parent
57174b6424
commit
95f776a8ed
|
@ -23,8 +23,10 @@ const (
|
|||
Units Property = "units"
|
||||
// CacheTimeout cache timeout
|
||||
CacheTimeout Property = "cache_timeout"
|
||||
// CacheKey key used when caching the response
|
||||
CacheKey string = "owm_response"
|
||||
// CacheKeyResponse key used when caching the response
|
||||
CacheKeyResponse string = "owm_response"
|
||||
// CacheKeyUrl key used when caching the url responsible for the response
|
||||
CacheKeyURL string = "owm_url"
|
||||
)
|
||||
|
||||
type weather struct {
|
||||
|
@ -70,13 +72,14 @@ func (d *owm) getResult() (*OWMDataResponse, error) {
|
|||
response := new(OWMDataResponse)
|
||||
if cacheTimeout > 0 {
|
||||
// check if data stored in cache
|
||||
val, found := d.env.cache().get(CacheKey)
|
||||
val, found := d.env.cache().get(CacheKeyResponse)
|
||||
// we got something from te cache
|
||||
if found {
|
||||
err := json.Unmarshal([]byte(val), response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d.url, _ = d.env.cache().get(CacheKeyURL)
|
||||
return response, nil
|
||||
}
|
||||
}
|
||||
|
@ -98,7 +101,8 @@ func (d *owm) getResult() (*OWMDataResponse, error) {
|
|||
|
||||
if cacheTimeout > 0 {
|
||||
// persist new forecasts in cache
|
||||
d.env.cache().set(CacheKey, string(body), cacheTimeout)
|
||||
d.env.cache().set(CacheKeyResponse, string(body), cacheTimeout)
|
||||
d.env.cache().set(CacheKeyURL, d.url, cacheTimeout)
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
|
|
@ -185,6 +185,33 @@ func TestOWMSegmentIcons(t *testing.T) {
|
|||
assert.Nil(t, o.setStatus())
|
||||
assert.Equal(t, expectedString, o.string(), tc.Case)
|
||||
}
|
||||
|
||||
// test with hyperlink enabled
|
||||
for _, tc := range cases {
|
||||
env := &MockedEnvironment{}
|
||||
props := &properties{
|
||||
values: map[Property]interface{}{
|
||||
APIKey: "key",
|
||||
Location: "AMSTERDAM,NL",
|
||||
Units: "metric",
|
||||
CacheTimeout: 0,
|
||||
EnableHyperlink: true,
|
||||
},
|
||||
}
|
||||
|
||||
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, tc.IconID)
|
||||
expectedString := fmt.Sprintf("[%s (20°C)](http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", tc.ExpectedIconString)
|
||||
|
||||
env.On("doGet", OWMAPIURL).Return([]byte(response), nil)
|
||||
|
||||
o := &owm{
|
||||
props: props,
|
||||
env: env,
|
||||
}
|
||||
|
||||
assert.Nil(t, o.setStatus())
|
||||
assert.Equal(t, expectedString, o.string(), tc.Case)
|
||||
}
|
||||
}
|
||||
func TestOWMSegmentFromCache(t *testing.T) {
|
||||
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d")
|
||||
|
@ -204,9 +231,36 @@ func TestOWMSegmentFromCache(t *testing.T) {
|
|||
env: env,
|
||||
}
|
||||
cache.On("get", "owm_response").Return(response, true)
|
||||
cache.On("get", "owm_url").Return("http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key", true)
|
||||
cache.On("set").Return()
|
||||
env.On("cache", nil).Return(cache)
|
||||
|
||||
assert.Nil(t, o.setStatus())
|
||||
assert.Equal(t, expectedString, o.string())
|
||||
}
|
||||
|
||||
func TestOWMSegmentFromCacheWithHyperlink(t *testing.T) {
|
||||
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d")
|
||||
expectedString := fmt.Sprintf("[%s (20°C)](http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", "\ufa98")
|
||||
|
||||
env := &MockedEnvironment{}
|
||||
cache := &MockedCache{}
|
||||
props := &properties{
|
||||
values: map[Property]interface{}{
|
||||
APIKey: "key",
|
||||
Location: "AMSTERDAM,NL",
|
||||
Units: "metric",
|
||||
EnableHyperlink: true,
|
||||
},
|
||||
}
|
||||
o := &owm{
|
||||
props: props,
|
||||
env: env,
|
||||
}
|
||||
cache.On("get", "owm_response").Return(response, true)
|
||||
cache.On("get", "owm_url").Return("http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key", true)
|
||||
cache.On("set").Return()
|
||||
env.On("cache", nil).Return(cache)
|
||||
// env.On("doGet", "http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key").Return([]byte(response), nil)
|
||||
|
||||
assert.Nil(t, o.setStatus())
|
||||
assert.Equal(t, expectedString, o.string())
|
||||
|
|
Loading…
Reference in a new issue