From 3769db0ab69e6f2f1487d19eeb3ac3dbccd80374 Mon Sep 17 00:00:00 2001
From: Cesar Schneider <cesschneider@gmail.com>
Date: Tue, 14 Jan 2025 16:28:24 +0100
Subject: [PATCH] Fixed wrong API version on URLs and adds APIVersion as client
 option.

---
 README.md          |  7 ++++---
 alpaca/rest.go     |  1 +
 marketdata/rest.go | 16 ++++++++++------
 3 files changed, 15 insertions(+), 9 deletions(-)

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
 	}