-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
currencies.go
54 lines (47 loc) · 1.08 KB
/
currencies.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
package dinero
const (
currenciesAPIPath = "currencies.json"
)
// CurrenciesService handles currency request/responses.
type CurrenciesService struct {
client *Client
}
// NewCurrenciesService creates a new handler for this service.
func NewCurrenciesService(
client *Client,
) *CurrenciesService {
return &CurrenciesService{
client,
}
}
// CurrencyResponse represents a currency from OXR.
type CurrencyResponse struct {
Code string `json:"code"`
Name string `json:"name"`
}
// List will fetch all list of all currencies available via the OXR api.
func (s *CurrenciesService) List() ([]*CurrencyResponse, error) {
// Build request.
req, err := s.client.NewUnauthedRequest(
"GET",
currenciesAPIPath,
nil,
)
if err != nil {
return nil, err
}
// Make request.
rsp := map[string]string{}
if _, err = s.client.Do(req, &rsp); err != nil {
return nil, err
}
// Parse rsp into slice of *CurrencyResponse's.
latest := []*CurrencyResponse{}
for code, name := range rsp {
latest = append(latest, &CurrencyResponse{
Code: code,
Name: name,
})
}
return latest, nil
}