github.com/polygon-io/client-go@v1.16.4/websocket/config.go (about)

     1  package polygonws
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  // Config is a set of WebSocket client options.
     8  type Config struct {
     9  	// APIKey is the API key used to authenticate against the server.
    10  	APIKey string
    11  
    12  	// Feed is the data feed (e.g. Delayed, RealTime) which represents the server host.
    13  	Feed Feed
    14  
    15  	// Market is the type of market (e.g. Stocks, Crypto) used to connect to the server.
    16  	Market Market
    17  
    18  	// MaxRetries is the maximum number of retry attempts that will occur. If the maximum
    19  	// is reached, the client will close the connection. Omitting this will cause the
    20  	// client to reconnect indefinitely until the user closes it.
    21  	MaxRetries *uint64
    22  
    23  	// RawData is a flag indicating whether data should be returned as a raw JSON or raw bytes. If BypassRawDataRouting is unset
    24  	// then the data will be returned as raw JSON, otherwise it will be raw bytes.
    25  	RawData bool
    26  
    27  	// BypassRawDataRouting is a flag that interacts with the RawData flag. If RawData flag is unset then this flag is ignored.
    28  	// If RawData is `true`, then this flag indicates whether the raw data should be parsed as json.RawMessage
    29  	// and routed via the client's internal logic (`BypassRawDataRouting=false`), or returned to the application code as []byte (`BypassRawDataRouting=true`).
    30  	// If this flag is `true`, it's up to the caller to handle all message types including auth and subscription responses.
    31  	BypassRawDataRouting bool
    32  
    33  	// Log is an optional logger. Any logger implementation can be used as long as it
    34  	// implements the basic Logger interface. Omitting this will disable client logging.
    35  	Log Logger
    36  }
    37  
    38  func (c *Config) validate() error {
    39  	if c.APIKey == "" {
    40  		return errors.New("API key is required")
    41  	}
    42  
    43  	if c.Log == nil {
    44  		c.Log = &nopLogger{}
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  // Feed is the data feed (e.g. Delayed, RealTime) which represents the server host.
    51  type Feed string
    52  
    53  const (
    54  	Delayed                           Feed = "wss://delayed.polygon.io"
    55  	RealTime                          Feed = "wss://socket.polygon.io"
    56  	Nasdaq                            Feed = "wss://nasdaqfeed.polygon.io"
    57  	PolyFeed                          Feed = "wss://polyfeed.polygon.io"
    58  	PolyFeedPlus                      Feed = "wss://polyfeedplus.polygon.io"
    59  	StarterFeed                       Feed = "wss://starterfeed.polygon.io"
    60  	LaunchpadFeed                     Feed = "wss://launchpad.polygon.io"
    61  	BusinessFeed                      Feed = "wss://business.polygon.io"
    62  	EdgxBusinessFeed                  Feed = "wss://edgx-business.polygon.io"
    63  	DelayedBusinessFeed               Feed = "wss://delayed-business.polygon.io"
    64  	DelayedEdgxBusinessFeed           Feed = "wss://delayed-edgx-business.polygon.io"
    65  	DelayedNasdaqLastSaleBusinessFeed Feed = "wss://delayed-nasdaq-last-sale-business.polygon.io"
    66  	DelayedNasdaqBasicFeed            Feed = "wss://delayed-nasdaq-basic-business.polygon.io"
    67  	DelayedFullMarketBusinessFeed     Feed = "wss://delayed-fullmarket-business.polygon.io"
    68  	FullMarketBusinessFeed            Feed = "wss://fullmarket-business.polygon.io"
    69  	NasdaqLastSaleBusinessFeed        Feed = "wss://nasdaq-last-sale-business.polygon.io"
    70  	NasdaqBasicBusinessFeed           Feed = "wss://nasdaq-basic-business.polygon.io"
    71  )
    72  
    73  // Market is the type of market (e.g. Stocks, Crypto) used to connect to the server.
    74  type Market string
    75  
    76  const (
    77  	Stocks  Market = "stocks"
    78  	Options Market = "options"
    79  	Forex   Market = "forex"
    80  	Crypto  Market = "crypto"
    81  	Indices Market = "indices"
    82  )
    83  
    84  func (m Market) supports(topic Topic) bool {
    85  	// FMV is supported for Stocks, Options, Forex, and Crypto but is not within the range
    86  	// so we need to make sure FMV is suppored if these markets are used.
    87  	isBusinessFairMarketValue := topic == BusinessFairMarketValue
    88  
    89  	switch m {
    90  	case Stocks:
    91  		return isBusinessFairMarketValue || (topic > stocksMin && topic < stocksMax)
    92  	case Options:
    93  		return isBusinessFairMarketValue || (topic > optionsMin && topic < optionsMax)
    94  	case Forex:
    95  		return isBusinessFairMarketValue || (topic > forexMin && topic < forexMax)
    96  	case Crypto:
    97  		return isBusinessFairMarketValue || (topic > cryptoMin && topic < cryptoMax)
    98  	}
    99  
   100  	return true // assume user knows what they're doing if they use some unknown market
   101  }
   102  
   103  // Topic is the data type used to subscribe and retrieve data from the server.
   104  type Topic uint8
   105  
   106  // The launchpad topics should be used for any asset class when connecting to
   107  // the Launchpad feed
   108  const (
   109  	stocksMin              Topic = 10
   110  	StocksSecAggs          Topic = 11
   111  	StocksMinAggs          Topic = 12
   112  	StocksTrades           Topic = 13
   113  	StocksQuotes           Topic = 14
   114  	StocksImbalances       Topic = 15
   115  	StocksLULD             Topic = 16
   116  	StocksLaunchpadMinAggs Topic = 17
   117  	StocksLaunchpadValue   Topic = 18
   118  	stocksMax              Topic = 19
   119  
   120  	optionsMin              Topic = 30
   121  	OptionsSecAggs          Topic = 31
   122  	OptionsMinAggs          Topic = 32
   123  	OptionsTrades           Topic = 33
   124  	OptionsQuotes           Topic = 34
   125  	OptionsLaunchpadMinAggs Topic = 35
   126  	OptionsLaunchpadValue   Topic = 36
   127  	optionsMax              Topic = 37
   128  
   129  	forexMin              Topic = 50
   130  	ForexSecAggs          Topic = 51
   131  	ForexMinAggs          Topic = 52
   132  	ForexQuotes           Topic = 53
   133  	ForexLaunchpadMinAggs Topic = 54
   134  	ForexLaunchpadValue   Topic = 55
   135  	forexMax              Topic = 56
   136  
   137  	cryptoMin              Topic = 70
   138  	CryptoSecAggs          Topic = 71
   139  	CryptoMinAggs          Topic = 72
   140  	CryptoTrades           Topic = 73
   141  	CryptoQuotes           Topic = 74
   142  	CryptoL2Book           Topic = 75
   143  	CryptoLaunchpadMinAggs Topic = 76
   144  	CryptoLaunchpadValue   Topic = 77
   145  	cryptoMax              Topic = 78
   146  
   147  	IndexSecAggs Topic = 90
   148  	IndexMinAggs Topic = 91
   149  	IndexValue   Topic = 92
   150  
   151  	BusinessFairMarketValue Topic = 100
   152  )
   153  
   154  func (t Topic) prefix() string {
   155  	switch t {
   156  	case StocksSecAggs:
   157  		return "A"
   158  	case StocksMinAggs:
   159  		return "AM"
   160  	case StocksTrades:
   161  		return "T"
   162  	case StocksQuotes:
   163  		return "Q"
   164  	case StocksImbalances:
   165  		return "NOI"
   166  	case StocksLULD:
   167  		return "LULD"
   168  	case StocksLaunchpadMinAggs:
   169  		return "AM"
   170  	case StocksLaunchpadValue:
   171  		return "LV"
   172  	case OptionsSecAggs:
   173  		return "A"
   174  	case OptionsMinAggs:
   175  		return "AM"
   176  	case OptionsTrades:
   177  		return "T"
   178  	case OptionsQuotes:
   179  		return "Q"
   180  	case OptionsLaunchpadMinAggs:
   181  		return "AM"
   182  	case OptionsLaunchpadValue:
   183  		return "LV"
   184  	case ForexSecAggs:
   185  		return "CAS"
   186  	case ForexMinAggs:
   187  		return "CA"
   188  	case ForexQuotes:
   189  		return "C"
   190  	case ForexLaunchpadMinAggs:
   191  		return "AM"
   192  	case ForexLaunchpadValue:
   193  		return "LV"
   194  	case CryptoSecAggs:
   195  		return "XAS"
   196  	case CryptoMinAggs:
   197  		return "XA"
   198  	case CryptoTrades:
   199  		return "XT"
   200  	case CryptoQuotes:
   201  		return "XQ"
   202  	case CryptoL2Book:
   203  		return "XL2"
   204  	case CryptoLaunchpadMinAggs:
   205  		return "AM"
   206  	case CryptoLaunchpadValue:
   207  		return "LV"
   208  	case IndexSecAggs:
   209  		return "A"
   210  	case IndexMinAggs:
   211  		return "AM"
   212  	case IndexValue:
   213  		return "V"
   214  	case BusinessFairMarketValue:
   215  		return "FMV"
   216  	}
   217  	return ""
   218  }
   219  
   220  // Logger is a basic logger interface used for logging within the client.
   221  type Logger interface {
   222  	Debugf(template string, args ...any)
   223  	Infof(template string, args ...any)
   224  	Errorf(template string, args ...any)
   225  }
   226  
   227  type nopLogger struct{}
   228  
   229  func (l *nopLogger) Debugf(template string, args ...any) {}
   230  func (l *nopLogger) Infof(template string, args ...any)  {}
   231  func (l *nopLogger) Errorf(template string, args ...any) {}