-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecbratex.go
123 lines (101 loc) · 3.32 KB
/
ecbratex.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Package ecbratex provides a convenient interface and useful data structures for fetching
// and manipulating currency exchange rate records from the European Central Bank (https://ecb.europa.eu).
package ecbratex
import (
"errors"
"github.com/jieggii/ecbratex/pkg/provider"
"github.com/jieggii/ecbratex/pkg/record"
"github.com/jieggii/ecbratex/pkg/timeseries"
"github.com/jieggii/ecbratex/pkg/xml"
)
// Provider is [provider.Provider] which will be used to fetch rates data across the ecbratex.
// provider.HTTPProvider with URLs to the ECB website is used by default.
var Provider provider.Provider = provider.NewHTTPProvider(
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml",
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml",
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml",
)
// SetProvider sets data provider which will be used across the ecbratex to fetch exchange rates records.
func SetProvider(p provider.Provider) {
Provider = p
}
var ErrUnexpectedPeriod = errors.New("unexpected period")
type Period uint8
const (
PeriodWhole Period = iota
PeriodLast90Days
)
// DataKind returns matching [provider.DataKind] for this period.
func (p Period) DataKind() (provider.DataKind, error) {
switch p {
case PeriodWhole:
return provider.DataKindTimeSeries, nil
case PeriodLast90Days:
return provider.DataKindTimeSeriesLast90Days, nil
default:
return 0, ErrUnexpectedPeriod
}
}
// FetchLatest fetches latest available exchange rates using Provider.
func FetchLatest() (*record.WithDate, error) {
rawData, err := Provider.GetRatesData(provider.DataKindLatest)
if err != nil {
return nil, err
}
xmlData, err := xml.NewData(rawData)
if err != nil {
return nil, err
}
return record.NewWithDateFromXMLData(xmlData)
}
// FetchTimeSeries fetches rate records within the given period using Provider.
// Returns records represented as timeseries.RecordsMap.
func FetchTimeSeries(period Period) (timeseries.UnorderedRecords, error) {
dataKind, err := period.DataKind()
if err != nil {
return nil, err
}
rawData, err := Provider.GetRatesData(dataKind)
if err != nil {
return nil, err
}
xmlData, err := xml.NewData(rawData)
if err != nil {
return nil, err
}
return timeseries.NewUnorderedRecordsFromXML(xmlData)
}
// FetchOrderedTimeSeries fetches rate records within the given period using Provider.
// Returns records represented as timeseries.RecordsSlice.
func FetchOrderedTimeSeries(period Period) (timeseries.OrderedRecords, error) {
dataKind, err := period.DataKind()
if err != nil {
return nil, err
}
rawData, err := Provider.GetRatesData(dataKind)
if err != nil {
return nil, err
}
xmlData, err := xml.NewData(rawData)
if err != nil {
return nil, err
}
return timeseries.NewOrderedRecordsFromXML(xmlData)
}
// FetchOrderedUnorderedTimeSeries fetches rate records within the given period using Provider.
// Returns records represented as timeseries.RecordsSliceMap.
func FetchOrderedUnorderedTimeSeries(period Period) (*timeseries.OrderedUnorderedRecords, error) {
dataKind, err := period.DataKind()
if err != nil {
return nil, err
}
rawData, err := Provider.GetRatesData(dataKind)
if err != nil {
return nil, err
}
xmlData, err := xml.NewData(rawData)
if err != nil {
return nil, err
}
return timeseries.NewOrderedUnorderedRecordsFromXML(xmlData)
}