forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchangeratesapi.go
281 lines (243 loc) · 8.47 KB
/
exchangeratesapi.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
package exchangerates
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/base"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/log"
)
var errAPIKeyNotSet = errors.New("API key must be set")
// Setup sets appropriate values for CurrencyLayer
func (e *ExchangeRates) Setup(config base.Settings) error {
if config.APIKey == "" {
return errAPIKeyNotSet
}
e.Name = config.Name
e.Enabled = config.Enabled
e.RESTPollingDelay = config.RESTPollingDelay
e.Verbose = config.Verbose
e.PrimaryProvider = config.PrimaryProvider
e.APIKey = config.APIKey
e.APIKeyLvl = config.APIKeyLvl
e.Requester = request.New(e.Name,
common.NewHTTPClientWithTimeout(base.DefaultTimeOut),
request.WithLimiter(request.NewBasicRateLimit(rateLimitInterval, requestRate)))
return nil
}
func (e *ExchangeRates) cleanCurrencies(baseCurrency, symbols string) string {
if len(e.supportedCurrencies) == 0 {
supportedCurrencies, err := e.GetSupportedCurrencies()
if err != nil {
log.Warnf(log.Global, "ExchangeRatesAPI unable to fetch supported currencies: %s", err)
} else {
e.supportedCurrencies = supportedCurrencies
}
}
var cleanedCurrencies []string
symbols = strings.Replace(symbols, "RUR", "RUB", -1)
var s = strings.Split(symbols, ",")
for _, x := range s {
// first make sure that the baseCurrency is not in the symbols list
// if it is set
if baseCurrency != "" {
if x == baseCurrency {
continue
}
} else {
// otherwise since the baseCurrency is empty, make sure that it
// does not exist in the symbols list
if x == "EUR" {
continue
}
}
// remove and warn about any unsupported currencies
if len(e.supportedCurrencies) > 0 {
if !strings.Contains(strings.Join(e.supportedCurrencies, ","), x) {
log.Warnf(log.Global,
"Forex provider ExchangeRatesAPI does not support currency %s, removing from forex rates query.\n", x)
continue
}
}
cleanedCurrencies = append(cleanedCurrencies, x)
}
return strings.Join(cleanedCurrencies, ",")
}
// GetSymbols returns a list of supported symbols
func (e *ExchangeRates) GetSymbols() (map[string]string, error) {
resp := struct {
Symbols map[string]string `json:"symbols"`
}{}
return resp.Symbols, e.SendHTTPRequest("symbols", url.Values{}, &resp)
}
// GetLatestRates returns a map of forex rates based on the supplied params
// baseCurrency - USD [optional] The base currency to use for forex rates, defaults to EUR
// symbols - AUD,USD [optional] The symbols to query the forex rates for, default is
// all supported currencies
func (e *ExchangeRates) GetLatestRates(baseCurrency, symbols string) (*Rates, error) {
vals := url.Values{}
if len(baseCurrency) > 0 && e.APIKeyLvl <= apiKeyFree && !strings.EqualFold("EUR", baseCurrency) {
return nil, errCannotSetBaseCurrencyOnFreePlan
} else if len(baseCurrency) > 0 {
vals.Set("base", baseCurrency)
}
if len(symbols) > 0 {
symbols = e.cleanCurrencies(baseCurrency, symbols)
vals.Set("symbols", symbols)
}
var result Rates
return &result, e.SendHTTPRequest(exchangeRatesLatest, vals, &result)
}
// GetHistoricalRates returns historical exchange rate data for all available or
// a specific set of currencies.
// date - YYYY-MM-DD [required] A date in the past
// baseCurrency - USD [optional] The base currency to use for forex rates, defaults to EUR
// symbols - AUD,USD [optional] The symbols to query the forex rates for, default is
// all supported currencies
func (e *ExchangeRates) GetHistoricalRates(date time.Time, baseCurrency string, symbols []string) (*HistoricalRates, error) {
if date.IsZero() {
return nil, errors.New("a date must be specified")
}
var resp HistoricalRates
v := url.Values{}
if len(baseCurrency) > 0 && e.APIKeyLvl <= apiKeyFree && !strings.EqualFold("EUR", baseCurrency) {
return nil, errCannotSetBaseCurrencyOnFreePlan
} else if len(baseCurrency) > 0 {
v.Set("base", baseCurrency)
}
if len(symbols) > 0 {
s := e.cleanCurrencies(baseCurrency, strings.Join(symbols, ","))
v.Set("symbols", s)
}
return &resp, e.SendHTTPRequest(date.UTC().Format(timeLayout), v, &resp)
}
// ConvertCurrency converts a currency based on the supplied params
func (e *ExchangeRates) ConvertCurrency(from, to string, amount float64, date time.Time) (*ConvertCurrency, error) {
if e.APIKeyLvl <= apiKeyFree {
return nil, errAPIKeyLevelRestrictedAccess
}
vals := url.Values{}
if from == "" || to == "" || amount == 0 {
return nil, errors.New("from, to and amount must be set")
}
vals.Set("from", from)
vals.Set("to", to)
vals.Set("amount", strconv.FormatFloat(amount, 'e', -1, 64))
if !date.IsZero() {
vals.Set("date", date.UTC().Format(timeLayout))
}
var cc ConvertCurrency
return &cc, e.SendHTTPRequest(exchangeRatesConvert, vals, &cc)
}
// GetTimeSeriesRates returns daily historical exchange rate data between two
// specified dates for all available or a specific set of currencies.
// startDate - YYYY-MM-DD [required] A date in the past
// endDate - YYYY-MM-DD [required] A date in the past but greater than the startDate
// baseCurrency - USD [optional] The base currency to use for forex rates, defaults to EUR
// symbols - AUD,USD [optional] The symbols to query the forex rates for, default is
// all supported currencies
func (e *ExchangeRates) GetTimeSeriesRates(startDate, endDate time.Time, baseCurrency string, symbols []string) (*TimeSeriesRates, error) {
if e.APIKeyLvl <= apiKeyFree {
return nil, errAPIKeyLevelRestrictedAccess
}
if startDate.IsZero() || endDate.IsZero() {
return nil, errStartEndDatesInvalid
}
if startDate.After(endDate) {
return nil, errStartAfterEnd
}
v := url.Values{}
v.Set("start_date", startDate.UTC().Format(timeLayout))
v.Set("end_date", endDate.UTC().Format(timeLayout))
if baseCurrency != "" {
v.Set("base", baseCurrency)
}
if len(symbols) > 0 {
s := e.cleanCurrencies(baseCurrency, strings.Join(symbols, ","))
v.Set("symbols", s)
}
var resp TimeSeriesRates
return &resp, e.SendHTTPRequest(exchangeRatesTimeSeries, v, &resp)
}
// GetFluctuations returns rate fluctuations based on the supplied params
func (e *ExchangeRates) GetFluctuations(startDate, endDate time.Time, baseCurrency, symbols string) (*Fluctuations, error) {
if e.APIKeyLvl <= apiKeyFree {
return nil, errAPIKeyLevelRestrictedAccess
}
if startDate.IsZero() || endDate.IsZero() {
return nil, errStartEndDatesInvalid
}
if startDate.After(endDate) {
return nil, errStartAfterEnd
}
v := url.Values{}
v.Set("start_date", startDate.UTC().Format(timeLayout))
v.Set("end_date", endDate.UTC().Format(timeLayout))
if baseCurrency != "" {
v.Set("base", baseCurrency)
}
if symbols != "" {
v.Set("symbols", symbols)
}
var f Fluctuations
return &f, e.SendHTTPRequest(exchangeRatesFluctuation, v, &f)
}
// GetRates is a wrapper function to return forex rates
func (e *ExchangeRates) GetRates(baseCurrency, symbols string) (map[string]float64, error) {
result, err := e.GetLatestRates(baseCurrency, symbols)
if err != nil {
return nil, err
}
standardisedRates := make(map[string]float64)
for k, v := range result.Rates {
curr := baseCurrency + k
standardisedRates[curr] = v
}
return standardisedRates, nil
}
// GetSupportedCurrencies returns the supported currency list
func (e *ExchangeRates) GetSupportedCurrencies() ([]string, error) {
symbols, err := e.GetSymbols()
if err != nil {
return nil, err
}
var supportedCurrencies []string
for x := range symbols {
supportedCurrencies = append(supportedCurrencies, x)
}
e.supportedCurrencies = supportedCurrencies
return supportedCurrencies, nil
}
// SendHTTPRequest sends a HTTPS request to the desired endpoint and returns the result
func (e *ExchangeRates) SendHTTPRequest(endPoint string, values url.Values, result interface{}) error {
if e.APIKey == "" {
return errors.New("api key must be set")
}
values.Set("access_key", e.APIKey)
protocolScheme := "https://"
if e.APIKeyLvl == apiKeyFree {
protocolScheme = "http://"
}
path := common.EncodeURLValues(protocolScheme+exchangeRatesAPI+"/v1/"+endPoint, values)
item := &request.Item{
Method: http.MethodGet,
Path: path,
Result: result,
Verbose: e.Verbose,
}
err := e.Requester.SendPayload(context.TODO(), request.Unset, func() (*request.Item, error) {
return item, nil
})
if err != nil {
return fmt.Errorf("exchangeRatesAPI: SendHTTPRequest error %s with path %s",
err,
path)
}
return nil
}