forked from peterhellberg/fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
214 lines (169 loc) · 4.61 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
package fixer
import (
"context"
"encoding/json"
"net/http"
"net/url"
"os"
"time"
)
// FixerClient is a client configured to use https://api.fixer.io
var FixerClient = NewClient(AccessKey(os.Getenv("FIXER_ACCESS_KEY")))
// ExratesClient is a client configured to use https://api.exchangeratesapi.io
var ExratesClient = NewClient(BaseURL("https://api.exchangeratesapi.io"))
// DefaultClient is the default client for the Foreign exchange rates and currency conversion API
var DefaultClient = FixerClient
const DateFormat = "2006-01-02"
// Client for the Foreign exchange rates and currency conversion API
type Client struct {
httpClient *http.Client
baseURL *url.URL
accessKey string
userAgent string
}
// NewClient creates a Client
func NewClient(options ...func(*Client)) *Client {
c := &Client{
httpClient: &http.Client{
Timeout: 20 * time.Second,
},
baseURL: &url.URL{
Scheme: "https",
Host: "api.apilayer.com",
Path: "/fixer",
},
accessKey: "",
userAgent: "fixer/client.go (https://github.com/peterhellberg/fixer)",
}
for _, f := range options {
f(c)
}
return c
}
// HTTPClient changes the HTTP client to the provided *http.Client
func HTTPClient(hc *http.Client) func(*Client) {
return func(c *Client) {
c.httpClient = hc
}
}
// BaseURL changes the base URL to the provided rawurl
func BaseURL(rawurl string) func(*Client) {
return func(c *Client) {
if u, err := url.Parse(rawurl); err == nil {
c.baseURL = u
}
}
}
// AccessKey sets the access key used by the client
func AccessKey(ak string) func(*Client) {
return func(c *Client) {
c.accessKey = ak
}
}
// UserAgent changes the User-Agent used by the client
func UserAgent(ua string) func(*Client) {
return func(c *Client) {
c.userAgent = ua
}
}
// Base sets the base query variable based on a Currency
func Base(c Currency) url.Values {
v := url.Values{}
if s := string(c); s != "" {
v.Set("base", s)
}
return v
}
// Symbols sets the symbols query variable based on the provided currencies
func Symbols(cs ...Currency) url.Values {
v := url.Values{}
if s := Currencies(cs).String(); s != "" {
v.Set("symbols", s)
}
return v
}
// Latest foreign exchange reference rates
func (c *Client) Latest(ctx context.Context, attributes ...url.Values) (*Response, error) {
return c.get(ctx, "/latest", c.query(attributes))
}
// At returns historical rates for any day since 1999
func (c *Client) At(ctx context.Context, t time.Time, attributes ...url.Values) (*Response, error) {
return c.get(ctx, "/"+c.date(t), c.query(attributes))
}
func (c *Client) date(t time.Time) string {
return t.Format(DateFormat)
}
func (c *Client) get(ctx context.Context, path string, query url.Values) (*Response, error) {
req, err := c.request(ctx, path, query)
if err != nil {
return nil, err
}
r, err := c.do(req)
if err != nil {
return nil, err
}
r.Links = Links{
"base": c.baseURL.String(),
"self": req.URL.String(),
}
return r, nil
}
func (c *Client) query(attributes []url.Values) url.Values {
v := url.Values{}
for _, a := range attributes {
if base := a.Get("base"); base != "" {
v.Set("base", base)
}
if symbols := a.Get("symbols"); symbols != "" {
v.Set("symbols", symbols)
}
}
return v
}
func (c *Client) request(ctx context.Context, path string, query url.Values) (*http.Request, error) {
rawurl := c.baseURL.Path + path
if len(query) > 0 {
rawurl += "?" + query.Encode()
}
rel, err := url.Parse(rawurl)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", c.baseURL.ResolveReference(rel).String(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
if c.accessKey != "" {
req.Header.Add("apiKey", c.accessKey)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("User-Agent", c.userAgent)
return req, nil
}
func (c *Client) do(req *http.Request) (*Response, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
// defer func() {
// _, _ = io.CopyN(ioutil.Discard, resp.Body, 64)
// _ = resp.Body.Close()
// }()
if err := responseError(resp); err != nil {
return nil, err
}
var r Response
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
return nil, err
}
return &r, nil
}
// Latest foreign exchange reference rates using the DefaultClient
func Latest(ctx context.Context, attributes ...url.Values) (*Response, error) {
return DefaultClient.Latest(ctx, attributes...)
}
// At returns historical rates for any day since 1999 using the DefaultClient
func At(ctx context.Context, t time.Time, attributes ...url.Values) (*Response, error) {
return DefaultClient.At(ctx, t, attributes...)
}