-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
65 lines (57 loc) · 1.39 KB
/
client.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
package gocko
import (
"encoding/json"
"net/http"
)
const baseURL = "https://api.coingecko.com/api/v3"
type Client struct {
httpClient *http.Client
}
type Option func(*Client)
func NewClient(options ...Option) *Client {
c := &Client{
httpClient: http.DefaultClient,
}
for _, option := range options {
option(c)
}
return c
}
func WithHttpClient(hc *http.Client) Option { return func(c *Client) { c.httpClient = hc } }
func (c *Client) Do(url string, params QueryParams, ptr interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
if params != nil {
qp, err := params.toQuery()
if err != nil {
return err
}
q := req.URL.Query()
for k, v := range qp {
q.Set(k, v)
}
req.URL.RawQuery = q.Encode()
}
res, err := c.httpClient.Do(req)
if err != nil {
return err
}
return json.NewDecoder(res.Body).Decode(ptr)
}
type Api interface {
Ping() (Ping, error)
SimpleSupportedVsCurrencies() ([]string, error)
SimplePrice(SimplePriceParams) (SimplePrices, error)
CoinsList(CoinsParams) ([]Coin, error)
CoinsMarkets(CoinsMarketsParams) ([]Market, error)
CoinsID(CoinsDataParams) (CoinData, error)
CoinsMarketCharts(CoinsChartsParams) (Charts, error)
CoinsOHLC(CoinsOHLCParams) (OHLC, error)
ExchangesList() (ExchangeList, error)
Exchanges(params ExchangesParams) ([]Exchange, error)
}
func assertApiInterface() {
var _ Api = (*Client)(nil)
}