Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Fiat endpoint implementation with fixes #6

Merged
merged 4 commits into from
Aug 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 95 additions & 13 deletions pro/v1/coinmarketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Client struct {
proAPIKey string
Cryptocurrency *CryptocurrencyService
Fiat *FiatService
Exchange *ExchangeService
GlobalMetrics *GlobalMetricsService
Tools *ToolsService
Expand All @@ -27,6 +28,9 @@ type Config struct {
// CryptocurrencyService ...
type CryptocurrencyService service

// FiatService ...
type FiatService service

// ExchangeService ...
type ExchangeService service

Expand Down Expand Up @@ -68,15 +72,30 @@ type Listing struct {
}

// MapListing is the structure of a map listing
type Platform struct {
ID int `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
TokenAddress string `json:"token_address"`
}
type MapListing struct {
ID float64 `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
IsActive int `json:"is_active"`
FirstHistoricalData string `json:"first_historical_data"`
LastHistoricalData string `json:"last_historical_data"`
Platform *string
ID float64 `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
IsActive int `json:"is_active"`
FirstHistoricalData string `json:"first_historical_data"`
LastHistoricalData string `json:"last_historical_data"`
Platform Platform `json:"platform"`
}

// FiatMapListing is the structure of a fiat map listing
type FiatMapListing struct {
ID float64 `json:"id"`
Name string `json:"name"`
Sign string `json:"sign"`
Symbol string `json:"symbol"`
}

// ConvertListing is the converted listing structure
Expand Down Expand Up @@ -175,6 +194,13 @@ type MapOptions struct {
Symbol string
}

// FiatMapOptions options
type FiatMapOptions struct {
Start int
Limit int
IncludeMetals bool
}

// QuoteOptions options
type QuoteOptions struct {
// Covert suppots multiple currencies command separated. eg. "BRL,USD"
Expand All @@ -185,11 +211,12 @@ type QuoteOptions struct {

// ConvertOptions options
type ConvertOptions struct {
Amount float64
ID string
Symbol string
Time int
Convert string
Amount float64
ID string
Symbol string
Time int
Convert string
ConvertID string
}

// MarketPairOptions options
Expand Down Expand Up @@ -254,6 +281,7 @@ func NewClient(cfg *Config) *Client {

c.common.client = c
c.Cryptocurrency = (*CryptocurrencyService)(&c.common)
c.Fiat = (*FiatService)(&c.common)
c.Exchange = (*ExchangeService)(&c.common)
c.GlobalMetrics = (*GlobalMetricsService)(&c.common)
c.Tools = (*ToolsService)(&c.common)
Expand Down Expand Up @@ -415,6 +443,58 @@ func (s *CryptocurrencyService) Map(options *MapOptions) ([]*MapListing, error)
return result, nil
}

// Map returns a paginated list of all cryptocurrencies by CoinMarketCap ID.
func (s *FiatService) Map(options *FiatMapOptions) ([]*FiatMapListing, error) {
var params []string
if options == nil {
options = new(FiatMapOptions)
}

if options.Start != 0 {
params = append(params, fmt.Sprintf("start=%d", options.Start))
}

if options.Limit != 0 {
params = append(params, fmt.Sprintf("limit=%d", options.Limit))
}

if options.IncludeMetals {
params = append(params, "include_metals=true")
}

url := fmt.Sprintf("%s/fiat/map?%s", baseURL, strings.Join(params, "&"))

body, err := s.client.makeReq(url)
resp := new(Response)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, fmt.Errorf("JSON Error: [%s]. Response body: [%s]", err.Error(), string(body))
}

var result []*FiatMapListing
ifcs, ok := resp.Data.(interface{})
if !ok {
return nil, ErrTypeAssertion
}

for _, item := range ifcs.([]interface{}) {
value := new(FiatMapListing)
b, err := json.Marshal(item)
if err != nil {
return nil, err
}

err = json.Unmarshal(b, value)
if err != nil {
return nil, err
}

result = append(result, value)
}

return result, nil
}

// Exchange ...
type Exchange struct {
ID int `json:"id"`
Expand Down Expand Up @@ -702,6 +782,8 @@ func (s *ToolsService) PriceConversion(options *ConvertOptions) (*ConvertListing

if options.Convert != "" {
params = append(params, fmt.Sprintf("convert=%s", options.Convert))
} else if options.ConvertID != "" {
params = append(params, fmt.Sprintf("convert_id=%s", options.ConvertID))
}

url := fmt.Sprintf("%s/tools/price-conversion?%s", baseURL, strings.Join(params, "&"))
Expand Down