-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
502 lines (406 loc) · 13 KB
/
table.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// complete tables of currency exchange rates published by the NBP.PL service
//
// public func: NewTable
// types: NBPTable, ExchangeTable, ExchangeTableC
// NBPTable methods:
// TableRaw, TableByDate, TableLast
// GetTableCurrent, GetTableCCurrent, GetTableToday, GetTableCToday, GetTableByDay, GetTableCByDay
// GetPrettyOutput, GetCSVOutput, GetRawOutput
package nbpapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"text/tabwriter"
"time"
)
// NewTable - function creates new table type
func NewTable(tableType string) *NBPTable {
cli := &http.Client{
Timeout: time.Second * 10,
}
r := &NBPTable{
tableType: tableType,
Client: cli,
}
return r
}
/*
TableRaw - function downloads data in json or xml form, the data will be
placed as [] byte in the result field
Function returns error or nil
Parameters:
date - date in the format: 'YYYY-MM-DD' (ISO 8601 standard),
or a range of dates in the format: 'YYYY-MM-DD:YYYY-MM-DD' or 'today'
(rate for today) or 'current' - current table / rate (last published)
last - as an alternative to date, the last <n> tables/rates
can be retrieved
format - 'json' or 'xml'
*/
func (t *NBPTable) TableRaw(date string, last int, format string) error {
var err error
url := getTableAddress(t.tableType, date, last)
t.result, err = t.getData(url, format)
if err != nil {
return err
}
return err
}
/*
TableByDate - function downloads and writes data to NBPTable.Exchange
(NBPTable.ExchangeC) slice, raw data (json) still available in
NBPTable.result field
Function returns error or nil
Parameters:
date - date in the format: 'YYYY-MM-DD' (ISO 8601 standard),
or a range of dates in the format: 'YYYY-MM-DD:YYYY-MM-DD' or 'today'
(rate for today) or 'current' - current table / rate (last published)
*/
func (t *NBPTable) TableByDate(date string) error {
var err error
url := getTableAddress(t.tableType, date, 0)
t.result, err = t.getData(url, "json")
if err != nil {
return err
}
if t.tableType != "C" {
err = json.Unmarshal(t.result, &t.Exchange)
} else {
err = json.Unmarshal(t.result, &t.ExchangeC)
}
if err != nil {
return err
}
return err
}
/*
TableLast - function downloads and writes data to NBPTable.Exchange
(NBPTable.ExchangeC) slice, raw data (json) still available in
NBPTable.result field
Function returns error or nil
Parameters:
last - the last <n> tables/rates can be retrieved
*/
func (t *NBPTable) TableLast(last int) error {
var err error
url := getTableAddress(t.tableType, "", last)
t.result, err = t.getData(url, "json")
if err != nil {
return err
}
if t.tableType != "C" {
err = json.Unmarshal(t.result, &t.Exchange)
} else {
err = json.Unmarshal(t.result, &t.ExchangeC)
}
if err != nil {
return err
}
return err
}
/*
GetTableCurrent - function downloads current table of currency exchange
rates and return slice of struct ExchangeTable (or error), version for
table A, B (mid - average price)
*/
func (t *NBPTable) GetTableCurrent() ([]ExchangeTable, error) {
var err error
if t.tableType == "C" {
return nil, errors.New("Invalid function call context, use GetTableCCurrent")
}
url := getTableAddress(t.tableType, "current", 0)
t.result, err = t.getData(url, "json")
if err != nil {
return nil, err
}
err = json.Unmarshal(t.result, &t.Exchange)
if err != nil {
return nil, err
}
return t.Exchange, err
}
/*
GetTableCCurrent - function downloads current table of currency exchange
rates and return slice of struct ExchangeTableC (or error), version for
table C (bid, ask - buy, sell prices)
*/
func (t *NBPTable) GetTableCCurrent() ([]ExchangeTableC, error) {
var err error
if t.tableType != "C" {
return nil, errors.New("Invalid function call context, use GetTableCurrent")
}
url := getTableAddress(t.tableType, "current", 0)
t.result, err = t.getData(url, "json")
if err != nil {
return nil, err
}
err = json.Unmarshal(t.result, &t.ExchangeC)
if err != nil {
return nil, err
}
return t.ExchangeC, err
}
/*
GetTableByDate - function retrieves a table of exchange rates for a given day
and return slice of struct ExchangeTable (or error). Version for
table A, B (mid - average price)
Parameters:
date - date in the format: 'YYYY-MM-DD' (ISO 8601 standard),
or a range of dates in the format: 'YYYY-MM-DD:YYYY-MM-DD' or 'today'
(rate for today) or 'current' - current table / rate (last published)
*/
func (t *NBPTable) GetTableByDate(date string) ([]ExchangeTable, error) {
var err error
if t.tableType == "C" {
return nil, errors.New("Invalid function call context, use GetTableCByDate")
}
url := getTableAddress(t.tableType, date, 0)
t.result, err = t.getData(url, "json")
if err != nil {
return nil, err
}
err = json.Unmarshal(t.result, &t.Exchange)
if err != nil {
return nil, err
}
return t.Exchange, err
}
/*
GetTableCByDate - function retrieves a table of exchange rates for a given day
and return slice of struct ExchangeTable (or error). Version for
table C (ask, bid - buy, sell prices)
Parameters:
date - date in the format: 'YYYY-MM-DD' (ISO 8601 standard),
or a range of dates in the format: 'YYYY-MM-DD:YYYY-MM-DD' or 'today'
(rate for today) or 'current' - current table / rate (last published)
*/
func (t *NBPTable) GetTableCByDate(date string) ([]ExchangeTableC, error) {
var err error
if t.tableType != "C" {
return nil, errors.New("Invalid function call context, use GetTableByDate")
}
url := getTableAddress(t.tableType, date, 0)
t.result, err = t.getData(url, "json")
if err != nil {
return nil, err
}
err = json.Unmarshal(t.result, &t.ExchangeC)
if err != nil {
return nil, err
}
return t.ExchangeC, err
}
/*
GetTableToday - function downloads today's table of currency exchange
rates and return slice of struct ExchangeTable (or error), version for
table A, B (mid - average price)
*/
func (t *NBPTable) GetTableToday() ([]ExchangeTable, error) {
var err error
if t.tableType == "C" {
return nil, errors.New("Invalid function call context, use GetTableCToday")
}
url := getTableAddress(t.tableType, "today", 0)
t.result, err = t.getData(url, "json")
if err != nil {
return nil, err
}
err = json.Unmarshal(t.result, &t.Exchange)
if err != nil {
return nil, err
}
return t.Exchange, err
}
/*
GetTableCToday - function downloads today's table of currency exchange
rates and return slice of struct ExchangeTableC (or error), version for
table C (bid, ask - buy, sell prices)
*/
func (t *NBPTable) GetTableCToday() ([]ExchangeTableC, error) {
var err error
if t.tableType != "C" {
return nil, errors.New("Invalid function call context, use GetTableToday")
}
url := getTableAddress(t.tableType, "today", 0)
t.result, err = t.getData(url, "json")
if err != nil {
return nil, err
}
err = json.Unmarshal(t.result, &t.ExchangeC)
if err != nil {
return nil, err
}
return t.ExchangeC, err
}
/*
CreatePrettyOutput - function returns tables of exchange rates as
formatted table, depending on the tableType field: for type A and B tables
a column with an average rate is printed, for type C two columns:
buy price and sell price
Parameters:
lang - 'en' or 'pl'
*/
func (t *NBPTable) CreatePrettyOutput(lang string) string {
const padding = 3
var builder strings.Builder
var output string
// output language
setLang(lang)
w := tabwriter.NewWriter(&builder, 0, 0, padding, ' ', tabwriter.Debug)
if t.tableType != "C" {
for _, item := range t.Exchange {
output += fmt.Sprintln()
output += fmt.Sprintln(l.Get("Table type:")+"\t\t", item.Table)
output += fmt.Sprintln(l.Get("Table number:")+"\t\t", item.No)
output += fmt.Sprintln(l.Get("Publication date:")+"\t", item.EffectiveDate)
output += fmt.Sprintln()
fmt.Fprintln(w, l.Get("CODE \t NAME \t AVERAGE (PLN)"))
fmt.Fprintln(w, l.Get("---- \t ---- \t -------------"))
for _, currencyItem := range item.Rates {
currencyValue := fmt.Sprintf("%.4f", currencyItem.Mid)
fmt.Fprintln(w, currencyItem.Code+" \t "+currencyItem.Currency+" \t "+currencyValue)
}
w.Flush()
output += builder.String()
builder.Reset()
}
} else {
for _, item := range t.ExchangeC {
output += fmt.Sprintln()
output += fmt.Sprintln(l.Get("Table type:")+"\t\t", item.Table)
output += fmt.Sprintln(l.Get("Table number:")+"\t\t", item.No)
output += fmt.Sprintln(l.Get("Trading date:")+"\t\t", item.TradingDate)
output += fmt.Sprintln(l.Get("Publication date:")+"\t", item.EffectiveDate)
output += fmt.Sprintln()
fmt.Fprintln(w, l.Get("CODE \t NAME \t BUY (PLN) \t SELL (PLN) "))
fmt.Fprintln(w, l.Get("---- \t ---- \t --------- \t ---------- "))
for _, currencyItem := range item.Rates {
currencyValueBid := fmt.Sprintf("%.4f", currencyItem.Bid)
currencyValueAsk := fmt.Sprintf("%.4f", currencyItem.Ask)
fmt.Fprintln(w, currencyItem.Code+" \t "+currencyItem.Currency+" \t "+currencyValueBid+" \t "+currencyValueAsk)
}
w.Flush()
output += builder.String()
builder.Reset()
}
}
return output
}
/*
CreateCSVOutput - function prints tables of exchange rates in the console,
in the form of CSV (data separated by a comma), depending on the
tableType field: for type A and B tables a column with an average
rate is printed, for type C two columns: buy price and sell price
Parameters:
lang - 'en' or 'pl'
*/
func (t *NBPTable) CreateCSVOutput(lang string) string {
var tableNo string
var output string = ""
// output language
setLang(lang)
if t.tableType != "C" {
output += fmt.Sprintln(l.Get("TABLE,CODE,NAME,AVERAGE (PLN)"))
for _, item := range t.Exchange {
tableNo = item.No
for _, currencyItem := range item.Rates {
currencyValue := fmt.Sprintf("%.4f", currencyItem.Mid)
output += fmt.Sprintln(tableNo + "," + currencyItem.Code + "," + currencyItem.Currency + "," + currencyValue)
}
}
} else {
output += fmt.Sprintln(l.Get("TABLE,CODE,NAME,BUY (PLN),SELL (PLN)"))
for _, item := range t.ExchangeC {
tableNo = item.No
for _, currencyItem := range item.Rates {
currencyValueBid := fmt.Sprintf("%.4f", currencyItem.Bid)
currencyValueAsk := fmt.Sprintf("%.4f", currencyItem.Ask)
output += fmt.Sprintln(tableNo + "," + currencyItem.Code + "," + currencyItem.Currency + "," + currencyValueBid + "," + currencyValueAsk)
}
}
}
return output
}
// CreateRawOutput - function returns just result of request (json or xml)
func (t *NBPTable) CreateRawOutput() string {
return string(t.result)
}
// SetTableType - the function allows to set the supported type of exchange rate table
func (t *NBPTable) SetTableType(tableType string) error {
if t.tableType == tableType {
return nil
}
err := checkTableType(tableType)
if err != nil {
return err
}
// data cleaning
t.clearData()
t.tableType = tableType
return nil
}
/* getData - function that retrieves data from the NBP website
and returns them in the form of JSON / XML (or error), based on
the arguments provided:
url - NBP web api address
format - 'json' or 'xml'
*/
func (t *NBPTable) getData(url, format string) ([]byte, error) {
t.clearData()
return fetchData(t.Client, url, format)
}
// clearData - data cleaning
func (t *NBPTable) clearData() {
t.result = nil
t.Exchange = nil
t.ExchangeC = nil
}
// --------------------- Private func ---------------------------------
// getTableAddress - build download address depending on previously
// verified input parameters (--table, --date or --last)
func getTableAddress(tableType, date string, last int) string {
var address string
if last != 0 {
address = queryTableLast(tableType, strconv.Itoa(last))
} else if date == "today" {
address = queryTableToday(tableType)
} else if date == "current" {
address = queryTableCurrent(tableType)
} else if len(date) == 10 {
address = queryTableDate(tableType, date)
} else if len(date) == 21 {
address = queryTableRange(tableType, date)
}
return address
}
// queryTableToday - returns query: exchange rate table published today
func queryTableToday(tableType string) string {
return fmt.Sprintf("%s/tables/%s/today/", baseAddressTable, tableType)
}
// queryTableCurrent - returns query: current table of exchange rates
// (last published table)
func queryTableCurrent(tableType string) string {
return fmt.Sprintf("%s/tables/%s/", baseAddressTable, tableType)
}
// queryTableDay - returns query: table of exchange rates
// on the given date (YYYY-MM-DD)
func queryTableDate(tableType, date string) string {
return fmt.Sprintf("%s/tables/%s/%s/", baseAddressTable, tableType, date)
}
// queryTableRange - returns query: table of exchange rates within
// the given date range (RRRR-MM-DD:RRRR-MM-DD)
func queryTableRange(tableType, date string) string {
var startDate, stopDate string
temp := strings.Split(date, ":")
startDate = temp[0]
stopDate = temp[1]
address := fmt.Sprintf("%s/tables/%s/%s/%s/", baseAddressTable, tableType, startDate, stopDate)
return address
}
// queryTableLast - returns query: last <number> tables of exchange rates
func queryTableLast(tableType, last string) string {
return fmt.Sprintf("%s/tables/%s/last/%s/", baseAddressTable, tableType, last)
}