1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/exchangerates/exchangerates.go
Chris Cummer cde904ff08
Use errcheck to find unhandled errors (#795)
Signed-off-by: Chris Cummer <chriscummer@me.com>
2019-12-17 08:26:16 -08:00

43 lines
826 B
Go

package exchangerates
import (
"fmt"
"net/http"
"github.com/wtfutil/wtf/utils"
)
type Response struct {
Base string `json:"base"`
Rates map[string]float64 `json:"rates"`
}
func FetchExchangeRates(settings *Settings) (map[string]map[string]float64, error) {
out := map[string]map[string]float64{}
for base, rates := range settings.rates {
resp, err := http.Get(fmt.Sprintf("https://api.exchangeratesapi.io/latest?base=%s", base))
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var data Response
err = utils.ParseJSON(&data, resp.Body)
if err != nil {
return nil, err
}
out[base] = map[string]float64{}
for _, currency := range rates {
rate, ok := data.Rates[currency]
if ok {
out[base][currency] = rate
}
}
}
return out, nil
}