diff --git a/README.md b/README.md index 0738af2..611807b 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,10 @@ func main() { client := alpaca.NewClient(alpaca.ClientOpts{ // Alternatively you can set your key and secret using the // APCA_API_KEY_ID and APCA_API_SECRET_KEY environment variables - APIKey: "YOUR_API_KEY", - APISecret: "YOUR_API_SECRET", - BaseURL: "https://paper-api.alpaca.markets", + APIKey: "YOUR_API_KEY", + APISecret: "YOUR_API_SECRET", + APIVersion: "v1beta1", + BaseURL: "https://paper-api.alpaca.markets", }) acct, err := client.GetAccount() if err != nil { diff --git a/alpaca/rest.go b/alpaca/rest.go index 24b782d..ed4dc5a 100644 --- a/alpaca/rest.go +++ b/alpaca/rest.go @@ -22,6 +22,7 @@ import ( type ClientOpts struct { APIKey string APISecret string + APIVersion string BrokerKey string BrokerSecret string OAuth string diff --git a/marketdata/rest.go b/marketdata/rest.go index 3607220..a4c9bda 100644 --- a/marketdata/rest.go +++ b/marketdata/rest.go @@ -25,6 +25,7 @@ import ( type ClientOpts struct { APIKey string APISecret string + APIVersion string BrokerKey string BrokerSecret string OAuth string @@ -60,6 +61,9 @@ func NewClient(opts ClientOpts) *Client { if opts.APISecret == "" { opts.APISecret = os.Getenv("APCA_API_SECRET_KEY") } + if opts.APIVersion == "" { + opts.APIVersion = os.Getenv("APCA_API_VERSION") + } if opts.OAuth == "" { opts.OAuth = os.Getenv("APCA_API_OAUTH") } @@ -718,8 +722,8 @@ func (c *Client) GetSnapshots(symbols []string, req GetSnapshotRequest) (map[str return snapshots, nil } -const cryptoPrefix = "v1beta3/crypto" -const cryptoPerpPrefix = "v1beta1/crypto-perps" +const cryptoPrefix = "%s/crypto" +const cryptoPerpPrefix = "%s/crypto-perps" type cryptoBaseRequest struct { Symbols []string @@ -939,7 +943,7 @@ func (c *Client) GetCryptoBars(symbol string, req GetCryptoBarsRequest) ([]Crypt // GetCryptoMultiBars returns bars for the given crypto symbols. func (c *Client) GetCryptoMultiBars(symbols []string, req GetCryptoBarsRequest) (map[string][]CryptoBar, error) { u, err := url.Parse(fmt.Sprintf("%s/%s/%s/bars", - c.opts.BaseURL, cryptoPrefix, c.cryptoFeed(req.CryptoFeed))) + c.opts.BaseURL, fmt.Sprintf(cryptoPrefix, c.opts.APIVersion), c.cryptoFeed(req.CryptoFeed))) if err != nil { return nil, err } @@ -1010,9 +1014,9 @@ type cryptoRequest interface { } func (c *Client) cryptoURL(fromReq cryptoRequest) string { - prefix := cryptoPrefix + prefix := fmt.Sprintf(cryptoPrefix, c.opts.APIVersion) if fromReq.isPerp() { - prefix = cryptoPerpPrefix + prefix = fmt.Sprintf(cryptoPerpPrefix, c.opts.APIVersion) } feed := fromReq.cryptoFeed() if feed == "" { @@ -1499,7 +1503,7 @@ type GetCorporateActionsRequest struct { // GetCorporateActions returns the corporate actions based on the given req. func (c *Client) GetCorporateActions(req GetCorporateActionsRequest) (CorporateActions, error) { - u, err := url.Parse(fmt.Sprintf("%s/v1beta1/corporate-actions", c.opts.BaseURL)) + u, err := url.Parse(fmt.Sprintf("%s/%s/corporate-actions", c.opts.APIVersion, c.opts.BaseURL)) if err != nil { return CorporateActions{}, err }