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

Commit

Permalink
Implemented /quotes/latest to get prices by symbol and also converted
Browse files Browse the repository at this point in the history
  • Loading branch information
Evandro Zilli Pavei committed Jan 30, 2019
1 parent 975586d commit 514aec7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pro/v1/coinmarketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ type Listing struct {
Quote map[string]*Quote `json:"quote"`
}

// QuoteLatest is the quotes structure
type QuoteLatest struct {
ID float64 `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
CirculatingSupply float64 `json:"circulating_supply"`
TotalSupply float64 `json:"total_supply"`
MaxSupply float64 `json:"max_supply"`
DateAdded string `json:"date_added"`
NumMarketPairs float64 `json:"num_market_pairs"`
CMCRank float64 `json:"cmc_rank"`
LastUpdated string `json:"last_updated"`
Quote map[string]*Quote `json:"quote"`
}

// Quote is the quote structure
type Quote struct {
Price float64 `json:"price"`
Expand Down Expand Up @@ -84,6 +100,12 @@ type CryptocurrencyListingsLatestOptions struct {
Sort string
}

// CryptocurrencyQuotesLatestOptions options
type CryptocurrencyQuotesLatestOptions struct {
Convert string
Symbol string
}

// SortOptions sort options
var SortOptions sortOptions

Expand Down Expand Up @@ -240,6 +262,53 @@ func (s *Client) CryptocurrencyListingsLatest(options *CryptocurrencyListingsLat
return listings, nil
}

// CryptocurrencyQuotesLatest gets latest quote for each specified symbol. Use the "convert" option to return market values in multiple fiat and cryptocurrency conversions in the same call.
func (s *Client) CryptocurrencyQuotesLatest(options *CryptocurrencyQuotesLatestOptions) ([]*QuoteLatest, error) {
var params []string
if options == nil {
options = new(CryptocurrencyQuotesLatestOptions)
}

if options.Symbol != "" {
params = append(params, fmt.Sprintf("symbol=%s", options.Symbol))
}

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

url := fmt.Sprintf("%s/cryptocurrency/quotes/latest?%s", baseURL, strings.Join(params, "&"))

body, err := s.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 quotesLatest []*QuoteLatest
ifcs, ok := resp.Data.(interface{})
if !ok {
return nil, ErrCouldNotCast
}

for _, coinObj := range ifcs.(map[string]interface{}) {
quoteLatest := new(QuoteLatest)
b, err := json.Marshal(coinObj)
if err != nil {
return nil, err
}

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

quotesLatest = append(quotesLatest, quoteLatest)
}
return quotesLatest, nil
}

func init() {
SortOptions = sortOptions{
Name: "name",
Expand Down
27 changes: 27 additions & 0 deletions pro/v1/example/quotes_latest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"log"

cmc ".."
)

func main() {
client := cmc.NewClient(&cmc.Config{
ProAPIKey: "01585d6d-123-456-789-3146576cbc70",
})

quotesLatest, err := client.CryptocurrencyQuotesLatest(&cmc.CryptocurrencyQuotesLatestOptions{
Symbol: "BTC", //add comma and other coin symbol e.g.: "BTC,ETH,XRP"
Convert: "BRL", //add comma and other coin symbol e.g.: "BRL,USD"
})
if err != nil {
log.Fatal(err)
}

for _, quote := range quotesLatest {
fmt.Println(quote.Name) // "Bitcoin"
fmt.Println(quote.Quote["BRL"].Price) // 12880.958109581403 // BTC price converted to BRL
}
}

0 comments on commit 514aec7

Please sign in to comment.