This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
v3.go
executable file
·362 lines (322 loc) · 9.09 KB
/
v3.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package coingecko
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/superoo7/go-gecko/format"
"github.com/superoo7/go-gecko/v3/types"
)
var baseURL = "https://api.coingecko.com/api/v3"
// Client struct
type Client struct {
httpClient *http.Client
}
// NewClient create new client object
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
return &Client{httpClient: httpClient}
}
// helper
// doReq HTTP client
func doReq(req *http.Request, client *http.Client) ([]byte, error) {
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if 200 != resp.StatusCode {
return nil, fmt.Errorf("%s", body)
}
return body, nil
}
// MakeReq HTTP request helper
func (c *Client) MakeReq(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := doReq(req, c.httpClient)
if err != nil {
return nil, err
}
return resp, err
}
// API
// Ping /ping endpoint
func (c *Client) Ping() (*types.Ping, error) {
url := fmt.Sprintf("%s/ping", baseURL)
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.Ping
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
// SimpleSinglePrice /simple/price Single ID and Currency (ids, vs_currency)
func (c *Client) SimpleSinglePrice(id string, vsCurrency string) (*types.SimpleSinglePrice, error) {
idParam := []string{strings.ToLower(id)}
vcParam := []string{strings.ToLower(vsCurrency)}
t, err := c.SimplePrice(idParam, vcParam)
if err != nil {
return nil, err
}
curr := (*t)[id]
if len(curr) == 0 {
return nil, fmt.Errorf("id or vsCurrency not existed")
}
data := &types.SimpleSinglePrice{ID: id, Currency: vsCurrency, MarketPrice: curr[vsCurrency]}
return data, nil
}
// SimplePrice /simple/price Multiple ID and Currency (ids, vs_currencies)
func (c *Client) SimplePrice(ids []string, vsCurrencies []string) (*map[string]map[string]float32, error) {
params := url.Values{}
idsParam := strings.Join(ids[:], ",")
vsCurrenciesParam := strings.Join(vsCurrencies[:], ",")
params.Add("ids", idsParam)
params.Add("vs_currencies", vsCurrenciesParam)
url := fmt.Sprintf("%s/simple/price?%s", baseURL, params.Encode())
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
t := make(map[string]map[string]float32)
err = json.Unmarshal(resp, &t)
if err != nil {
return nil, err
}
return &t, nil
}
// SimpleSupportedVSCurrencies /simple/supported_vs_currencies
func (c *Client) SimpleSupportedVSCurrencies() (*types.SimpleSupportedVSCurrencies, error) {
url := fmt.Sprintf("%s/simple/supported_vs_currencies", baseURL)
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.SimpleSupportedVSCurrencies
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
// CoinsList /coins/list
func (c *Client) CoinsList() (*types.CoinList, error) {
url := fmt.Sprintf("%s/coins/list", baseURL)
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.CoinList
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
// CoinsMarket /coins/market
func (c *Client) CoinsMarket(vsCurrency string, ids []string, order string, perPage int, page int, sparkline bool, priceChangePercentage []string) (*types.CoinsMarket, error) {
if len(vsCurrency) == 0 {
return nil, fmt.Errorf("vs_currency is required")
}
params := url.Values{}
// vs_currency
params.Add("vs_currency", vsCurrency)
// order
if len(order) == 0 {
order = types.OrderTypeObject.MarketCapDesc
}
params.Add("order", order)
// ids
if len(ids) != 0 {
idsParam := strings.Join(ids[:], ",")
params.Add("ids", idsParam)
}
// per_page
if perPage <= 0 || perPage > 250 {
perPage = 100
}
params.Add("per_page", format.Int2String(perPage))
params.Add("page", format.Int2String(page))
// sparkline
params.Add("sparkline", format.Bool2String(sparkline))
// price_change_percentage
if len(priceChangePercentage) != 0 {
priceChangePercentageParam := strings.Join(priceChangePercentage[:], ",")
params.Add("price_change_percentage", priceChangePercentageParam)
}
url := fmt.Sprintf("%s/coins/markets?%s", baseURL, params.Encode())
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.CoinsMarket
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
// CoinsID /coins/{id}
func (c *Client) CoinsID(id string, localization bool, tickers bool, marketData bool, communityData bool, developerData bool, sparkline bool) (*types.CoinsID, error) {
if len(id) == 0 {
return nil, fmt.Errorf("id is required")
}
params := url.Values{}
params.Add("localization", format.Bool2String(sparkline))
params.Add("tickers", format.Bool2String(tickers))
params.Add("market_data", format.Bool2String(marketData))
params.Add("community_data", format.Bool2String(communityData))
params.Add("developer_data", format.Bool2String(developerData))
params.Add("sparkline", format.Bool2String(sparkline))
url := fmt.Sprintf("%s/coins/%s?%s", baseURL, id, params.Encode())
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.CoinsID
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
// CoinsIDTickers /coins/{id}/tickers
func (c *Client) CoinsIDTickers(id string, page int) (*types.CoinsIDTickers, error) {
if len(id) == 0 {
return nil, fmt.Errorf("id is required")
}
params := url.Values{}
if page > 0 {
params.Add("page", format.Int2String(page))
}
url := fmt.Sprintf("%s/coins/%s/tickers?%s", baseURL, id, params.Encode())
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.CoinsIDTickers
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
// CoinsIDHistory /coins/{id}/history?date={date}&localization=false
func (c *Client) CoinsIDHistory(id string, date string, localization bool) (*types.CoinsIDHistory, error) {
if len(id) == 0 || len(date) == 0 {
return nil, fmt.Errorf("id and date is required")
}
params := url.Values{}
params.Add("date", date)
params.Add("localization", format.Bool2String(localization))
url := fmt.Sprintf("%s/coins/%s/history?%s", baseURL, id, params.Encode())
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.CoinsIDHistory
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
// CoinsIDMarketChart /coins/{id}/market_chart?vs_currency={usd, eur, jpy, etc.}&days={1,14,30,max}
func (c *Client) CoinsIDMarketChart(id string, vs_currency string, days string) (*types.CoinsIDMarketChart, error) {
if len(id) == 0 || len(vs_currency) == 0 || len(days) == 0 {
return nil, fmt.Errorf("id, vs_currency, and days is required")
}
params := url.Values{}
params.Add("vs_currency", vs_currency)
params.Add("days", days)
url := fmt.Sprintf("%s/coins/%s/market_chart?%s", baseURL, id, params.Encode())
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
m := types.CoinsIDMarketChart{}
err = json.Unmarshal(resp, &m)
if err != nil {
return &m, err
}
return &m, nil
}
// CoinsIDStatusUpdates
// CoinsIDContractAddress https://api.coingecko.com/api/v3/coins/{id}/contract/{contract_address}
// func CoinsIDContractAddress(id string, address string) (nil, error) {
// url := fmt.Sprintf("%s/coins/%s/contract/%s", baseURL, id, address)
// resp, err := request.MakeReq(url)
// if err != nil {
// return nil, err
// }
// }
// EventsCountries https://api.coingecko.com/api/v3/events/countries
func (c *Client) EventsCountries() ([]types.EventCountryItem, error) {
url := fmt.Sprintf("%s/events/countries", baseURL)
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.EventsCountries
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data.Data, nil
}
// EventsTypes https://api.coingecko.com/api/v3/events/types
func (c *Client) EventsTypes() (*types.EventsTypes, error) {
url := fmt.Sprintf("%s/events/types", baseURL)
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.EventsTypes
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
// ExchangeRates https://api.coingecko.com/api/v3/exchange_rates
func (c *Client) ExchangeRates() (*types.ExchangeRatesItem, error) {
url := fmt.Sprintf("%s/exchange_rates", baseURL)
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.ExchangeRatesResponse
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return &data.Rates, nil
}
// Global https://api.coingecko.com/api/v3/global
func (c *Client) Global() (*types.Global, error) {
url := fmt.Sprintf("%s/global", baseURL)
resp, err := c.MakeReq(url)
if err != nil {
return nil, err
}
var data *types.GlobalResponse
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return &data.Data, nil
}