-
Notifications
You must be signed in to change notification settings - Fork 9
/
coins.go
181 lines (157 loc) · 4.64 KB
/
coins.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package goingecko
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/JulianToledano/goingecko/coins"
"github.com/JulianToledano/goingecko/types"
)
func (c *Client) CoinsList() ([]*coins.CoinInfo, error) {
rUrl := fmt.Sprintf("%s/%s", c.getCoinsURL(), "list")
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data []*coins.CoinInfo
err = json.Unmarshal([]byte(resp), &data)
if err != nil {
return nil, err
}
return data, nil
}
func (c *Client) CoinsMarket(currency string, ids []string, category string, order string, perPage, page string, sparkline bool, priceChange []string) ([]*coins.Market, error) {
params := url.Values{}
idsParam := strings.Join(ids[:], ",")
pChange := strings.Join(priceChange[:], ",")
// vsCurrenciesParam := strings.Join(vsCurrencies[:], ",")
params.Add("vs_currency", currency)
params.Add("ids", idsParam)
if category != "" {
params.Add("category", category)
}
params.Add("order", order)
params.Add("per_page", perPage)
params.Add("page", page)
params.Add("sparkline", strconv.FormatBool(sparkline))
params.Add("price_change_percentage", pChange)
rUrl := fmt.Sprintf("%s/%s?%s", c.getCoinsURL(), "markets", params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data []*coins.Market
err = json.Unmarshal([]byte(resp), &data)
if err != nil {
return nil, err
}
return data, nil
}
func (c *Client) CoinsId(id string, localization, tickers, marketData, communityData, developerData, sparkline bool) (*coins.CoinID, error) {
params := url.Values{}
params.Add("localization", strconv.FormatBool(localization))
params.Add("tickers", strconv.FormatBool(tickers))
params.Add("market_data", strconv.FormatBool(marketData))
params.Add("community_data", strconv.FormatBool(communityData))
params.Add("developer_data", strconv.FormatBool(developerData))
params.Add("sparkline", strconv.FormatBool(sparkline))
rUrl := fmt.Sprintf("%s/%s?%s", c.getCoinsURL(), id, params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data *coins.CoinID
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
func (c *Client) CoinsIdTickers(id, exchangeId, includeExchangeLogo, page, order, depth string) (*coins.Tickers, error) {
params := url.Values{}
params.Add("exchange_ids", exchangeId)
params.Add("include_exchange_logo", includeExchangeLogo)
params.Add("page", page)
params.Add("order", order)
params.Add("depth", depth)
rUrl := fmt.Sprintf("%s/%s/%s?%s", c.getCoinsURL(), id, "tickers", params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data *coins.Tickers
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
func (c *Client) CoinsIdHistory(id, date string, localization bool) (*coins.History, error) {
params := url.Values{}
params.Add("date", date)
params.Add("localization", strconv.FormatBool(localization))
rUrl := fmt.Sprintf("%s/%s/%s?%s", c.getCoinsURL(), id, "history", params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data *coins.History
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
func (c *Client) CoinsIdMarketChart(id, currency, days, interval string) (*types.MarketChart, error) {
params := url.Values{}
params.Add("vs_currency", currency)
params.Add("days", days)
if interval != "" {
params.Add("interval", interval)
}
rUrl := fmt.Sprintf("%s/%s/%s?%s", c.getCoinsURL(), id, "market_chart", params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data *types.MarketChart
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
func (c *Client) CoinsIdMarketChartRange(id, currency, from, to string) (*types.MarketChart, error) {
params := url.Values{}
params.Add("vs_currency", currency)
params.Add("from", from)
params.Add("to", to)
rUrl := fmt.Sprintf("%s/%s/%s?%s", c.getCoinsURL(), id, "market_chart/range", params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data *types.MarketChart
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
func (c *Client) CoinsOhlc(id, currency, days string) (*coins.Ohlc, error) {
params := url.Values{}
params.Add("vs_currency", currency)
params.Add("days", days)
rUrl := fmt.Sprintf("%s/%s/%s?%s", c.getCoinsURL(), id, "ohlc", params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data *coins.Ohlc
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}