github.com/polygon-io/client-go@v1.16.4/rest/quotes.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  	ListQuotesPath                    = "/v3/quotes/{ticker}"
    14  	GetLastQuotePath                  = "/v2/last/nbbo/{ticker}"
    15  	GetLastForexQuotePath             = "/v1/last_quote/currencies/{from}/{to}"
    16  	GetRealTimeCurrencyConversionPath = "/v1/conversion/{from}/{to}"
    17  )
    18  
    19  // QuotesClient defines a REST client for the Polygon quotes API.
    20  type QuotesClient struct {
    21  	client.Client
    22  }
    23  
    24  // ListQuotes retrieves quotes for a specified ticker. For more details see
    25  // https://polygon.io/docs/stocks/get_v3_quotes__stockticker.
    26  //
    27  // This method returns an iterator that should be used to access the results via this pattern:
    28  //
    29  //	iter := c.ListQuotes(context.TODO(), params, opts...)
    30  //	for iter.Next() {
    31  //		log.Print(iter.Item()) // do something with the current value
    32  //	}
    33  //	if iter.Err() != nil {
    34  //		return iter.Err()
    35  //	}
    36  func (c *QuotesClient) ListQuotes(ctx context.Context, params *models.ListQuotesParams, options ...models.RequestOption) *iter.Iter[models.Quote] {
    37  	return iter.NewIter(ctx, ListQuotesPath, params, func(uri string) (iter.ListResponse, []models.Quote, error) {
    38  		res := &models.ListQuotesResponse{}
    39  		err := c.CallURL(ctx, http.MethodGet, uri, res, options...)
    40  		return res, res.Results, err
    41  	})
    42  }
    43  
    44  // GetLastQuote retrieves the last quote (NBBO) for a specified ticker. For more details see
    45  // https://polygon.io/docs/stocks/get_v2_last_nbbo__stocksticker.
    46  func (c *QuotesClient) GetLastQuote(ctx context.Context, params *models.GetLastQuoteParams, options ...models.RequestOption) (*models.GetLastQuoteResponse, error) {
    47  	res := &models.GetLastQuoteResponse{}
    48  	err := c.Call(ctx, http.MethodGet, GetLastQuotePath, params, res, options...)
    49  	return res, err
    50  }
    51  
    52  // GetLastForexQuote retrieves the last quote (BBO) for a forex currency pair. For more details see
    53  // https://polygon.io/docs/forex/get_v1_last_quote_currencies__from___to.
    54  func (c *QuotesClient) GetLastForexQuote(ctx context.Context, params *models.GetLastForexQuoteParams, options ...models.RequestOption) (*models.GetLastForexQuoteResponse, error) {
    55  	res := &models.GetLastForexQuoteResponse{}
    56  	err := c.Call(ctx, http.MethodGet, GetLastForexQuotePath, params, res, options...)
    57  	return res, err
    58  }
    59  
    60  // GetRealTimeCurrencyConversion retrieves retrieves currency conversion using the latest market conversion rates. Note
    61  // that you can convert in both directions. For more details see
    62  // https://polygon.io/docs/forex/get_v1_conversion__from___to.
    63  func (c *QuotesClient) GetRealTimeCurrencyConversion(ctx context.Context, params *models.GetRealTimeCurrencyConversionParams, options ...models.RequestOption) (*models.GetRealTimeCurrencyConversionResponse, error) {
    64  	res := &models.GetRealTimeCurrencyConversionResponse{}
    65  	err := c.Call(ctx, http.MethodGet, GetRealTimeCurrencyConversionPath, params, res, options...)
    66  	return res, err
    67  }