-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
68 lines (56 loc) · 1.37 KB
/
main.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
package feeproxy
import (
"encoding/json"
"net/http"
"time"
)
const api = "https://bitcoinfees.earn.com/api/v1/fees/recommended"
const bcinfoAPI = "https://api.blockchain.info/mempool/fees"
type sourceData struct {
Priority int `json:"fastestFee"`
Normal int `json:"halfHourFee"`
Economic int `json:"hourFee"`
}
type limits struct {
Min int `json:"min"`
Max int `json:"max"`
}
type sourceDataBCI struct {
Limits limits `json:"limits"`
Regular int `json:"regular"`
Priority int `json:"priority"`
}
type responseData struct {
Priority int `json:"priority"`
Normal int `json:"normal"`
Economic int `json:"economic"`
SuperEconomic int `json:"superEconomic"`
}
var httpClient http.Client = http.Client{Timeout: time.Second * 30}
func Query() ([]byte, error) {
resp, err := httpClient.Get(api)
if err != nil {
return nil, err
}
feeData := &sourceData{}
err = json.NewDecoder(resp.Body).Decode(feeData)
if err != nil {
return nil, err
}
resp.Body.Close()
resp, err = httpClient.Get(bcinfoAPI)
if err != nil {
return nil, err
}
superData := &sourceDataBCI{}
err = json.NewDecoder(resp.Body).Decode(superData)
if err != nil {
return nil, err
}
return json.Marshal(&responseData{
Priority: superData.Limits.Max,
Normal: superData.Priority,
Economic: superData.Regular,
SuperEconomic: superData.Limits.Min,
})
}