refactor(owm): rename properties

This commit is contained in:
Jan De Dobbeleer 2021-09-24 07:36:21 +02:00 committed by Jan De Dobbeleer
parent aa5a235953
commit 57174b6424
2 changed files with 37 additions and 41 deletions

View file

@ -15,16 +15,16 @@ type owm struct {
} }
const ( const (
// APIKEY openweathermap api key // APIKey openweathermap api key
APIKEY Property = "apikey" APIKey Property = "apikey"
// LOCATION openweathermap location // Location openweathermap location
LOCATION Property = "location" Location Property = "location"
// UNITS openweathermap units // Units openweathermap units
UNITS Property = "units" Units Property = "units"
// CACHETimeout cache timeout // CacheTimeout cache timeout
CACHETimeout Property = "cache_timeout" CacheTimeout Property = "cache_timeout"
// CACHEKey key used when caching the response // CacheKey key used when caching the response
CACHEKey string = "owm_response" CacheKey string = "owm_response"
) )
type weather struct { type weather struct {
@ -66,50 +66,45 @@ func (d *owm) string() string {
} }
func (d *owm) getResult() (*OWMDataResponse, error) { func (d *owm) getResult() (*OWMDataResponse, error) {
apikey := d.props.getString(APIKEY, ".") cacheTimeout := int64(d.props.getInt(CacheTimeout, DefaultCacheTimeout))
location := d.props.getString(LOCATION, "De Bilt,NL") response := new(OWMDataResponse)
units := d.props.getString(UNITS, "standard")
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 // check if data stored in cache
val, found := d.env.cache().get(CACHEKey) val, found := d.env.cache().get(CacheKey)
// we got something from te cache // we got something from te cache
if found { if found {
q := new(OWMDataResponse) err := json.Unmarshal([]byte(val), response)
err := json.Unmarshal([]byte(val), q)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return q, nil return response, nil
} }
} }
apikey := d.props.getString(APIKey, ".")
location := d.props.getString(Location, "De Bilt,NL")
units := d.props.getString(Units, "standard")
httpTimeout := d.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
d.url = fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", location, units, apikey)
body, err := d.env.doGet(d.url, httpTimeout) body, err := d.env.doGet(d.url, httpTimeout)
if err != nil { if err != nil {
return new(OWMDataResponse), err return new(OWMDataResponse), err
} }
q := new(OWMDataResponse) err = json.Unmarshal(body, &response)
err = json.Unmarshal(body, &q)
if err != nil { if err != nil {
return new(OWMDataResponse), err return new(OWMDataResponse), err
} }
if cacheTimeout > 0 { if cacheTimeout > 0 {
// persist new forecasts in cache // persist new forecasts in cache
d.env.cache().set(CACHEKey, string(body), cacheTimeout) d.env.cache().set(CacheKey, string(body), cacheTimeout)
if err != nil {
return nil, err
}
} }
return q, nil return response, nil
} }
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 {

View file

@ -38,10 +38,10 @@ func TestOWMSegmentSingle(t *testing.T) {
env := &MockedEnvironment{} env := &MockedEnvironment{}
props := &properties{ props := &properties{
values: map[Property]interface{}{ values: map[Property]interface{}{
APIKEY: "key", APIKey: "key",
LOCATION: "AMSTERDAM,NL", Location: "AMSTERDAM,NL",
UNITS: "metric", Units: "metric",
CACHETimeout: 0, CacheTimeout: 0,
}, },
} }
@ -165,10 +165,10 @@ func TestOWMSegmentIcons(t *testing.T) {
env := &MockedEnvironment{} env := &MockedEnvironment{}
props := &properties{ props := &properties{
values: map[Property]interface{}{ values: map[Property]interface{}{
APIKEY: "key", APIKey: "key",
LOCATION: "AMSTERDAM,NL", Location: "AMSTERDAM,NL",
UNITS: "metric", Units: "metric",
CACHETimeout: 0, CacheTimeout: 0,
}, },
} }
@ -194,9 +194,9 @@ func TestOWMSegmentFromCache(t *testing.T) {
cache := &MockedCache{} cache := &MockedCache{}
props := &properties{ props := &properties{
values: map[Property]interface{}{ values: map[Property]interface{}{
APIKEY: "key", APIKey: "key",
LOCATION: "AMSTERDAM,NL", Location: "AMSTERDAM,NL",
UNITS: "metric", Units: "metric",
}, },
} }
o := &owm{ o := &owm{
@ -206,6 +206,7 @@ func TestOWMSegmentFromCache(t *testing.T) {
cache.On("get", "owm_response").Return(response, true) cache.On("get", "owm_response").Return(response, true)
cache.On("set").Return() cache.On("set").Return()
env.On("cache", nil).Return(cache) 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.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string()) assert.Equal(t, expectedString, o.string())