Skip to content

Commit

Permalink
id, currency types to string
Browse files Browse the repository at this point in the history
  • Loading branch information
esenmx committed Jul 25, 2021
1 parent f67e3dd commit 81000bf
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 41 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ implement only the requests I need for my apps, but you can open `issue` for un-
- Dependency-Free
- Required parameters handled before the requests
- Messy payloads simplified, not well organized fields omitted(see also `/coins/{id}`), open `issue` for your needs
- Critical fields will have custom types(see also `ID`, `Currency`)
- `Nullable` fields have pointer types
- `Localization` is not supported

Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Client) do(url string, params QueryParams, ptr interface{}) error {

type api interface {
Ping() (Ping, error)
SimpleSupportedVsCurrencies() ([]Currency, error)
SimpleSupportedVsCurrencies() ([]string, error)
SimplePrice(SimplePriceParams) (SimplePrices, error)
CoinsList(CoinsParams) ([]Coin, error)
CoinsMarkets(CoinsMarketsParams) ([]Market, error)
Expand Down
27 changes: 12 additions & 15 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ type Ping struct {
GeckoSays string `json:"gecko_says"`
}

type ID string
type EID string // Exchange ID
type Currency string
type Coin struct {
Id ID `json:"id"`
Id string `json:"id"`
Symbol string `json:"symbol"`
Name string `json:"name"`
}
Expand All @@ -39,13 +36,13 @@ type Price struct {
}

type SimplePrice struct {
CurrencyPrice map[Currency]Price
CurrencyPrice map[string]Price
LastUpdatedAt *int64
}

type SimplePrices struct {
vsCurrencies []Currency
Prices map[ID]SimplePrice
vsCurrencies []string
Prices map[string]SimplePrice
}

func (r *SimplePrices) UnmarshalJSON(bs []byte) error {
Expand All @@ -57,10 +54,10 @@ func (r *SimplePrices) UnmarshalJSON(bs []byte) error {
if err != nil {
return err
}
r.Prices = make(map[ID]SimplePrice, len(data))
r.Prices = make(map[string]SimplePrice, len(data))
for k, v := range data {
lu := int64(v["last_updated_at"])
ps := SimplePrice{LastUpdatedAt: &lu, CurrencyPrice: make(map[Currency]Price, len(r.vsCurrencies))}
ps := SimplePrice{LastUpdatedAt: &lu, CurrencyPrice: make(map[string]Price, len(r.vsCurrencies))}
for _, vsc := range r.vsCurrencies {
parser := func(k string) *float64 {
if p, ok := v[fmt.Sprintf("%s_%s", vsc, k)]; ok {
Expand All @@ -76,7 +73,7 @@ func (r *SimplePrices) UnmarshalJSON(bs []byte) error {
Change24h: parser("24h_change"),
}
}
r.Prices[ID(k)] = ps
r.Prices[string(k)] = ps
}
return nil
}
Expand All @@ -95,7 +92,7 @@ type Description string
type Platforms map[string]string
type Project struct {
Type string `json:"type"`
Id ID `json:"id"`
Id string `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Image Image `json:"image"`
Expand Down Expand Up @@ -159,9 +156,9 @@ func (r *Platforms) UnmarshalJSON(bs []byte) error {
}

type ROI struct {
Times float64 `json:"times"`
Currency Currency `json:"currency"`
Percentage float64 `json:"percentage"`
Times float64 `json:"times"`
Currency string `json:"currency"`
Percentage float64 `json:"percentage"`
}
type SparklineIn7D struct {
Price []float64 `json:"price"`
Expand Down Expand Up @@ -208,7 +205,7 @@ type Charts struct {
type OHLC [][5]float64

type ExchangeList []struct {
Id EID `json:"id"`
Id string `json:"id"`
Name string `json:"name"`
}

Expand Down
8 changes: 4 additions & 4 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestSimplePrices(t *testing.T) {
var sps SimplePrices
sps.vsCurrencies = []Currency{"usd", "aud"}
sps.vsCurrencies = []string{"usd", "aud"}
err := unmarshalModel("simple_price", &sps)
assert.NoError(t, err)
assert.Equal(t, 2, len(sps.Prices))
Expand All @@ -36,12 +36,12 @@ func TestMarkets(t *testing.T) {
assert.Equal(t, 2, len(ms))
eth := ms[0]
usdt := ms[1]
assert.Equal(t, ID("ethereum"), eth.Id)
assert.Equal(t, ID("tether"), usdt.Id)
assert.Equal(t, "ethereum", eth.Id)
assert.Equal(t, "tether", usdt.Id)
assert.Nil(t, usdt.SparklineIn7D)
assert.Nil(t, usdt.PriceChangePercentage1HInCurrency)
assert.Nil(t, usdt.Roi)
assert.Equal(t, Currency("btc"), eth.Roi.Currency)
assert.Equal(t, "btc", eth.Roi.Currency)
assert.Equal(t, 168, len(eth.SparklineIn7D.Price))
assert.Equal(t, time.Date(2015, 3, 2, 0, 0, 0, 0, time.UTC), usdt.AtlDate)
}
Expand Down
29 changes: 14 additions & 15 deletions parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"strconv"
"strings"
"unsafe"
)

var MissingParameterError = errors.New("missing parameter")
Expand All @@ -23,8 +22,8 @@ func (c CoinsParams) toQuery() (map[string]string, error) {
}

type SimplePriceParams struct {
Ids []ID // required
VsCurrencies []Currency // required
Ids []string // required
VsCurrencies []string // required
IncludeMarketCap bool
Include24hrVol bool
Include24hrChange bool
Expand All @@ -36,8 +35,8 @@ func (p SimplePriceParams) toQuery() (map[string]string, error) {
return nil, MissingParameterError
}
return map[string]string{
"ids": strings.Join(*(*[]string)(unsafe.Pointer(&p.Ids)), ","),
"vs_currencies": strings.Join(*(*[]string)(unsafe.Pointer(&p.VsCurrencies)), ","),
"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),
Expand All @@ -47,8 +46,8 @@ func (p SimplePriceParams) toQuery() (map[string]string, error) {
}

type CoinsMarketsParams struct {
VsCurrency Currency // required usd, eur, jpy, etc
Ids []ID
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
Expand All @@ -71,7 +70,7 @@ func (c CoinsMarketsParams) toQuery() (map[string]string, error) {
}
if c.Ids != nil {
if len(c.Ids) > 0 {
q["ids"] = strings.Join(*(*[]string)(unsafe.Pointer(&c.Ids)), ",")
q["ids"] = strings.Join(c.Ids, ",")
}
}
if len(c.Order) > 0 {
Expand All @@ -91,7 +90,7 @@ func (c CoinsMarketsParams) toQuery() (map[string]string, error) {
}

type CoinsDataParams struct {
Id ID
Id string
//Localization bool
//Tickers bool
//MarketData bool
Expand All @@ -115,9 +114,9 @@ func (c CoinsDataParams) toQuery() (map[string]string, error) {
}

type CoinsChartsParams struct {
Id ID // required
VsCurrency Currency // required
Days string // required (eg. 1,14,30,max) 5min interval 1 day, 1h interval 1-90days, 1d interval 90+days
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) {
Expand All @@ -131,9 +130,9 @@ func (c CoinsChartsParams) toQuery() (map[string]string, error) {
}

type CoinsOHLCParams struct {
Id ID // required
VsCurrency Currency // required
Days string // required 1/7/14/30/90/180/365/max, intervals: 1-2d:30m, 3-30d:4h, 31+d:4d
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) {
Expand Down
4 changes: 2 additions & 2 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func (c *Client) Ping() (Ping, error) {
// Simple
//

func (c *Client) SimpleSupportedVsCurrencies() ([]Currency, error) {
var scs []Currency
func (c *Client) SimpleSupportedVsCurrencies() ([]string, error) {
var scs []string
err := c.do(fmt.Sprintf("%s/simple/supported_vs_currencies", baseURL), nil, &scs)
return scs, err
}
Expand Down
6 changes: 3 additions & 3 deletions requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestClient_SupportedVsCurrencies(t *testing.T) {

func TestClient_SimplePrice(t *testing.T) {
res, err := client.SimplePrice(SimplePriceParams{
Ids: []ID{"polkadot", "solana", "chainlink", "kusama"},
VsCurrencies: []Currency{"usd", "aud"},
Ids: []string{"polkadot", "solana", "chainlink", "kusama"},
VsCurrencies: []string{"usd", "aud"},
})
assert.NoError(t, err)
assert.Equal(t, 4, len(res.Prices))
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestClient_CoinsMarkets(t *testing.T) {
}
})
t.Run("Ids", func(t *testing.T) {
ms, err := client.CoinsMarkets(CoinsMarketsParams{VsCurrency: "usd", Ids: []ID{"polkadot", "solana"}})
ms, err := client.CoinsMarkets(CoinsMarketsParams{VsCurrency: "usd", Ids: []string{"polkadot", "solana"}})
assert.NoError(t, err)
assert.Equal(t, 2, len(ms))
})
Expand Down

0 comments on commit 81000bf

Please sign in to comment.