-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
rates.go
141 lines (118 loc) · 3.05 KB
/
rates.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
package dinero
import (
"errors"
"fmt"
"net/url"
"time"
)
const (
latestAPIPath = "latest.json"
)
// RatesService handles rate request/responses.
type RatesService struct {
client *Client
baseCurrency string
}
// NewRatesService creates a new handler for this service.
func NewRatesService(
client *Client,
baseCurrency string,
) *RatesService {
return &RatesService{
client: client,
baseCurrency: baseCurrency,
}
}
// RateResponse holds our forex rates for a given base currency
type RateResponse struct {
Rates map[string]float64 `json:"rates"`
Base string `json:"base"`
Timestamp int64 `json:"timestamp"`
}
// List will fetch all the latest rates for the base currency either from the store or the OXR api.
func (s *RatesService) List() (*RateResponse, error) {
// If we have cached results, use them.
if results, ok := s.client.Cache.Get(s.baseCurrency, time.Now()); ok {
return results, nil
}
// No cached results, go and fetch them.
if err := s.fetch(); err != nil {
return nil, err
}
return s.List()
}
// ListHistorical will fetch all rates for the base currency for the given time.Time.
func (s *RatesService) ListHistorical(date time.Time) (*RateResponse, error) {
// add `base` query param if it is not empty.
params := url.Values{}
if s.baseCurrency != "" {
params.Set("base", s.baseCurrency)
}
request, err := s.client.NewRequest(
"GET",
fmt.Sprintf(historicalAPIPath, date.Format("2006-01-02")),
params,
nil,
)
if err != nil {
return nil, err
}
response := &RateResponse{}
if _, err := s.client.Do(request, response); err != nil {
return nil, err
}
return response, nil
}
// Get will fetch a single rate for a given currency either from the store or the OXR api.
func (s *RatesService) Get(code string) (*float64, error) {
// No code passed, let them know!
if code == "" {
return nil, errors.New("currency code must be passed")
}
// If we have cached results, use them.
if results, ok := s.client.Cache.Get(s.baseCurrency, time.Now()); ok {
if single, ok := results.Rates[code]; ok {
return &single, nil
}
return nil, ErrRatesNotFound
}
// No cached results, go and fetch them.
if err := s.fetch(); err != nil {
return nil, err
}
return s.Get(code)
}
// GetBaseCurrency will return the baseCurrency.
func (s *RatesService) GetBaseCurrency() string {
return s.baseCurrency
}
// SetBaseCurrency will set the base currency to be used for requests.
func (s *RatesService) SetBaseCurrency(base string) {
s.baseCurrency = base
}
func (s *RatesService) fetch() error {
// Build request.
// add `base` query param if it is not empty
params := url.Values{}
if s.baseCurrency != "" {
params.Set("base", s.baseCurrency)
}
request, err := s.client.NewRequest(
"GET",
latestAPIPath,
params,
nil,
)
if err != nil {
return err
}
// Make request
var latest *RateResponse
if _, err := s.client.Do(request, &latest); err != nil {
return err
}
s.SetBaseCurrency(latest.Base)
// Store our results.
s.client.Cache.Store(latest, time.Now())
return nil
}