forked from peterhellberg/fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixer.go
276 lines (244 loc) · 5.53 KB
/
fixer.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
/*
Package fixer contains a client for the
Foreign exchange rates and currency conversion API
Installation
go get -u github.com/peterhellberg/fixer
Usage
A small usage example
package main
import (
"context"
"flag"
"fmt"
"github.com/peterhellberg/fixer"
)
func main() {
f := flag.String("from", "EUR", "")
t := flag.String("to", "SEK", "")
n := flag.Float64("n", 1, "")
flag.Parse()
from, to := fixer.Currency(*f), fixer.Currency(*t)
resp, err := fixer.Latest(context.Background(),
fixer.Base(from), fixer.Symbols(to),
)
if err == nil {
fmt.Printf("%.2f %s equals %.2f %s\n", *n, from, resp.Rates[to]**n, to)
}
}
API Documentation
http://fixer.io/
*/
package fixer
import (
"sort"
"strconv"
"strings"
"time"
)
type Date time.Time
func (d *Date) UnmarshalJSON(data []byte) error {
str, err := strconv.Unquote(string(data))
if err != nil {
return err
}
t, err := time.Parse("2006-01-02", str)
if err != nil {
return err
}
*d = Date(t)
return nil
}
// Rates is the list of rates quoted against the base (EUR by default)
type Rates map[Currency]float64
// Links is a links object related to the primary data of the Response
type Links map[string]string
// Response data from the Foreign exchange rates and currency conversion API
type Response struct {
Base Currency `json:"base"`
Date Date `json:"date"`
Rates Rates `json:"rates"`
Links Links `json:"links,omitempty"`
}
// Currencies is a slice of Currency
type Currencies []Currency
func (cs Currencies) String() string {
symbols := []string{}
for _, c := range cs {
symbols = append(symbols, string(c))
}
sort.Strings(symbols)
return strings.Join(symbols, ",")
}
// Currency is the type used for ISO 4217 Currency codes
type Currency string
// Currency codes published by the European Central Bank
const (
AED Currency = "AED"
AFN Currency = "AFN"
ALL Currency = "ALL"
AMD Currency = "AMD"
ANG Currency = "ANG"
AOA Currency = "AOA"
ARS Currency = "ARS"
AUD Currency = "AUD"
AWG Currency = "AWG"
AZN Currency = "AZN"
BAM Currency = "BAM"
BBD Currency = "BBD"
BDT Currency = "BDT"
BGN Currency = "BGN"
BHD Currency = "BHD"
BIF Currency = "BIF"
BMD Currency = "BMD"
BND Currency = "BND"
BOB Currency = "BOB"
BRL Currency = "BRL"
BSD Currency = "BSD"
BTC Currency = "BTC"
BTN Currency = "BTN"
BWP Currency = "BWP"
BYN Currency = "BYN"
BYR Currency = "BYR"
BZD Currency = "BZD"
CAD Currency = "CAD"
CDF Currency = "CDF"
CHF Currency = "CHF"
CLF Currency = "CLF"
CLP Currency = "CLP"
CNY Currency = "CNY"
COP Currency = "COP"
CRC Currency = "CRC"
CUC Currency = "CUC"
CUP Currency = "CUP"
CVE Currency = "CVE"
CZK Currency = "CZK"
DJF Currency = "DJF"
DKK Currency = "DKK"
DOP Currency = "DOP"
DZD Currency = "DZD"
EGP Currency = "EGP"
ERN Currency = "ERN"
ETB Currency = "ETB"
EUR Currency = "EUR"
FJD Currency = "FJD"
FKP Currency = "FKP"
GBP Currency = "GBP"
GEL Currency = "GEL"
GGP Currency = "GGP"
GHS Currency = "GHS"
GIP Currency = "GIP"
GMD Currency = "GMD"
GNF Currency = "GNF"
GTQ Currency = "GTQ"
GYD Currency = "GYD"
HKD Currency = "HKD"
HNL Currency = "HNL"
HRK Currency = "HRK"
HTG Currency = "HTG"
HUF Currency = "HUF"
IDR Currency = "IDR"
ILS Currency = "ILS"
IMP Currency = "IMP"
INR Currency = "INR"
IQD Currency = "IQD"
IRR Currency = "IRR"
ISK Currency = "ISK"
JEP Currency = "JEP"
JMD Currency = "JMD"
JOD Currency = "JOD"
JPY Currency = "JPY"
KES Currency = "KES"
KGS Currency = "KGS"
KHR Currency = "KHR"
KMF Currency = "KMF"
KPW Currency = "KPW"
KRW Currency = "KRW"
KWD Currency = "KWD"
KYD Currency = "KYD"
KZT Currency = "KZT"
LAK Currency = "LAK"
LBP Currency = "LBP"
LKR Currency = "LKR"
LRD Currency = "LRD"
LSL Currency = "LSL"
LTL Currency = "LTL"
LVL Currency = "LVL"
LYD Currency = "LYD"
MAD Currency = "MAD"
MDL Currency = "MDL"
MGA Currency = "MGA"
MKD Currency = "MKD"
MMK Currency = "MMK"
MNT Currency = "MNT"
MOP Currency = "MOP"
MRO Currency = "MRO"
MUR Currency = "MUR"
MVR Currency = "MVR"
MWK Currency = "MWK"
MXN Currency = "MXN"
MYR Currency = "MYR"
MZN Currency = "MZN"
NAD Currency = "NAD"
NGN Currency = "NGN"
NIO Currency = "NIO"
NOK Currency = "NOK"
NPR Currency = "NPR"
NZD Currency = "NZD"
OMR Currency = "OMR"
PAB Currency = "PAB"
PEN Currency = "PEN"
PGK Currency = "PGK"
PHP Currency = "PHP"
PKR Currency = "PKR"
PLN Currency = "PLN"
PYG Currency = "PYG"
QAR Currency = "QAR"
RON Currency = "RON"
RSD Currency = "RSD"
RUB Currency = "RUB"
RWF Currency = "RWF"
SAR Currency = "SAR"
SBD Currency = "SBD"
SCR Currency = "SCR"
SDG Currency = "SDG"
SEK Currency = "SEK"
SGD Currency = "SGD"
SHP Currency = "SHP"
SLL Currency = "SLL"
SOS Currency = "SOS"
SRD Currency = "SRD"
STD Currency = "STD"
SVC Currency = "SVC"
SYP Currency = "SYP"
SZL Currency = "SZL"
THB Currency = "THB"
TJS Currency = "TJS"
TMT Currency = "TMT"
TND Currency = "TND"
TOP Currency = "TOP"
TRY Currency = "TRY"
TTD Currency = "TTD"
TWD Currency = "TWD"
TZS Currency = "TZS"
UAH Currency = "UAH"
UGX Currency = "UGX"
USD Currency = "USD"
UYU Currency = "UYU"
UZS Currency = "UZS"
VEF Currency = "VEF"
VND Currency = "VND"
VUV Currency = "VUV"
WST Currency = "WST"
XAF Currency = "XAF"
XAG Currency = "XAG"
XAU Currency = "XAU"
XCD Currency = "XCD"
XDR Currency = "XDR"
XOF Currency = "XOF"
XPF Currency = "XPF"
YER Currency = "YER"
ZAR Currency = "ZAR"
ZMK Currency = "ZMK"
ZMW Currency = "ZMW"
ZWL Currency = "ZWL"
)