Skip to content

Commit

Permalink
feat: use options (#32)
Browse files Browse the repository at this point in the history
* update: simple use options

* update: client names,
add: ping

* update: contract use options

* add: gecko api version

* update: assetPlatforms use options

* update: categories use options

* update: exchanges use options

* update: derivatices use options

* fix

* update: nfts use options

* update: exchangeRates use options

* update: search use options

* update: trending use options

* update: global use options

* update: companies use options

* update: move common types to api

* del: tests
  • Loading branch information
JulianToledano authored Dec 30, 2024
1 parent 1ac956e commit 5b3bc45
Show file tree
Hide file tree
Showing 106 changed files with 1,788 additions and 1,519 deletions.
62 changes: 62 additions & 0 deletions api/assetPlatforms/assetPlatforms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package assetPlatforms

import (
"context"
"encoding/json"
"fmt"
"net/url"

"github.com/JulianToledano/goingecko/api"

"github.com/JulianToledano/goingecko/api/assetPlatforms/types"
)

// assetPlatformsOption is an interface that extends api.Option to provide
// asset platform-specific options
type assetPlatformsOption interface {
api.Option

isAssetPlatformsOption()
}

// WithFilter returns an assetPlatformsOption that sets the filter parameter
// for asset platform requests. The filter parameter can be used to filter
// asset platforms by name or ID.
// Current supported filters: [nft]
func WithFilter(filter string) assetPlatformsOption {
return &filterOption{filter}
}

// AssetPlatforms allows you to query all the asset platforms on CoinGecko.
//
// 👍 Tips
//
// You may use this endpoint to query the list of asset platforms for other endpoints that contain params like id orids(asset platforms)
// You may include NFT at the filter params to get the list of NFT-support asset platforms on CoinGecko
func (c *AssetPlatformsClient) AssetPlatforms(ctx context.Context, opts ...assetPlatformsOption) (*types.AssetPlatforms, error) {
params := url.Values{}

for _, opt := range opts {
opt.Apply(&params)
}

rUrl := fmt.Sprintf("%s?%s", c.assetPlatformsUrl(), params.Encode())
resp, err := c.MakeReq(ctx, rUrl)
if err != nil {
return nil, err
}
var data *types.AssetPlatforms
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}

type filterOption struct{ filter string }

func (o filterOption) Apply(v *url.Values) {
v.Set("filter", o.filter)
}

func (o filterOption) isAssetPlatformsOption() {}
22 changes: 22 additions & 0 deletions api/assetPlatforms/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package assetPlatforms

import (
"github.com/JulianToledano/goingecko/api/internal"
geckohttp "github.com/JulianToledano/goingecko/http"
)

type AssetPlatformsClient struct {
*internal.Client
}

func NewClient(c *geckohttp.Client, url string) *AssetPlatformsClient {
return &AssetPlatformsClient{
internal.NewClient(c, url),
}
}

func (c *AssetPlatformsClient) assetPlatformsUrl() string {
return c.URL + "/asset_platforms"
}

// TODO: https://docs.coingecko.com/reference/token-lists
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package assetPlatforms
package types

type AssetPlatforms []Asset

Expand Down
60 changes: 60 additions & 0 deletions api/categories/categories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package categories

import (
"context"
"encoding/json"
"fmt"
"net/url"

"github.com/JulianToledano/goingecko/api"
"github.com/JulianToledano/goingecko/api/categories/types"
)

// categoriesOption is an interface that extends api.Option to provide
// category-specific options
type categoriesOption interface {
api.Option

isCategoryOptions()
}

// WithOrderOption returns a categoriesOption that sets the order parameter
// for category requests. The order parameter can be used to sort categories
// by market cap, volume, etc.
func WithOrderOption(order string) categoriesOption {
return &orderOption{order}
}

// Categories allows you to query all the coins categories with market data (market cap, volume, etc.) on CoinGecko.
//
// 📘Notes
//
// CoinGecko Equivalent Page: https://www.coingecko.com/en/categories
// Cache / Update Frequency: every 5 minutes for all the API plans
// CoinGecko categories are different from GeckoTerminal categories
func (c *CategoriesClient) Categories(ctx context.Context, options ...categoriesOption) (*types.CategoriesWithMarketDataList, error) {
params := url.Values{}

for _, opt := range options {
opt.Apply(&params)
}

rUrl := fmt.Sprintf("%s?%s", c.categoriesUrl(), params.Encode())
resp, err := c.MakeReq(ctx, rUrl)
if err != nil {
return nil, err
}
var data *types.CategoriesWithMarketDataList
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}

type orderOption struct{ order string }

func (o orderOption) Apply(v *url.Values) {
v.Set("order", o.order)
}
func (o orderOption) isCategoryOptions() {}
20 changes: 20 additions & 0 deletions api/categories/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package categories

import (
"github.com/JulianToledano/goingecko/api/internal"
geckohttp "github.com/JulianToledano/goingecko/http"
)

type CategoriesClient struct {
*internal.Client
}

func NewClient(c *geckohttp.Client, url string) *CategoriesClient {
return &CategoriesClient{
internal.NewClient(c, url),
}
}

func (c *CategoriesClient) categoriesUrl() string {
return c.URL + "/categories"
}
33 changes: 33 additions & 0 deletions api/categories/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package categories

import (
"context"
"encoding/json"
"fmt"
"github.com/JulianToledano/goingecko/api/categories/types"
)

// CategoriesList allows you to query all the coins categories on CoinGecko.
//
// 👍 Tips
//
// You may use this endpoint to query the list of categories for other endpoints that contain params like category
//
// 📘 Notes
//
// CoinGecko Equivalent Page: https://www.coingecko.com/en/categories
// Cache / Update Frequency: every 5 minutes for all the API plans
// CoinGecko categories are different from GeckoTerminal categories
func (c *CategoriesClient) CategoriesList(ctx context.Context) (*types.CategoriesList, error) {
rUrl := fmt.Sprintf("%s/%s", c.categoriesUrl(), "list")
resp, err := c.MakeReq(ctx, rUrl)
if err != nil {
return nil, err
}
var data *types.CategoriesList
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package categories
package types

type CategoriesList []Category

Expand Down
44 changes: 41 additions & 3 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ import (
"net/http"

"github.com/JulianToledano/goingecko/api"

"github.com/JulianToledano/goingecko/api/assetPlatforms"
"github.com/JulianToledano/goingecko/api/categories"
"github.com/JulianToledano/goingecko/api/coins"
"github.com/JulianToledano/goingecko/api/companies"
"github.com/JulianToledano/goingecko/api/contract"
"github.com/JulianToledano/goingecko/api/derivatives"
"github.com/JulianToledano/goingecko/api/exchangeRates"
"github.com/JulianToledano/goingecko/api/exchanges"
"github.com/JulianToledano/goingecko/api/global"
"github.com/JulianToledano/goingecko/api/nfts"
"github.com/JulianToledano/goingecko/api/ping"
"github.com/JulianToledano/goingecko/api/search"
"github.com/JulianToledano/goingecko/api/simple"
"github.com/JulianToledano/goingecko/api/trending"
geckohttp "github.com/JulianToledano/goingecko/http"
)

Expand All @@ -25,7 +37,20 @@ func demoApiHeader(apiKey string) func(r *http.Request) {

// Client wraps the CoinGecko API client functionality
type Client struct {
*coins.Client
*ping.PingClient
*simple.SimpleClient
*coins.CoinsClient
*contract.ContractClient
*assetPlatforms.AssetPlatformsClient
*categories.CategoriesClient
*exchanges.ExchangesClient
*derivatives.DerivativesClient
*nfts.NftsClient
*exchangeRates.ExchangeRatesClient
*search.SearchClient
*trending.TrendingClient
*global.GlobalClient
*companies.CompaniesClient

url string
}
Expand Down Expand Up @@ -57,6 +82,19 @@ func NewProApiClient(apiKey string, c *http.Client) *Client {
// newClient creates a new Client with the provided HTTP client and base URL
func newClient(c *geckohttp.Client, url string) *Client {
return &Client{
Client: coins.NewCoinsClient(c, url),
PingClient: ping.NewClient(c, url),
SimpleClient: simple.NewClient(c, url),
CoinsClient: coins.NewClient(c, url),
ContractClient: contract.NewClient(c, url),
AssetPlatformsClient: assetPlatforms.NewClient(c, url),
CategoriesClient: categories.NewClient(c, url),
ExchangesClient: exchanges.NewClient(c, url),
DerivativesClient: derivatives.NewClient(c, url),
NftsClient: nfts.NewClient(c, url),
ExchangeRatesClient: exchangeRates.NewClient(c, url),
SearchClient: search.NewClient(c, url),
TrendingClient: trending.NewClient(c, url),
GlobalClient: global.NewClient(c, url),
CompaniesClient: companies.NewClient(c, url),
}
}
4 changes: 2 additions & 2 deletions api/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

func TestClient_CoinsList(t *testing.T) {
c := NewDefaultClient()

got, err := c.CoinsList(context.Background())
if err != nil {
t.Fatal(err)
}
if got != nil {
if got == nil {
t.Fatal("nil response")
}
}
18 changes: 8 additions & 10 deletions api/coins/client.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package coins

import (
"github.com/JulianToledano/goingecko/api/internal"
geckohttp "github.com/JulianToledano/goingecko/http"
)

type Client struct {
*geckohttp.Client

url string
type CoinsClient struct {
*internal.Client
}

func NewCoinsClient(c *geckohttp.Client, url string) *Client {
return &Client{
c,
url,
func NewClient(c *geckohttp.Client, url string) *CoinsClient {
return &CoinsClient{
internal.NewClient(c, url),
}
}

func (c *Client) coinsUrl() string {
return c.url + "/coins"
func (c *CoinsClient) coinsUrl() string {
return c.URL + "/coins"
}
2 changes: 1 addition & 1 deletion api/coins/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func WithCoinSparkline(sparkline bool) coinsIdOption { return coinSparklineOptio
// Cache/Update Frequency:
// Every 60 seconds for all the API plans
// Community data for Twitter and Telegram will be updated on weekly basis (Reddit community data is no longer supported)
func (c *Client) CoinsId(ctx context.Context, id string, options ...coinsIdOption) (*types.CoinID, error) {
func (c *CoinsClient) CoinsId(ctx context.Context, id string, options ...coinsIdOption) (*types.CoinID, error) {
params := url.Values{}

// Apply all the options
Expand Down
2 changes: 1 addition & 1 deletion api/coins/id_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func WithLocalizationIdHistoryOption(loc bool) idHistoryOption {
//
// The data returned is at 00:00:00 UTC
// The last completed UTC day (00:00) is available 35 minutes after midnight on the next UTC day (00:35)
func (c *Client) CoinsIdHistory(ctx context.Context, id, date string, options ...idHistoryOption) (*types.History, error) {
func (c *CoinsClient) CoinsIdHistory(ctx context.Context, id, date string, options ...idHistoryOption) (*types.History, error) {
params := url.Values{}
params.Set("date", date)

Expand Down
4 changes: 2 additions & 2 deletions api/coins/id_market_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/url"

"github.com/JulianToledano/goingecko/api"
"github.com/JulianToledano/goingecko/types"
"github.com/JulianToledano/goingecko/api/types"
)

// idMarketChartOption is an interface that extends api.Option to provide
Expand Down Expand Up @@ -53,7 +53,7 @@ func WithPrecisionIdMarketChart(precision string) idMarketChartOption {
// Cache / Update Frequency:
// every 30 seconds for all the API plans (for last data point)
// The last completed UTC day (00:00) data is available 10 minutes after midnight on the next UTC day (00:10).
func (c *Client) CoinsIdMarketChart(ctx context.Context, id, vsCurrency, days string, options ...idMarketChartOption) (*types.MarketChart, error) {
func (c *CoinsClient) CoinsIdMarketChart(ctx context.Context, id, vsCurrency, days string, options ...idMarketChartOption) (*types.MarketChart, error) {
params := url.Values{}
params.Add("vs_currency", vsCurrency)
params.Add("days", days)
Expand Down
4 changes: 2 additions & 2 deletions api/coins/id_market_chart_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/url"

"github.com/JulianToledano/goingecko/api"
"github.com/JulianToledano/goingecko/types"
"github.com/JulianToledano/goingecko/api/types"
)

// idMarketChartRangeOption is an interface that extends api.Option to provide
Expand All @@ -30,7 +30,7 @@ func WithPrecisionIdMarketChartRange(precision string) idMarketChartRangeOption
return precisionIdMarketChartRangeOption{precision}
}

func (c *Client) CoinsIdMarketChartRange(ctx context.Context, id, currency, from, to string, options ...idMarketChartRangeOption) (*types.MarketChart, error) {
func (c *CoinsClient) CoinsIdMarketChartRange(ctx context.Context, id, currency, from, to string, options ...idMarketChartRangeOption) (*types.MarketChart, error) {
params := url.Values{}
params.Add("vs_currency", currency)
params.Add("from", from)
Expand Down
2 changes: 1 addition & 1 deletion api/coins/id_tickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func WithDepth(depth string) idTickersOption { return depthOption{depth} }
// The tickers are paginated to 100 items
// Cache / Update Frequency: every 2 minutes for all the API plans
// When order is sorted by 'volume', converted_volume will be used instead of volume
func (c *Client) CoinsIdTickers(ctx context.Context, id string, options ...idTickersOption) (*types.Tickers, error) {
func (c *CoinsClient) CoinsIdTickers(ctx context.Context, id string, options ...idTickersOption) (*types.Tickers, error) {
params := url.Values{}

// Apply all the options
Expand Down
2 changes: 1 addition & 1 deletion api/coins/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func WithStatus(status string) listOption { return statusOption{status: status}
// 📘 Notes
// There is no pagination required for this endpoint
// Cache/Update Frequency: Every 5 minutes for all the API plans
func (c *Client) CoinsList(ctx context.Context, options ...listOption) ([]*types.CoinInfo, error) {
func (c *CoinsClient) CoinsList(ctx context.Context, options ...listOption) ([]*types.CoinInfo, error) {
params := url.Values{}

// Apply all the options
Expand Down
Loading

0 comments on commit 5b3bc45

Please sign in to comment.