-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinbin.go
156 lines (126 loc) · 3.49 KB
/
coinbin.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
package coinbin
import (
"net/http"
"fmt"
"io/ioutil"
"encoding/json"
"errors"
)
type Coin struct {
Name string `json:"name"`
Ticker string `json:"ticker"`
USDValue float64 `json:"usd"`
BTCValue float64 `json:"btc"`
Rank int `json:"rank"`
}
func (c Coin) GetValue(amount float64) (CoinValue, error) {
return GetCoinValue(c.Name, amount)
}
type CoinResponse struct {
Coin Coin `json:"coin"`
}
type CoinValue struct {
ExchangeRate float64 `json:"exchange_rate"`
USDValue float64 `json:"usd"`
}
type CoinValueResponse struct {
CoinValue CoinValue `json:"coin"`
}
type CoinExchangeRate struct {
ExchangeRate float64 `json:"exchange_rate"`
}
type CoinExchangeRateResponse struct {
CoinExchangeRate CoinExchangeRate `json:"coin"`
}
type CoinExchangeValue struct {
ExchangeRate float64 `json:"exchange_rate"`
Value float64 `json:"value"`
ResultCoin string `json:"value.coin"`
}
type CoinExchangeValueResponse struct {
CoinExchangeValue CoinExchangeValue `json:"coin"`
}
func GetCoin(name string) (Coin, error) {
url := fmt.Sprintf("https://coinbin.org/%s", name)
response, err := http.Get(url)
if err != nil {
return Coin{}, err
}
if response.StatusCode == 500 {
return Coin{}, errors.New("specified coin does not exist")
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return Coin{}, err
}
var coinResponse CoinResponse
err = json.Unmarshal(data, &coinResponse)
if err != nil {
return Coin{}, err
}
return coinResponse.Coin, err
}
func GetCoinValue(name string, amount float64) (CoinValue, error) {
url := fmt.Sprintf("https://coinbin.org/%s/%f", name, amount)
response, err := http.Get(url)
if err != nil {
return CoinValue{}, err
}
if response.StatusCode == 500 {
return CoinValue{}, errors.New("specified coin does not exist")
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return CoinValue{}, err
}
var coinValueResponse CoinValueResponse
err = json.Unmarshal(data, &coinValueResponse)
if err != nil {
return CoinValue{}, err
}
return coinValueResponse.CoinValue, err
}
func GetCoinExchangeRate(from string, to string) (CoinExchangeRate, error) {
url := fmt.Sprintf("https://coinbin.org/%s/to/%s", from, to)
response, err := http.Get(url)
if err != nil {
return CoinExchangeRate{}, err
}
if response.StatusCode == 500 {
return CoinExchangeRate{}, errors.New("specified coin does not exist")
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return CoinExchangeRate{}, err
}
var coinExchangeRateResponse CoinExchangeRateResponse
err = json.Unmarshal(data, &coinExchangeRateResponse)
if err != nil {
return CoinExchangeRate{}, err
}
return coinExchangeRateResponse.CoinExchangeRate, err
}
func GetCoinExchangeValue(from string, to string, amount float64) (CoinExchangeValue, error) {
url := fmt.Sprintf("https://coinbin.org/%s/%f/to/%s", from, amount, to)
response, err := http.Get(url)
if err != nil {
return CoinExchangeValue{}, err
}
if response.StatusCode == 500 {
return CoinExchangeValue{}, errors.New("specified coin does not exist")
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return CoinExchangeValue{}, err
}
var coinExchangeValueResponse CoinExchangeValueResponse
err = json.Unmarshal(data, &coinExchangeValueResponse)
if err != nil {
return CoinExchangeValue{}, err
}
return coinExchangeValueResponse.CoinExchangeValue, err
}