Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed wrong API version in URLs and added APIVersion as client option. #320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions alpaca/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
type ClientOpts struct {
APIKey string
APISecret string
APIVersion string
BrokerKey string
BrokerSecret string
OAuth string
Expand Down
16 changes: 10 additions & 6 deletions marketdata/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type ClientOpts struct {
APIKey string
APISecret string
APIVersion string
BrokerKey string
BrokerSecret string
OAuth string
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
}
Expand Down