-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
368 lines (290 loc) · 9.13 KB
/
client.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
package oxr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
)
const (
timeFormat = "2006-01-02"
basePath = "https://openexchangerates.org/api/"
)
var (
ErrBadResponse = errors.New("failed to receive successful response")
)
// Doer sends a http.Request and returns a http.Response.
type Doer interface {
Do(r *http.Request) (*http.Response, error)
}
// Client is responsible for all interactions between OXR.
type Client struct {
appID string
doer Doer
baseURL string
}
// New instantiates a Client.
func New(opts ...ClientOption) Client {
c := Client{
baseURL: basePath,
}
for _, opt := range opts {
opt(&c)
}
return c
}
// Latest retrieves the latest exchange rates available from the Open Exchange Rates API.
// https://docs.openexchangerates.org/docs/latest-json
func (c Client) Latest(ctx context.Context, opts ...LatestOption) (LatestRatesResponse, error) {
r := latestParams{}
for _, opt := range opts {
opt(&r)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%slatest.json", c.baseURL), http.NoBody)
if err != nil {
return LatestRatesResponse{}, err
}
v := req.URL.Query()
v.Add("app_id", c.appID)
v.Add("prettyprint", strconv.FormatBool(r.prettyPrint))
if r.baseCurrency != "" {
v.Add("base", r.baseCurrency)
}
if r.destinationCurrencies != "" {
v.Add("symbols", r.destinationCurrencies)
}
v.Add("show_alternative", strconv.FormatBool(r.showAlternative))
req.URL.RawQuery = v.Encode()
res, err := c.doer.Do(req)
defer res.Body.Close()
if err != nil {
return LatestRatesResponse{}, err
}
if res.StatusCode != http.StatusOK {
return LatestRatesResponse{}, fmt.Errorf("status received: %v: %w", res.StatusCode, ErrBadResponse)
}
var resData LatestRatesResponse
err = json.NewDecoder(res.Body).Decode(&resData)
if err != nil {
return LatestRatesResponse{}, err
}
return resData, nil
}
// Historical retrieves historical exchange rates for any date available from the Open Exchange Rates API, currently
// going back to 1st January 1999.
// https://docs.openexchangerates.org/docs/historical-json
func (c Client) Historical(ctx context.Context, opts ...HistoricalOption) (HistoricalRatesResponse, error) {
r := historicalParams{}
for _, opt := range opts {
opt(&r)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
fmt.Sprintf("%shistorical/%s.json", c.baseURL, r.date.Format(timeFormat)), http.NoBody)
if err != nil {
return HistoricalRatesResponse{}, err
}
v := req.URL.Query()
v.Add("app_id", c.appID)
v.Add("prettyprint", strconv.FormatBool(r.prettyPrint))
v.Add("show_alternative", strconv.FormatBool(r.showAlternative))
if r.baseCurrency != "" {
v.Add("base", r.baseCurrency)
}
if r.destinationCurrencies != "" {
v.Add("symbols", r.destinationCurrencies)
}
req.URL.RawQuery = v.Encode()
res, err := c.doer.Do(req)
defer res.Body.Close()
if err != nil {
return HistoricalRatesResponse{}, err
}
if res.StatusCode != http.StatusOK {
return HistoricalRatesResponse{}, fmt.Errorf("status received: %v: %w", res.StatusCode, ErrBadResponse)
}
var resData HistoricalRatesResponse
err = json.NewDecoder(res.Body).Decode(&resData)
if err != nil {
return HistoricalRatesResponse{}, err
}
return resData, nil
}
// Currencies retrieves the list of all currency symbols available from the Open Exchange Rates API, along with their
// full names.
// https://docs.openexchangerates.org/docs/currencies-json
func (c Client) Currencies(ctx context.Context, opts ...CurrenciesOption) (CurrenciesResponse, error) {
r := currenciesParams{}
for _, opt := range opts {
opt(&r)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
fmt.Sprintf("%scurrencies.json", c.baseURL), http.NoBody)
if err != nil {
return CurrenciesResponse{}, err
}
v := req.URL.Query()
v.Add("app_id", c.appID)
v.Add("show_inactive", strconv.FormatBool(r.showInactive))
v.Add("prettyprint", strconv.FormatBool(r.prettyPrint))
v.Add("show_alternative", strconv.FormatBool(r.showAlternative))
req.URL.RawQuery = v.Encode()
res, err := c.doer.Do(req)
defer res.Body.Close()
if err != nil {
return CurrenciesResponse{}, err
}
if res.StatusCode != http.StatusOK {
return CurrenciesResponse{}, fmt.Errorf("status received: %v: %w", res.StatusCode, ErrBadResponse)
}
var resData CurrenciesResponse
err = json.NewDecoder(res.Body).Decode(&resData.Currencies)
if err != nil {
return CurrenciesResponse{}, err
}
return resData, nil
}
// TimeSeries retrieves historical exchange rates for a given time period, where available, using the time series / bulk
// download API endpoint.
// https://docs.openexchangerates.org/docs/time-series-json
func (c Client) TimeSeries(ctx context.Context, opts ...TimeSeriesOption) (TimeSeriesResponse, error) {
r := timeSeriesParams{}
for _, opt := range opts {
opt(&r)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
fmt.Sprintf("%stime-series.json", c.baseURL), http.NoBody)
if err != nil {
return TimeSeriesResponse{}, err
}
v := req.URL.Query()
v.Add("app_id", c.appID)
v.Add("prettyprint", strconv.FormatBool(r.prettyPrint))
v.Add("show_alternative", strconv.FormatBool(r.showAlternative))
v.Add("start", r.startDate.Format(timeFormat))
v.Add("end", r.endDate.Format(timeFormat))
if r.destinationCurrencies != "" {
v.Add("symbols", r.destinationCurrencies)
}
if r.baseCurrency != "" {
v.Add("base", r.baseCurrency)
}
req.URL.RawQuery = v.Encode()
res, err := c.doer.Do(req)
defer res.Body.Close()
if err != nil {
return TimeSeriesResponse{}, err
}
if res.StatusCode != http.StatusOK {
return TimeSeriesResponse{}, fmt.Errorf("status received: %v: %w", res.StatusCode, ErrBadResponse)
}
var resData TimeSeriesResponse
err = json.NewDecoder(res.Body).Decode(&resData)
if err != nil {
return TimeSeriesResponse{}, err
}
return resData, nil
}
// Convert any money value from one currency to another at the latest API rates.
// https://docs.openexchangerates.org/docs/convert
func (c Client) Convert(ctx context.Context, opts ...ConvertOption) (ConversionResponse, error) {
r := convertParams{}
for _, opt := range opts {
opt(&r)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
fmt.Sprintf("%sconvert/%v/%s/%s", c.baseURL, r.value, r.baseCurrency, r.destinationCurrency), http.NoBody)
if err != nil {
return ConversionResponse{}, err
}
v := req.URL.Query()
v.Add("app_id", c.appID)
v.Add("prettyprint", strconv.FormatBool(r.prettyPrint))
req.URL.RawQuery = v.Encode()
res, err := c.doer.Do(req)
defer res.Body.Close()
if err != nil {
return ConversionResponse{}, err
}
if res.StatusCode != http.StatusOK {
return ConversionResponse{}, fmt.Errorf("status received: %v: %w", res.StatusCode, ErrBadResponse)
}
var resData ConversionResponse
err = json.NewDecoder(res.Body).Decode(&resData)
if err != nil {
return ConversionResponse{}, err
}
return resData, nil
}
// OpenHighLowClose retrieves historical Open, High Low, Close (OHLC) and Average exchange rates for a given time period,
// ranging from 1 month to 1 minute, where available.
// https://docs.openexchangerates.org/docs/ohlc-json
func (c Client) OpenHighLowClose(ctx context.Context, opts ...OHLCOption) (OHLCResponse, error) {
r := ohlcParams{}
for _, opt := range opts {
opt(&r)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
fmt.Sprintf("%sohlc.json", c.baseURL), http.NoBody)
if err != nil {
return OHLCResponse{}, err
}
v := req.URL.Query()
v.Add("app_id", c.appID)
v.Add("prettyprint", strconv.FormatBool(r.prettyPrint))
v.Add("start_date", r.startTime.Format(time.RFC3339))
v.Add("period", r.period.String())
if r.destinationCurrencies != "" {
v.Add("symbols", r.destinationCurrencies)
}
if r.baseCurrency != "" {
v.Add("base", r.baseCurrency)
}
req.URL.RawQuery = v.Encode()
res, err := c.doer.Do(req)
defer res.Body.Close()
if err != nil {
return OHLCResponse{}, err
}
if res.StatusCode != http.StatusOK {
return OHLCResponse{}, fmt.Errorf("status received: %v: %w", res.StatusCode, ErrBadResponse)
}
var resData OHLCResponse
err = json.NewDecoder(res.Body).Decode(&resData)
if err != nil {
return OHLCResponse{}, err
}
return resData, nil
}
// Usage retrieves basic plan information and usage statistics for an Open Exchange Rates App ID.
// https://docs.openexchangerates.org/docs/usage-json
func (c Client) Usage(ctx context.Context, opts ...UsageOption) (UsageResponse, error) {
r := usageParams{}
for _, opt := range opts {
opt(&r)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
fmt.Sprintf("%susage.json", c.baseURL), http.NoBody)
if err != nil {
return UsageResponse{}, err
}
v := req.URL.Query()
v.Add("app_id", c.appID)
v.Add("prettyprint", strconv.FormatBool(r.prettyPrint))
req.URL.RawQuery = v.Encode()
res, err := c.doer.Do(req)
defer res.Body.Close()
if err != nil {
return UsageResponse{}, err
}
if res.StatusCode != http.StatusOK {
return UsageResponse{}, fmt.Errorf("status received: %v: %w", res.StatusCode, ErrBadResponse)
}
var resData UsageResponse
err = json.NewDecoder(res.Body).Decode(&resData)
if err != nil {
return UsageResponse{}, err
}
return resData, nil
}