Skip to content

Commit

Permalink
fix: cache_timeout mismatch + variable naming + schema.json
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu authored and JanDeDobbeleer committed Sep 24, 2021
1 parent 6097d2d commit ba71a4a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/config.go
Expand Up @@ -44,6 +44,8 @@ const (
HTTPTimeout Property = "http_timeout"
// DefaultHTTPTimeout default timeout used when executing http request
DefaultHTTPTimeout = 20
// DefaultCacheTimeout default timeout used when caching data
DefaultCacheTimeout = 10
)

func printConfigError(err error, eval bool) {
Expand Down
20 changes: 11 additions & 9 deletions src/segment_owm.go
Expand Up @@ -21,8 +21,10 @@ const (
LOCATION Property = "location"
// UNITS openweathermap units
UNITS Property = "units"
// CACHETIMEOUT cache timeout
CACHETIMEOUT Property = "cachetimeout"
// CACHETimeout cache timeout
CACHETimeout Property = "cache_timeout"
// CACHEKey key used when caching the response
CACHEKey string = "owm_response"
)

type weather struct {
Expand Down Expand Up @@ -67,14 +69,14 @@ func (d *owm) getResult() (*OWMDataResponse, error) {
apikey := d.props.getString(APIKEY, ".")
location := d.props.getString(LOCATION, "De Bilt,NL")
units := d.props.getString(UNITS, "standard")
timeout := d.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
cachetimeout := int64(d.props.getInt(CACHETIMEOUT, 10))
httpTimeout := d.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
cacheTimeout := int64(d.props.getInt(CACHETimeout, DefaultCacheTimeout))

d.url = fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", location, units, apikey)

if cachetimeout > 0 {
if cacheTimeout > 0 {
// check if data stored in cache
val, found := d.env.cache().get("owm_response")
val, found := d.env.cache().get(CACHEKey)
// we got something from te cache
if found {
q := new(OWMDataResponse)
Expand All @@ -86,7 +88,7 @@ func (d *owm) getResult() (*OWMDataResponse, error) {
}
}

body, err := d.env.doGet(d.url, timeout)
body, err := d.env.doGet(d.url, httpTimeout)
if err != nil {
return new(OWMDataResponse), err
}
Expand All @@ -96,9 +98,9 @@ func (d *owm) getResult() (*OWMDataResponse, error) {
return new(OWMDataResponse), err
}

if cachetimeout > 0 {
if cacheTimeout > 0 {
// persist new forecasts in cache
d.env.cache().set("owm_response", string(body), cachetimeout)
d.env.cache().set(CACHEKey, string(body), cacheTimeout)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions src/segment_owm_test.go
Expand Up @@ -41,7 +41,7 @@ func TestOWMSegmentSingle(t *testing.T) {
APIKEY: "key",
LOCATION: "AMSTERDAM,NL",
UNITS: "metric",
CACHETIMEOUT: 0,
CACHETimeout: 0,
},
}

Expand Down Expand Up @@ -168,7 +168,7 @@ func TestOWMSegmentIcons(t *testing.T) {
APIKEY: "key",
LOCATION: "AMSTERDAM,NL",
UNITS: "metric",
CACHETIMEOUT: 0,
CACHETimeout: 0,
},
}

Expand Down
12 changes: 6 additions & 6 deletions themes/schema.json
Expand Up @@ -1547,12 +1547,6 @@
},
"http_timeout": {
"$ref": "#/definitions/http_timeout"
},
"cache_timeout": {
"type": "integer",
"title": "cache timeout",
"description": "The number of minutes the response is cached. A value of 0 disables the cache.",
"default": 10
}
}
}
Expand Down Expand Up @@ -1595,6 +1589,12 @@
},
"http_timeout": {
"$ref": "#/definitions/http_timeout"
},
"cache_timeout": {
"type": "integer",
"title": "cache timeout",
"description": "The number of minutes the response is cached. A value of 0 disables the cache.",
"default": 10
}
}
}
Expand Down

0 comments on commit ba71a4a

Please sign in to comment.