mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
refactor: inherit struct
This commit is contained in:
parent
17751107a8
commit
30ea89a45b
|
@ -8,11 +8,8 @@ import (
|
||||||
type nightscout struct {
|
type nightscout struct {
|
||||||
props *properties
|
props *properties
|
||||||
env environmentInfo
|
env environmentInfo
|
||||||
// array of nightscoutData (is often just 1, and we will pick the ZEROeth)
|
|
||||||
|
|
||||||
Sgv int64
|
|
||||||
Direction string
|
|
||||||
|
|
||||||
|
NightscoutData
|
||||||
TrendIcon string
|
TrendIcon string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +28,8 @@ const (
|
||||||
NSCacheTimeout Property = "cache_timeout"
|
NSCacheTimeout Property = "cache_timeout"
|
||||||
)
|
)
|
||||||
|
|
||||||
type nightscoutData struct {
|
// NightscoutData struct contains the API data
|
||||||
|
type NightscoutData struct {
|
||||||
Sgv int64 `json:"sgv"`
|
Sgv int64 `json:"sgv"`
|
||||||
Direction string `json:"direction"`
|
Direction string `json:"direction"`
|
||||||
}
|
}
|
||||||
|
@ -41,9 +39,7 @@ func (ns *nightscout) enabled() bool {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
ns.Sgv = data.Sgv
|
ns.NightscoutData = *data
|
||||||
ns.Direction = data.Direction
|
|
||||||
|
|
||||||
ns.TrendIcon = ns.getTrendIcon()
|
ns.TrendIcon = ns.getTrendIcon()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -85,11 +81,11 @@ func (ns *nightscout) string() string {
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ns *nightscout) getResult() (*nightscoutData, error) {
|
func (ns *nightscout) getResult() (*NightscoutData, error) {
|
||||||
url := ns.props.getString(URL, "")
|
url := ns.props.getString(URL, "")
|
||||||
// natural and understood NS timeout is 5, anything else is unusual
|
// natural and understood NS timeout is 5, anything else is unusual
|
||||||
cacheTimeout := ns.props.getInt(NSCacheTimeout, 5)
|
cacheTimeout := ns.props.getInt(NSCacheTimeout, 5)
|
||||||
response := &nightscoutData{}
|
response := &NightscoutData{}
|
||||||
if cacheTimeout > 0 {
|
if cacheTimeout > 0 {
|
||||||
// check if data stored in cache
|
// check if data stored in cache
|
||||||
val, found := ns.env.cache().get(url)
|
val, found := ns.env.cache().get(url)
|
||||||
|
@ -107,18 +103,18 @@ func (ns *nightscout) getResult() (*nightscoutData, error) {
|
||||||
|
|
||||||
body, err := ns.env.doGet(url, httpTimeout)
|
body, err := ns.env.doGet(url, httpTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &nightscoutData{}, err
|
return &NightscoutData{}, err
|
||||||
}
|
}
|
||||||
var arr []*nightscoutData
|
var arr []*NightscoutData
|
||||||
err = json.Unmarshal(body, &arr)
|
err = json.Unmarshal(body, &arr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &nightscoutData{}, err
|
return &NightscoutData{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
firstelement := arr[0]
|
firstelement := arr[0]
|
||||||
firstData, err := json.Marshal(firstelement)
|
firstData, err := json.Marshal(firstelement)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &nightscoutData{}, err
|
return &NightscoutData{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cacheTimeout > 0 {
|
if cacheTimeout > 0 {
|
||||||
|
|
Loading…
Reference in a new issue