github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/tickers.go (about)

     1  package rest
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/ticker"
     8  )
     9  
    10  // TickerService manages the Ticker endpoint.
    11  type TickerService struct {
    12  	requestFactory
    13  	Synchronous
    14  }
    15  
    16  func (s *TickerService) getTickers(symbols []string) ([]*ticker.Ticker, error) {
    17  	req := NewRequestWithMethod("tickers", "GET")
    18  	req.Params = make(url.Values)
    19  	req.Params.Add("symbols", strings.Join(symbols, ","))
    20  	raw, err := s.Request(req)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	tickers := make([]*ticker.Ticker, 0)
    26  	for _, traw := range raw {
    27  		t, err := ticker.FromRestRaw(traw.([]interface{}))
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  		tickers = append(tickers, t)
    32  	}
    33  
    34  	return tickers, nil
    35  }
    36  
    37  // Get - retrieves the ticker for the given symbol
    38  // see https://docs.bitfinex.com/reference#rest-public-tickers for more info
    39  func (s *TickerService) Get(symbol string) (*ticker.Ticker, error) {
    40  	t, err := s.getTickers([]string{symbol})
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return t[0], nil
    46  }
    47  
    48  // GetMulti - retrieves the tickers for the given symbols
    49  // see https://docs.bitfinex.com/reference#rest-public-tickers for more info
    50  func (s *TickerService) GetMulti(symbols []string) ([]*ticker.Ticker, error) {
    51  	return s.getTickers(symbols)
    52  }
    53  
    54  // All - retrieves all tickers for all symbols
    55  // see https://docs.bitfinex.com/reference#rest-public-tickers for more info
    56  func (s *TickerService) All() ([]*ticker.Ticker, error) {
    57  	return s.getTickers([]string{"ALL"})
    58  }