-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.go
273 lines (240 loc) · 6.47 KB
/
requests.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
package erh
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
baseUrl = "https://api.exchangerate.host"
)
//Arg contain formatted argument
type Arg struct {
key string
value string
}
//Key returns key of the argument
func (a *Arg) Key() string {
return a.key
}
//Value returns value of the argument
func (a *Arg) Value() string {
return a.value
}
//String stringer interface
func (a *Arg) String() string {
return fmt.Sprintf("%s=%s", a.key, a.value)
}
//ArgBase add base currency argument
func ArgBase(base string) Arg {
return Arg{key: "base", value: base}
}
//ArgPlaces add currency places argument
func ArgPlaces(places int) Arg {
return Arg{key: "places", value: strconv.Itoa(places)}
}
//ArgAmount add currency amount argument
func ArgAmount(amount float64) Arg {
return Arg{key: "amount", value: strconv.FormatFloat(amount, 'f', -1, 64)}
}
//ArgSymbols add currency symbols argument
func ArgSymbols(symbols []string) Arg {
return Arg{key: "symbols", value: strings.Join(symbols, ",")}
}
//argsURLEncoded convert slice of arguments to url encoded string
func argsURLEncoded(args []Arg) string {
uv := url.Values{}
for _, arg := range args {
uv.Add(arg.Key(), arg.Value())
}
return uv.Encode()
}
//Client is API client
type Client struct {
client *http.Client
}
//NewClient create new API client with default http.Client
func NewClient() *Client {
return &Client{client: http.DefaultClient}
}
//SetHttpClient set custom http client
func (c *Client) SetHttpClient(client *http.Client) {
if client == nil {
return
}
c.client = client
}
//ConvertCtx convert currency request with context
func (c *Client) ConvertCtx(ctx context.Context, from, to string, amount float64, args ...Arg) (ConvertResponse, error) {
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf(
"%s/convert?from=%s&to=%s&amount=%f&%s",
baseUrl,
from,
to,
amount,
argsURLEncoded(args),
),
nil,
)
if err != nil {
return ConvertResponse{}, err
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return ConvertResponse{}, err
}
if resp.StatusCode != http.StatusOK {
return ConvertResponse{}, errors.New(resp.Status)
}
var response ConvertResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return ConvertResponse{}, err
}
return response, nil
}
//Convert currency conversion endpoint, can be used to convert any amount from one currency to another.
//In order to convert currencies, please use the API's convert endpoint,
//append the "from" and "to" parameters and set them to your preferred base and target currency codes.
func (c *Client) Convert(from, to string, amount float64, args ...Arg) (ConvertResponse, error) {
return c.ConvertCtx(context.Background(), from, to, amount, args...)
}
//HistoricalCtx historical currency with context
func (c *Client) HistoricalCtx(ctx context.Context, date time.Time, args ...Arg) (HistoricalResponse, error) {
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf(
"%s/%s?%s",
baseUrl,
date.Format("2006-01-02"),
argsURLEncoded(args),
),
nil,
)
if err != nil {
return HistoricalResponse{}, err
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return HistoricalResponse{}, err
}
if resp.StatusCode != http.StatusOK {
return HistoricalResponse{}, errors.New(resp.Status)
}
var response HistoricalResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return HistoricalResponse{}, err
}
return response, nil
}
//Historical rates are available for most currencies all the way back to the year of 1999.
func (c *Client) Historical(date time.Time, args ...Arg) (HistoricalResponse, error) {
return c.HistoricalCtx(context.Background(), date, args...)
}
//TimeSeriesCtx time series request with context
func (c *Client) TimeSeriesCtx(ctx context.Context, start, end time.Time, args ...Arg) (TimeSeriesResponse, error) {
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf(
"%s/timeseries?start_date=%s&end_date=%s&%s",
baseUrl,
start.Format("2006-01-02"),
end.Format("2006-01-02"),
argsURLEncoded(args),
),
nil,
)
if err != nil {
return TimeSeriesResponse{}, err
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return TimeSeriesResponse{}, err
}
if resp.StatusCode != http.StatusOK {
return TimeSeriesResponse{}, errors.New(resp.Status)
}
var response TimeSeriesResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return TimeSeriesResponse{}, err
}
return response, nil
}
//TimeSeries endpoint are for daily historical rates between two dates of your choice,
//with a maximum time frame of 366 days.
func (c *Client) TimeSeries(start, end time.Time, args ...Arg) (TimeSeriesResponse, error) {
return c.TimeSeriesCtx(context.Background(), start, end, args...)
}
//SymbolsCtx returns available symbols
func (c *Client) SymbolsCtx(ctx context.Context) (SymbolsResponse, error) {
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf(
"%s/symbols",
baseUrl,
),
nil,
)
if err != nil {
return SymbolsResponse{}, err
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return SymbolsResponse{}, err
}
if resp.StatusCode != http.StatusOK {
return SymbolsResponse{}, errors.New(resp.Status)
}
var response SymbolsResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return SymbolsResponse{}, err
}
return response, nil
}
//Symbols with context.Background
func (c *Client) Symbols() (SymbolsResponse, error) {
return c.SymbolsCtx(context.Background())
}
//LatestCtx request latest rates with context
func (c *Client) LatestCtx(ctx context.Context, args ...Arg) (LatestResponse, error) {
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf(
"%s/latest?%s",
baseUrl,
argsURLEncoded(args),
),
nil,
)
if err != nil {
return LatestResponse{}, err
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return LatestResponse{}, err
}
if resp.StatusCode != http.StatusOK {
return LatestResponse{}, errors.New(resp.Status)
}
var response LatestResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return LatestResponse{}, err
}
return response, nil
}
//Latest get the latest foreign exchange reference rates.
//Latest endpoint will return exchange rate data updated on daily basis.
func (c *Client) Latest(args ...Arg) (LatestResponse, error) {
return c.LatestCtx(context.Background(), args...)
}