-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparameters.go
158 lines (140 loc) · 4.36 KB
/
parameters.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
package gocko
import (
"errors"
"strconv"
"strings"
)
var MissingParameterError = errors.New("missing parameter")
var InvalidParameterError = errors.New("invalid parameter")
type QueryParams interface {
toQuery() (map[string]string, error)
}
type CoinsParams struct {
includePlatform bool
}
func (c CoinsParams) toQuery() (map[string]string, error) {
return map[string]string{"include_platform": strconv.FormatBool(c.includePlatform)}, nil
}
type SimplePriceParams struct {
Ids []string // required
VsCurrencies []string // required
IncludeMarketCap bool
Include24hrVol bool
Include24hrChange bool
IncludeLastUpdatedAt bool
}
func (p SimplePriceParams) toQuery() (map[string]string, error) {
if len(p.Ids) == 0 || len(p.VsCurrencies) == 0 {
return nil, MissingParameterError
}
return map[string]string{
"ids": strings.Join(p.Ids, ","),
"vs_currencies": strings.Join(p.VsCurrencies, ","),
"include_market_cap": strconv.FormatBool(p.IncludeMarketCap),
"include_24hr_vol": strconv.FormatBool(p.Include24hrVol),
"include_24hr_change": strconv.FormatBool(p.Include24hrChange),
"include_last_updated_at": strconv.FormatBool(p.IncludeLastUpdatedAt),
}, nil
}
type CoinsMarketsParams struct {
VsCurrency string // required usd, eur, jpy, etc
Ids []string
Category string // decentralized_finance_defi, stablecoins
Order string // gecko_desc, gecko_asc, market_cap_asc, market_cap_desc, volume_asc, volume_desc, id_asc, id_desc
PerPage int // max 250
Page int
PriceChangePercentage string // 1h, 24h, 7d, 14d, 30d, 200d, 1y (eg. '1h,24h,7d' comma-separated)
Sparkline bool
}
func (c CoinsMarketsParams) toQuery() (map[string]string, error) {
if len(c.VsCurrency) == 0 {
return nil, MissingParameterError
}
if c.Page < 0 || c.PerPage < 0 {
return nil, InvalidParameterError
}
q := map[string]string{}
q["vs_currency"] = c.VsCurrency
if len(c.Category) > 0 {
q["category"] = c.Category
}
if c.Ids != nil {
if len(c.Ids) > 0 {
q["ids"] = strings.Join(c.Ids, ",")
}
}
if len(c.Order) > 0 {
q["order"] = c.Order
}
if c.PerPage > 0 {
q["per_page"] = strconv.Itoa(c.PerPage)
}
if c.Page > 0 {
q["page"] = strconv.Itoa(c.Page)
}
if len(c.PriceChangePercentage) > 0 {
q["price_change_percentage"] = c.PriceChangePercentage
}
q["sparkline"] = strconv.FormatBool(c.Sparkline)
return q, nil
}
type CoinsDataParams struct {
Id string
//Localization bool
//Tickers bool
//MarketData bool
//CommunityData bool
//DeveloperData bool
//Sparkline bool
}
func (c CoinsDataParams) toQuery() (map[string]string, error) {
if len(c.Id) == 0 {
return nil, MissingParameterError
}
return map[string]string{
//"localization": strconv.FormatBool(c.Localization),
//"tickers": strconv.FormatBool(c.Tickers),
//"market_data": strconv.FormatBool(c.MarketData),
//"community_data": strconv.FormatBool(c.CommunityData),
//"developer_data": strconv.FormatBool(c.DeveloperData),
//"sparkline": strconv.FormatBool(c.Sparkline),
}, nil
}
type CoinsChartsParams struct {
Id string // required
VsCurrency string // required
Days string // required (eg. 1,14,30,max) 5min interval 1 day, 1h interval 1-90days, 1d interval 90+days
}
func (c CoinsChartsParams) toQuery() (map[string]string, error) {
if len(c.Id) == 0 || len(c.VsCurrency) == 0 || len(c.Days) == 0 {
return nil, MissingParameterError
}
return map[string]string{
"vs_currency": c.VsCurrency,
"days": c.Days,
}, nil
}
type CoinsOHLCParams struct {
Id string // required
VsCurrency string // required
Days string // required 1/7/14/30/90/180/365/max, intervals: 1-2d:30m, 3-30d:4h, 31+d:4d
}
func (c CoinsOHLCParams) toQuery() (map[string]string, error) {
if len(c.Id) == 0 || len(c.VsCurrency) == 0 || len(c.Days) == 0 {
return nil, MissingParameterError
}
return map[string]string{
"vs_currency": c.VsCurrency,
"days": c.Days,
}, nil
}
type ExchangesParams struct {
PerPage int // max 250
Page int
}
func (e ExchangesParams) toQuery() (map[string]string, error) {
if e.PerPage < 0 || e.Page < 0 {
return nil, InvalidParameterError
}
return map[string]string{"per_page": strconv.Itoa(e.PerPage), "page": strconv.Itoa(e.Page)}, nil
}