github.com/polygon-io/client-go@v1.16.4/rest/reference.go (about)

     1  package polygon
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/polygon-io/client-go/rest/client"
     8  	"github.com/polygon-io/client-go/rest/iter"
     9  	"github.com/polygon-io/client-go/rest/models"
    10  )
    11  
    12  const (
    13  	ListTickersPath      = "/v3/reference/tickers"
    14  	GetTickerDetailsPath = "/v3/reference/tickers/{ticker}"
    15  	ListTickerNewsPath   = "/v2/reference/news"
    16  	GetTickerTypesPath   = "/v3/reference/tickers/types"
    17  
    18  	GetMarketHolidaysPath = "/v1/marketstatus/upcoming"
    19  	GetMarketStatusPath   = "/v1/marketstatus/now"
    20  
    21  	ListSplitsPath = "/v3/reference/splits"
    22  
    23  	ListDividendsPath = "/v3/reference/dividends"
    24  
    25  	ListConditionsPath = "/v3/reference/conditions"
    26  
    27  	GetExchangesPath = "/v3/reference/exchanges"
    28  
    29  	GetOptionsContractPath   = "/v3/reference/options/contracts/{ticker}"
    30  	ListOptionsContractsPath = "/v3/reference/options/contracts"
    31  )
    32  
    33  // ReferenceClient defines a REST client for the Polygon reference API.
    34  type ReferenceClient struct {
    35  	client.Client
    36  }
    37  
    38  // ListTickers retrieves reference tickers. For more details see
    39  // https://polygon.io/docs/stocks/get_v3_reference_tickers__ticker.
    40  //
    41  // This method returns an iterator that should be used to access the results via this pattern:
    42  //
    43  //	iter := c.ListTickers(context.TODO(), params, opts...)
    44  //	for iter.Next() {
    45  //		log.Print(iter.Item()) // do something with the current value
    46  //	}
    47  //	if iter.Err() != nil {
    48  //		return iter.Err()
    49  //	}
    50  func (c *ReferenceClient) ListTickers(ctx context.Context, params *models.ListTickersParams, options ...models.RequestOption) *iter.Iter[models.Ticker] {
    51  	return iter.NewIter(ctx, ListTickersPath, params, func(uri string) (iter.ListResponse, []models.Ticker, error) {
    52  		res := &models.ListTickersResponse{}
    53  		err := c.CallURL(ctx, http.MethodGet, uri, res, options...)
    54  		return res, res.Results, err
    55  	})
    56  }
    57  
    58  // GetTickerDetails retrieves details for a specified ticker. For more details see
    59  // https://polygon.io/docs/stocks/get_v3_reference_tickers__ticker.
    60  func (c *ReferenceClient) GetTickerDetails(ctx context.Context, params *models.GetTickerDetailsParams, options ...models.RequestOption) (*models.GetTickerDetailsResponse, error) {
    61  	res := &models.GetTickerDetailsResponse{}
    62  	err := c.Call(ctx, http.MethodGet, GetTickerDetailsPath, params, res, options...)
    63  	return res, err
    64  }
    65  
    66  // ListTickerNews retrieves news articles for a specified ticker. For more details see
    67  // https://polygon.io/docs/stocks/get_v2_reference_news.
    68  //
    69  // This method returns an iterator that should be used to access the results via this pattern:
    70  //
    71  //	iter := c.ListTickerNews(context.TODO(), params, opts...)
    72  //	for iter.Next() {
    73  //		log.Print(iter.Item()) // do something with the current value
    74  //	}
    75  //	if iter.Err() != nil {
    76  //		return iter.Err()
    77  //	}
    78  func (c *ReferenceClient) ListTickerNews(ctx context.Context, params *models.ListTickerNewsParams, options ...models.RequestOption) *iter.Iter[models.TickerNews] {
    79  	return iter.NewIter(ctx, ListTickerNewsPath, params, func(uri string) (iter.ListResponse, []models.TickerNews, error) {
    80  		res := &models.ListTickerNewsResponse{}
    81  		err := c.CallURL(ctx, http.MethodGet, uri, res, options...)
    82  		return res, res.Results, err
    83  	})
    84  }
    85  
    86  // GetTickerTypes retrieves all the possible ticker types that can be queried. For more details see
    87  // https://polygon.io/docs/stocks/get_v3_reference_tickers_types.
    88  func (c *ReferenceClient) GetTickerTypes(ctx context.Context, params *models.GetTickerTypesParams, options ...models.RequestOption) (*models.GetTickerTypesResponse, error) {
    89  	res := &models.GetTickerTypesResponse{}
    90  	err := c.Call(ctx, http.MethodGet, GetTickerTypesPath, params, res, options...)
    91  	return res, err
    92  }
    93  
    94  // GetMarketHolidays retrieves upcoming market holidays and their open/close times. For more details see
    95  // https://polygon.io/docs/stocks/get_v1_marketstatus_upcoming.
    96  func (c *ReferenceClient) GetMarketHolidays(ctx context.Context, options ...models.RequestOption) (*models.GetMarketHolidaysResponse, error) {
    97  	res := &models.GetMarketHolidaysResponse{}
    98  	err := c.CallURL(ctx, http.MethodGet, GetMarketHolidaysPath, res, options...)
    99  	return res, err
   100  }
   101  
   102  // GetMarketStatus retrieves the current trading status of the exchanges and overall financial markets. For more details
   103  // see https://polygon.io/docs/stocks/get_v1_marketstatus_now.
   104  func (c *ReferenceClient) GetMarketStatus(ctx context.Context, options ...models.RequestOption) (*models.GetMarketStatusResponse, error) {
   105  	res := &models.GetMarketStatusResponse{}
   106  	err := c.CallURL(ctx, http.MethodGet, GetMarketStatusPath, res, options...)
   107  	return res, err
   108  }
   109  
   110  // ListSplits retrieves reference splits. For more details see https://polygon.io/docs/stocks/get_v3_reference_splits.
   111  //
   112  // This method returns an iterator that should be used to access the results via this pattern:
   113  //
   114  //	iter := c.ListSplits(context.TODO(), params, opts...)
   115  //	for iter.Next() {
   116  //		log.Print(iter.Item()) // do something with the current value
   117  //	}
   118  //	if iter.Err() != nil {
   119  //		return iter.Err()
   120  //	}
   121  func (c *ReferenceClient) ListSplits(ctx context.Context, params *models.ListSplitsParams, options ...models.RequestOption) *iter.Iter[models.Split] {
   122  	return iter.NewIter(ctx, ListSplitsPath, params, func(uri string) (iter.ListResponse, []models.Split, error) {
   123  		res := &models.ListSplitsResponse{}
   124  		err := c.CallURL(ctx, http.MethodGet, uri, res, options...)
   125  		return res, res.Results, err
   126  	})
   127  }
   128  
   129  // ListDividends retrieves reference dividends. For more details see
   130  // https://polygon.io/docs/stocks/get_v3_reference_dividends.
   131  //
   132  // This method returns an iterator that should be used to access the results via this pattern:
   133  //
   134  //	iter := c.ListDividends(context.TODO(), params, opts...)
   135  //	for iter.Next() {
   136  //		log.Print(iter.Item()) // do something with the current value
   137  //	}
   138  //	if iter.Err() != nil {
   139  //		return iter.Err()
   140  //	}
   141  func (c *ReferenceClient) ListDividends(ctx context.Context, params *models.ListDividendsParams, options ...models.RequestOption) *iter.Iter[models.Dividend] {
   142  	return iter.NewIter(ctx, ListDividendsPath, params, func(uri string) (iter.ListResponse, []models.Dividend, error) {
   143  		res := &models.ListDividendsResponse{}
   144  		err := c.CallURL(ctx, http.MethodGet, uri, res, options...)
   145  		return res, res.Results, err
   146  	})
   147  }
   148  
   149  // ListConditions retrieves reference conditions. For more details see
   150  // https://polygon.io/docs/stocks/get_v3_reference_conditions.
   151  //
   152  // This method returns an iterator that should be used to access the results via this pattern:
   153  //
   154  //	iter := c.ListConditions(context.TODO(), params, opts...)
   155  //	for iter.Next() {
   156  //		log.Print(iter.Item()) // do something with the current value
   157  //	}
   158  //	if iter.Err() != nil {
   159  //		return iter.Err()
   160  //	}
   161  func (c *ReferenceClient) ListConditions(ctx context.Context, params *models.ListConditionsParams, options ...models.RequestOption) *iter.Iter[models.Condition] {
   162  	return iter.NewIter(ctx, ListConditionsPath, params, func(uri string) (iter.ListResponse, []models.Condition, error) {
   163  		res := &models.ListConditionsResponse{}
   164  		err := c.CallURL(ctx, http.MethodGet, uri, res, options...)
   165  		return res, res.Results, err
   166  	})
   167  }
   168  
   169  // GetExchanges lists all exchanges that Polygon knows about. For more details see
   170  // https://polygon.io/docs/stocks/get_v3_reference_exchanges.
   171  func (c *ReferenceClient) GetExchanges(ctx context.Context, params *models.GetExchangesParams, options ...models.RequestOption) (*models.GetExchangesResponse, error) {
   172  	res := &models.GetExchangesResponse{}
   173  	err := c.Call(ctx, http.MethodGet, GetExchangesPath, params, res, options...)
   174  	return res, err
   175  }
   176  
   177  // GetOptionsContract retrieves a historical options contract. For more details see
   178  // https://polygon.io/docs/options/get_v3_reference_options_contracts__options_ticker.
   179  func (c *ReferenceClient) GetOptionsContract(ctx context.Context, params *models.GetOptionsContractParams, options ...models.RequestOption) (*models.GetOptionsContractResponse, error) {
   180  	res := &models.GetOptionsContractResponse{}
   181  	err := c.Call(ctx, http.MethodGet, GetOptionsContractPath, params, res, options...)
   182  	return res, err
   183  }
   184  
   185  // ListOptionsContracts lists historical options contracts. For more details see
   186  // https://polygon.io/docs/options/get_v3_reference_options_contracts.
   187  //
   188  // This method returns an iterator that should be used to access the results via this pattern:
   189  //
   190  //	iter := c.ListOptionsContracts(context.TODO(), params, opts...)
   191  //	for iter.Next() {
   192  //		log.Print(iter.Item()) // do something with the current value
   193  //	}
   194  //	if iter.Err() != nil {
   195  //		return iter.Err()
   196  //	}
   197  func (c *ReferenceClient) ListOptionsContracts(ctx context.Context, params *models.ListOptionsContractsParams, options ...models.RequestOption) *iter.Iter[models.OptionsContract] {
   198  	return iter.NewIter(ctx, ListOptionsContractsPath, params, func(uri string) (iter.ListResponse, []models.OptionsContract, error) {
   199  		res := &models.ListOptionsContractsResponse{}
   200  		err := c.CallURL(ctx, http.MethodGet, uri, res, options...)
   201  		return res, res.Results, err
   202  	})
   203  }