Skip to content

Commit

Permalink
add: categories endpoints (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano authored Nov 19, 2023
1 parent 8e31b83 commit ab510ef
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 38 deletions.
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@ Coingecko API client for golang.


## Endpoints
| Endpoint | Status | Function |
|------------------------------------------------------------|--|---|
| /ping || Ping |
| /simple/price || SimplePrice |
| /simple/token_price/{id} || SimpleTokenPrice |
| /simple/supported_vs_currencies || SimpleSupportedVsCurrency |
| /coins/list || CoinsList |
| /coins/markets || CoinsMarket |
| /coins/{id} || CoinsId |
| /coins/{id}/tickers || CoinsIdTickers |
| /coins/{id}/history || CoinsIdHistory |
| /coins/{id}/market_chart || CoinsIdMarketChart |
| /coins/{id}/market_chart/range || CoinsIdMarketChartRange |
| /coins/{id}/ohlc || CoinsOhlc |
| /coins/{id}/contract/{contract_address} || ContractInfo |
| /coins/{id}/contract/{contract_address}/market_chart/ || ContractMarketChart |
| /coins/{id}/contract/{contract_address}/market_chart/range || ContractMarketChartRange |
| /asset_platforms | | AssetPlatforms |
| /coins/categories/list | | |
| /coins/categories/ | | |
| /exchanges || |
| /exchanges/list || |
| /exchanges/{id} || |
| /exchanges/{id}/tickers || |
| /exchanges/{id}/volume_chart || |
| /derivaties || |
| /derivaties/exchanges || |
| /derivaties/exchanges/{id} || |
| /derivaties/exchanges/list || |
| /nfts/list || |
| /nfts/{id} || |
| /nfts/{asset_platform_id}/contract/{contract_address} || |
| /exchange_rates || ExchangeRates |
| /search || |
| /search/trending || |
| /global || Global |
| /global/decentralized_finance_defi || DecentrilizedFinanceDEFI |
| /companies/public_treasury/{coin_id} || |
| Endpoint | Status | Function |
|------------------------------------------------------------|--|-----------------------|
| /ping || Ping |
| /simple/price || SimplePrice |
| /simple/token_price/{id} || SimpleTokenPrice |
| /simple/supported_vs_currencies || SimpleSupportedVsCurrency |
| /coins/list || CoinsList |
| /coins/markets || CoinsMarket |
| /coins/{id} || CoinsId |
| /coins/{id}/tickers || CoinsIdTickers |
| /coins/{id}/history || CoinsIdHistory |
| /coins/{id}/market_chart || CoinsIdMarketChart |
| /coins/{id}/market_chart/range || CoinsIdMarketChartRange |
| /coins/{id}/ohlc || CoinsOhlc |
| /coins/{id}/contract/{contract_address} || ContractInfo |
| /coins/{id}/contract/{contract_address}/market_chart/ || ContractMarketChart |
| /coins/{id}/contract/{contract_address}/market_chart/range || ContractMarketChartRange |
| /asset_platforms | | AssetPlatforms |
| /coins/categories/list | | CategoriesList |
| /coins/categories/ | | Categories |
| /exchanges || |
| /exchanges/list || |
| /exchanges/{id} || |
| /exchanges/{id}/tickers || |
| /exchanges/{id}/volume_chart || |
| /derivaties || |
| /derivaties/exchanges || |
| /derivaties/exchanges/{id} || |
| /derivaties/exchanges/list || |
| /nfts/list || |
| /nfts/{id} || |
| /nfts/{asset_platform_id}/contract/{contract_address} || |
| /exchange_rates || ExchangeRates |
| /search || |
| /search/trending || |
| /global || Global |
| /global/decentralized_finance_defi || DecentrilizedFinanceDEFI |
| /companies/public_treasury/{coin_id} || |

### Beta endpoints
Beta endpoins are to be implemented in a matter of time :smile:
Expand Down
48 changes: 48 additions & 0 deletions categories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package goingecko

import (
"encoding/json"
"fmt"
"github.com/JulianToledano/goingecko/categories"
"net/url"
)

// CategoriesList List all categories
// Cache / Update Frequency: every 5 minutes
func (c *Client) CategoriesList() (*categories.CategoriesList, error) {
rUrl := fmt.Sprintf("%s/%s", categoriesURL, "list")
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data *categories.CategoriesList
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}

// Categories List all categories with market data
// Cache / Update Frequency: every 5 minutes
// Parameters:
// order: valid values: market_cap_desc (default), market_cap_asc, name_desc, name_asc, market_cap_change_24h_desc and market_cap_change_24h_asc
func (c *Client) Categories(order string) (*categories.CategoriesWithMarketDataList, error) {
params := url.Values{}

if order != "" {
params.Add("order", order)
}

rUrl := fmt.Sprintf("%s?%s", categoriesURL, params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}
var data *categories.CategoriesWithMarketDataList
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return data, nil
}
21 changes: 21 additions & 0 deletions categories/categories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package categories

type CategoriesList []Category

type Category struct {
CategoryId string `json:"category_id"`
Name string `json:"name"`
}

type CategoriesWithMarketDataList []CategoryWithMarketData

type CategoryWithMarketData struct {
ID string `json:"id"`
Name string `json:"name"`
MarketCap float64 `json:"market_cap"`
MarketCapChange24h float64 `json:"market_cap_change_24h"`
Content string `json:"content"`
Top3Coins []string `json:"top_3_coins"`
Volume24h float64 `json:"volume_24h"`
UpdatedAt string `json:"updated_at"`
}
1 change: 1 addition & 0 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var (
simpleURL = fmt.Sprintf("%s/simple", baseURL)
coinsURL = fmt.Sprintf("%s/coins", baseURL)
assetPlatformsURL = fmt.Sprintf("%s/asset_platforms", baseURL)
categoriesURL = fmt.Sprintf("%s/categories", coinsURL)
contractURL = fmt.Sprintf("%s/coins", baseURL)
exchangeRatesURL = fmt.Sprintf("%s/exchange_rates", baseURL)
trendingURL = fmt.Sprintf("%s/search/trending", baseURL)
Expand Down
32 changes: 32 additions & 0 deletions test/categories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package test

import (
"github.com/JulianToledano/goingecko"
"testing"
)

func TestCategoriesList(t *testing.T) {

cgClient := goingecko.NewClient(nil)

categoriesList, err := cgClient.CategoriesList()
if categoriesList == nil {
t.Errorf("Error")
}
if err != nil {
t.Errorf("Error: %s", err)
}
}

func TestCategories(t *testing.T) {

cgClient := goingecko.NewClient(nil)

categoriesList, err := cgClient.Categories("market_cap_desc")
if categoriesList == nil {
t.Errorf("Error")
}
if err != nil {
t.Errorf("Error: %s", err)
}
}

0 comments on commit ab510ef

Please sign in to comment.