-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecb.go
41 lines (35 loc) · 856 Bytes
/
ecb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"encoding/xml"
"net/http"
"time"
)
type ecbRates struct {
Rates []struct {
Currency string `xml:"currency,attr"`
Rate string `xml:"rate,attr"`
} `xml:"Cube>Cube>Cube"`
}
const ecbDailyUrl = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
// Get daily conversion rates from ECB
func fetchEcbDaily() (map[string]string, error) {
req, err := http.NewRequest("GET", ecbDailyUrl, nil)
if err != nil {
return nil, err
}
cl := &http.Client{Timeout: 15 * time.Second}
resp, err := cl.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var rates ecbRates
if err := xml.NewDecoder(resp.Body).Decode(&rates); err != nil {
return nil, err
}
mappedRates := make(map[string]string)
for _, r := range rates.Rates {
mappedRates[r.Currency] = r.Rate
}
return mappedRates, nil
}