forked from pegnet/pegnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeforex.go
170 lines (139 loc) · 4.25 KB
/
freeforex.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
package polling
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/cenkalti/backoff"
"github.com/pegnet/pegnet/common"
"github.com/zpatrick/go-config"
)
// FreeForexAPIDataSource is the datasource at https://www.freeforexapi.com
type FreeForexAPIDataSource struct {
config *config.Config
}
func NewFreeForexAPIDataSource(config *config.Config) (*FreeForexAPIDataSource, error) {
s := new(FreeForexAPIDataSource)
s.config = config
return s, nil
}
func (d *FreeForexAPIDataSource) Name() string {
return "FreeForexAPI"
}
func (d *FreeForexAPIDataSource) Url() string {
return "https://www.freeforexapi.com"
}
func (d *FreeForexAPIDataSource) ApiUrl() string {
return "https://www.freeforexapi.com/api/"
}
func (d *FreeForexAPIDataSource) SupportedPegs() []string {
// Does not have all the commodities
return common.MergeLists(common.CurrencyAssets, []string{"XAU", "XAG"})
}
func (d *FreeForexAPIDataSource) FetchPegPrices() (peg PegAssets, err error) {
resp, err := d.CallFreeForexAPI()
if err != nil {
return nil, err
}
peg = make(map[string]PegItem)
// Look for each asset we support
for _, asset := range d.SupportedPegs() {
index := fmt.Sprintf("USD%s", asset)
currency, ok := resp.Rates[index]
if !ok {
continue
}
timestamp := time.Unix(currency.Timestamp, 0)
// The USD price is 1/rate
peg[asset] = PegItem{Value: 1 / currency.Rate, WhenUnix: timestamp.Unix(), When: timestamp}
}
return
}
func (d *FreeForexAPIDataSource) FetchPegPrice(peg string) (i PegItem, err error) {
return FetchPegPrice(peg, d.FetchPegPrices)
}
func (d *FreeForexAPIDataSource) CallFreeForexAPI() (*FreeForexAPIDataSourceResponse, error) {
var resp *FreeForexAPIDataSourceResponse
operation := func() error {
data, err := d.FetchPeggedPrices()
if err != nil {
return err
}
resp, err = d.ParseFetchedPrices(data)
if err != nil {
// Try the other variation
return err
}
return nil
}
err := backoff.Retry(operation, PollingExponentialBackOff())
return resp, err
}
func (d *FreeForexAPIDataSource) ParseFetchedPrices(data []byte) (*FreeForexAPIDataSourceResponse, error) {
var resp FreeForexAPIDataSourceResponse
err := json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// ParseFetchedPricesVariation2 is when the api returns a different variation. For some reason this variation is missing
// XAG and XAU. For now, let's just let the original parse fail and exponentially retry.
// TODO: Figure out what the heck is going on when the response format is changed.
// Also the date was more than 2 weeks old. So I think this variation is.... bad
func (d *FreeForexAPIDataSource) ParseFetchedPricesVariation2(data []byte) (*FreeForexAPIDataSourceResponse, error) {
var resp FreeForexAPIDataSourceResponseVariation2
err := json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
// Convert to the normal
var orig FreeForexAPIDataSourceResponse
orig.Rates = make(map[string]FreeForexAPIDataSourceRate)
orig.Code = resp.Code
timestamp, err := time.Parse("2006-01-02", resp.Date)
if err != nil {
return nil, err
}
for k, v := range resp.Rates {
orig.Rates[resp.Base+k] = FreeForexAPIDataSourceRate{Rate: v, Timestamp: timestamp.Unix()}
}
return &orig, nil
}
func (d *FreeForexAPIDataSource) FetchPeggedPrices() ([]byte, error) {
client := NewHTTPClient()
req, err := http.NewRequest("GET", d.ApiUrl()+"live", nil)
if err != nil {
return nil, err
}
var ids []string
for _, asset := range d.SupportedPegs() {
ids = append(ids, "USD"+asset)
}
q := url.Values{}
q.Add("pairs", strings.Join(ids, ","))
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
type FreeForexAPIDataSourceResponseVariation2 struct {
Code int `json:"code"`
Base string `json:"base"`
Date string `json:"date"`
Rates map[string]float64 `json:"rates"`
}
type FreeForexAPIDataSourceResponse struct {
Code int `json:"code"`
Rates map[string]FreeForexAPIDataSourceRate `json:"rates"`
}
type FreeForexAPIDataSourceRate struct {
Rate float64 `json:"rate"`
Timestamp int64 `json:"timestamp"`
}