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

     1  package rest
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/derivatives"
    10  )
    11  
    12  // TradeService manages the Trade endpoint.
    13  type StatusService struct {
    14  	requestFactory
    15  	Synchronous
    16  }
    17  
    18  const (
    19  	DERIV_TYPE = "deriv"
    20  )
    21  
    22  func (ss *StatusService) get(sType string, key string) (*derivatives.Snapshot, error) {
    23  	req := NewRequestWithMethod(path.Join("status", sType), "GET")
    24  	req.Params = make(url.Values)
    25  	req.Params.Add("keys", key)
    26  	raw, err := ss.Request(req)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	trueRaw := make([][]interface{}, len(raw))
    31  	for i, r := range raw {
    32  		trueRaw[i] = r.([]interface{})
    33  	}
    34  	s, err := derivatives.SnapshotFromRaw(trueRaw)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return s, nil
    39  }
    40  
    41  // Retrieves derivative status information for the given symbol from the platform
    42  // see https://docs.bitfinex.com/reference#rest-public-status for more info
    43  func (ss *StatusService) DerivativeStatus(symbol string) (*derivatives.DerivativeStatus, error) {
    44  	data, err := ss.get(DERIV_TYPE, symbol)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	if len(data.Snapshot) == 0 {
    49  		return nil, fmt.Errorf("no status found for symbol %s", symbol)
    50  	}
    51  	return data.Snapshot[0], err
    52  }
    53  
    54  // Retrieves derivative status information for the given symbols from the platform
    55  // see https://docs.bitfinex.com/reference#rest-public-status for more info
    56  func (ss *StatusService) DerivativeStatusMulti(symbols []string) ([]*derivatives.DerivativeStatus, error) {
    57  	key := strings.Join(symbols, ",")
    58  	data, err := ss.get(DERIV_TYPE, key)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return data.Snapshot, err
    63  }
    64  
    65  // Retrieves derivative status information for all symbols from the platform
    66  // see https://docs.bitfinex.com/reference#rest-public-status for more info
    67  func (ss *StatusService) DerivativeStatusAll() ([]*derivatives.DerivativeStatus, error) {
    68  	data, err := ss.get(DERIV_TYPE, "ALL")
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	return data.Snapshot, err
    73  }