This repository was archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathfiatFeedOxr.go
78 lines (65 loc) · 1.66 KB
/
fiatFeedOxr.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
package plugins
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type fiatFeedOxr struct {
url string
client http.Client
}
type oxrRates struct {
Disclaimer string `json:"disclaimer"`
License string `json:"license"`
Timestamp int64 `json:"timestamp"`
Base string `json:"base"`
Rates map[string]float64 `json:"rates"`
}
type oxrError struct {
Err bool `json:"error"`
Status int `json:"status"`
Message string `json:"message"`
Description string `json:"description"`
}
func (e oxrError) Error() string {
return fmt.Sprintf("%v: %v", e.Message, e.Description)
}
var oxrErrorCodeMsg = map[int]string{
404: "not_found",
401: "invalid_or_missing_app_id",
429: "not_allowed",
403: "access_restricted",
400: "invalid_base",
}
func newFiatFeedOxr(url string) *fiatFeedOxr {
return &fiatFeedOxr{
url: url,
client: http.Client{Timeout: 10 * time.Second},
}
}
func (f *fiatFeedOxr) GetPrice() (float64, error) {
res, err := f.client.Get(f.url)
if err != nil {
return 0, fmt.Errorf("oxr: error %w", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
var e oxrError
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return 0, fmt.Errorf("oxr: error %w", err)
}
return 0, e
}
var rates oxrRates
if err := json.NewDecoder(res.Body).Decode(&rates); err != nil {
return 0, fmt.Errorf("oxr: error %w", err)
}
if len(rates.Rates) != 1 {
return 0, fmt.Errorf("oxr: error rates must contain single value found len %d", len(rates.Rates))
}
for _, v := range rates.Rates {
return v, nil
}
return 0, fmt.Errorf("oxr: error retrieving price")
}