-
Notifications
You must be signed in to change notification settings - Fork 13
/
fetcher.go
80 lines (65 loc) · 1.75 KB
/
fetcher.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
package ticker
import (
"encoding/json"
"net/http"
"time"
"github.com/gocraft/health"
)
var httpClient = &http.Client{Timeout: 30 * time.Second}
type fetchFn func() (exchangeRates, error)
// Fetch gets data from all sources, formats it, and sends it to the Writers.
func Fetch(stream *health.Stream, conf Config, writers ...Writer) error {
job := stream.NewJob("fetch")
// Fetch data from each provider
allRates := []exchangeRates{{"BTC": {Ask: "1", Bid: "1", Last: "1", Type: exchangeRateTypeCrypto.String()}}}
for _, f := range []fetchFn{
NewBTCAVGFetcher(conf.BTCAVGPubkey, conf.BTCAVGPrivkey),
NewCMCFetcher(conf.CMCEnv, conf.CMCAPIKey),
} {
rates, err := f()
if err != nil {
job.EventErr("fetch_data", err)
job.Complete(health.Error)
return err
}
allRates = append(allRates, rates)
}
fullRates := mergeRates(allRates)
// Ensure the final payload passes correctness checks
err := validateRates(fullRates)
if err != nil {
job.EventErr("validate_rates", err)
job.Complete(health.Error)
return err
}
// Serialize responses
responseBytes, err := json.Marshal(fullRates)
if err != nil {
job.EventErr("marshal", err)
job.Complete(health.Error)
return err
}
// Write
for _, writer := range writers {
err := writer(job, responseBytes)
if err != nil {
job.EventErr("write", err)
job.Complete(health.Error)
return err
}
}
job.Complete(health.Success)
return nil
}
func validateRates(rates exchangeRates) error {
for _, symbol := range RequiredSymbols {
if _, ok := rates[symbol]; !ok {
return errFetchMissingRequiredSymbol(symbol)
}
}
return nil
}
type errFetchMissingRequiredSymbol string
func (e errFetchMissingRequiredSymbol) Error() string {
return "Missing required symbol: " + string(e)
}