-
Notifications
You must be signed in to change notification settings - Fork 1
/
riksbank.go
343 lines (317 loc) · 8.48 KB
/
riksbank.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package riksbank
import (
"context"
"strconv"
"time"
"github.com/zeeraw/riksbank/currency"
"github.com/zeeraw/riksbank/period"
"github.com/zeeraw/riksbank/swea"
)
// Config represents riksbank API configuration
type Config struct {
Client swea.Client
}
// Riksbank represents the API client
type Riksbank struct {
Client swea.Client
}
// New starts a new instance of riksbank
func New(config Config) *Riksbank {
client := config.Client
if client == nil {
client = swea.DefaultClient
}
return &Riksbank{
Client: client,
}
}
// RatesRequest represents parameters for requesting interest and exchange rates
type RatesRequest struct {
Series []string
From time.Time
To time.Time
AggregateMethod AggregateMethod
AnalysisMethod AnalysisMethod
}
// RatesResponse represents the data retrieved from requesting interest and exchange rates
type RatesResponse struct {
Rates Rates
}
// Rates return interest or change rates for one or more series between different dates
func (rb *Riksbank) Rates(ctx context.Context, req *RatesRequest) (*RatesResponse, error) {
const (
defaultGroupID = "3" // Set this to 3 until we're able to fetch series and groups
)
series := make([]swea.SearchGroupSeries, len(req.Series))
for idx, s := range req.Series {
series[idx] = swea.SearchGroupSeries{
SeriesID: s,
GroupID: defaultGroupID,
}
}
sweaReq := &swea.GetInterestAndExchangeRatesRequest{
From: req.From,
To: req.To,
AggregateMethod: req.AggregateMethod.String(),
Language: swea.English,
Series: series,
}
switch req.AnalysisMethod {
case Mean:
sweaReq.Average = true
case Min:
sweaReq.Min = true
case Max:
sweaReq.Max = true
case Ultimo:
sweaReq.Ultimo = true
}
sweaRes, err := rb.Client.GetInterestAndExchangeRates(ctx, sweaReq)
if err != nil {
return nil, err
}
rates := make(Rates, len(sweaRes.Rates))
for idx, ri := range sweaRes.Rates {
var value string
switch req.AnalysisMethod {
case Mean:
value = ri.Average
case Min:
value = ri.Min
case Max:
value = ri.Max
case Ultimo:
value = ri.Ultimo
default:
value = ri.Value
}
rates[idx] = Rate{
Date: ri.Date,
Period: period.Parse(ri.Period),
Group: RateGroup{
ID: ri.GroupID,
Name: ri.GroupName,
},
Series: RateSeries{
ID: ri.SeriesID,
Name: ri.SeriesName,
},
Value: parseFloat(value),
}
}
res := &RatesResponse{
Rates: rates,
}
return res, nil
}
// ExchangeRatesRequest represents parameters for requesting exchange rates
type ExchangeRatesRequest struct {
CurrencyPairs []currency.Pair
AggregateMethod AggregateMethod
From time.Time
To time.Time
}
// ExchangeRatesResponse represents the data retrieved from requesting exchange rates
type ExchangeRatesResponse struct {
ExchangeRates []ExchangeRate
}
// ExchangeRates retrieves exchange rates for currency pairs
func (rb *Riksbank) ExchangeRates(ctx context.Context, req *ExchangeRatesRequest) (*ExchangeRatesResponse, error) {
pairs := make([]swea.CrossPair, len(req.CurrencyPairs))
for idx, cp := range req.CurrencyPairs {
pairs[idx] = swea.CrossPair{
BaseSeriesID: cp.Base.Series(),
CounterSeriesID: cp.Counter.Series(),
}
}
sweaReq := &swea.GetCrossRatesRequest{
CrossPairs: pairs,
AggregateMethod: req.AggregateMethod.String(),
From: req.From,
To: req.To,
Language: swea.English,
}
sweaRes, err := rb.Client.GetCrossRates(ctx, sweaReq)
if err != nil {
return nil, err
}
exchangeRates := make(ExchangeRates, len(sweaRes.CrossRates))
for idx, cr := range sweaRes.CrossRates {
var value string
switch req.AggregateMethod {
case Daily:
value = cr.Value
default:
value = cr.Average
}
exchangeRates[idx] = ExchangeRate{
Date: cr.Date,
Period: period.Parse(cr.Period),
Base: currency.Parse(cr.Base),
Counter: currency.Parse(cr.Counter),
Value: parseFloat(value),
}
}
res := &ExchangeRatesResponse{
ExchangeRates: exchangeRates,
}
return res, nil
}
// ExchangeCurrenciesRequest represents parameters for requesting a list of all exchange currencies
type ExchangeCurrenciesRequest struct{}
// ExchangeCurrenciesResponse represents the data retrieved from requesting a list of all exchange currencies
type ExchangeCurrenciesResponse struct {
Currencies ExchangeCurrencies
}
// ExchangeCurrencies retrieves exchange rates for currency pairs
func (rb *Riksbank) ExchangeCurrencies(ctx context.Context, req *ExchangeCurrenciesRequest) (*ExchangeCurrenciesResponse, error) {
sweaReq := &swea.GetAllCrossNamesRequest{
Language: swea.English,
}
sweaRes, err := rb.Client.GetAllCrossNames(ctx, sweaReq)
if err != nil {
return nil, err
}
currencies := make(ExchangeCurrencies, len(sweaRes.Series))
for idx, s := range sweaRes.Series {
currencies[idx] = ExchangeCurrency{
SeriesID: s.ID,
Currency: currency.Parse(s.Name),
Description: s.Description[4:],
}
}
res := &ExchangeCurrenciesResponse{
Currencies: currencies,
}
return res, nil
}
// DaysRequest represents parameters for requesting information about days
type DaysRequest struct {
From time.Time
To time.Time
}
// DaysResponse represents the data retrieved from requesting information about days
type DaysResponse struct {
Days Days
}
// Days retrieves exchange rates for currency pairs
func (rb *Riksbank) Days(ctx context.Context, req *DaysRequest) (*DaysResponse, error) {
sweaReq := &swea.GetCalendarDaysRequest{
From: req.From,
To: req.To,
}
sweaRes, err := rb.Client.GetCalendarDays(ctx, sweaReq)
if err != nil {
return nil, err
}
days := make(Days, len(sweaRes.Days))
for idx, day := range sweaRes.Days {
days[idx] = Day{
Date: day.Date,
IsBankDay: day.IsBankDay,
Week: day.Week,
Year: day.WeekYear,
}
}
res := &DaysResponse{
Days: days,
}
return res, nil
}
// GroupsRequest represents parameters for requesting a list of groups
type GroupsRequest struct{}
// GroupsResponse represents the data retrieved from requesting a list of groups
type GroupsResponse struct {
Groups Groups
}
// Groups returns a list of all interest and exchange rate series groups
func (rb *Riksbank) Groups(ctx context.Context, req *GroupsRequest) (*GroupsResponse, error) {
sweaRes, err := rb.Client.GetInterestAndExchangeGroupNames(ctx, &swea.GetInterestAndExchangeGroupNamesRequest{
Language: swea.English,
})
if err != nil {
return nil, err
}
groups := make(Groups, len(sweaRes.Groups))
for idx, group := range sweaRes.Groups {
groups[idx] = sweaGroupToGroup(group)
}
res := &GroupsResponse{
Groups: groups,
}
return res, nil
}
// SeriesRequest represents parameters for requesting grouped series
type SeriesRequest struct {
Groups []string
}
// SeriesResponse represents the data retrieved from requesting grouped series
type SeriesResponse struct {
Groups SeriesGroups
}
// Series returns a list of series grouped by their group
func (rb *Riksbank) Series(ctx context.Context, req *SeriesRequest) (*SeriesResponse, error) {
sweaGRes, err := rb.Client.GetInterestAndExchangeGroupNames(ctx, &swea.GetInterestAndExchangeGroupNamesRequest{
Language: swea.English,
})
if err != nil {
return nil, err
}
groups := SeriesGroups{}
for _, g := range sweaGRes.Groups {
if len(req.Groups) < 1 || isInSlice(req.Groups, g.ID) {
sweaENReq := &swea.GetInterestAndExchangeNamesRequest{
Language: swea.English,
GroupID: g.ID,
}
sweaENRes, err := rb.Client.GetInterestAndExchangeNames(ctx, sweaENReq)
if err != nil {
return nil, err
}
series := make([]Series, len(sweaENRes.Series))
for idx, s := range sweaENRes.Series {
series[idx] = Series{
ID: s.ID,
GroupID: s.GroupID,
Name: s.Name,
Description: s.Description,
LongDescription: s.LongDescription,
Source: s.Source,
From: s.From,
To: s.To,
}
}
groups = append(groups, SeriesGroup{
Group: sweaGroupToGroup(g),
Series: series,
})
}
}
res := &SeriesResponse{
Groups: groups,
}
return res, nil
}
func parseFloat(s string) *float64 {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return nil
}
return &f
}
func isInSlice(items []string, item string) bool {
for _, i := range items {
if i == item {
return true
}
}
return false
}
func sweaGroupToGroup(g swea.GroupInfo) Group {
return Group{
ID: g.ID,
ParentID: g.ParentID,
Name: g.Name,
Description: g.Description,
}
}