forked from pegnet/pegnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapilayer.go
114 lines (92 loc) · 2.63 KB
/
apilayer.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
// Copyright (c) of parts are held by the various contributors (see the CLA)
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
package polling
import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/cenkalti/backoff"
"github.com/pegnet/pegnet/common"
log "github.com/sirupsen/logrus"
"github.com/zpatrick/go-config"
)
// APILayerDataSource is the datasource at http://www.apilayer.net
type APILayerDataSource struct {
config *config.Config
}
func NewAPILayerDataSource(config *config.Config) (*APILayerDataSource, error) {
s := new(APILayerDataSource)
s.config = config
return s, nil
}
func (d *APILayerDataSource) Name() string {
return "APILayer"
}
func (d *APILayerDataSource) Url() string {
return "https://apilayer.com/"
}
func (d *APILayerDataSource) SupportedPegs() []string {
return common.CurrencyAssets
}
func (d *APILayerDataSource) FetchPegPrices() (peg PegAssets, err error) {
resp, err := CallAPILayer(d.config)
if err != nil {
return nil, err
}
peg = make(map[string]PegItem)
timestamp := time.Unix(resp.Timestamp, 0)
for _, currencyISO := range d.SupportedPegs() {
// Search for USDXXX pairs
if v, ok := resp.Quotes["USD"+currencyISO]; ok {
peg[currencyISO] = PegItem{Value: 1 / v, When: timestamp, WhenUnix: timestamp.Unix()}
}
}
return
}
func (d *APILayerDataSource) FetchPegPrice(peg string) (i PegItem, err error) {
return FetchPegPrice(peg, d.FetchPegPrices)
}
// ----
type APILayerResponse struct {
Success bool `json:"success"`
Terms string `json:"terms"`
Privacy string `json:"privacy"`
Timestamp int64 `json:"timestamp"`
Source string `json:"source"`
Quotes map[string]float64 `json:"quotes"`
Error APILayerError `json:"error"`
}
type APILayerError struct {
Code int64
Type string
Info string
}
func check(e error) {
if e != nil {
panic(e)
}
}
func CallAPILayer(c *config.Config) (response APILayerResponse, err error) {
var APILayerResponse APILayerResponse
var apikey string
{
apikey, err = c.String("Oracle.APILayerKey")
check(err)
}
operation := func() error {
resp, err := http.Get("http://www.apilayer.net/api/live?access_key=" + apikey)
if err != nil {
log.WithError(err).Warning("Failed to get response from API Layer")
}
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err != nil {
return err
} else if err = json.Unmarshal(body, &APILayerResponse); err != nil {
return err
}
return err
}
err = backoff.Retry(operation, PollingExponentialBackOff())
return APILayerResponse, err
}