Skip to content

Commit

Permalink
feat(alpacabkfeeder): add AuthMethod config (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakimura authored Sep 8, 2022
1 parent 44a9255 commit d2880b0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
4 changes: 4 additions & 0 deletions contrib/alpacabkfeeder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ bgworkers:
# environment variables.
api_key_id: "foobar"
api_secret_key: "fizzbuzz"
# auth method for the Alpaca API. "basic" (default value. basic authentication is used.)
# or "header" ("APCA-API-KEY-ID" and "APCA-API-SECRET-KEY" HTTP headers are used).
# This config can be manually overridden by "APCA_API_AUTH_METHOD" environmental variable.
#auth_method: "basic"
# Timeout: Due to the restriction of the Alpaca API Client library used in this plugin,
# Alpaca API Client timeout can be updated only by "APCA_API_CLIENT_TIMEOUT" environmental variable.
# Please set the env var to a duration string is a possibly signed sequence of
Expand Down
1 change: 1 addition & 0 deletions contrib/alpacabkfeeder/alpacav2.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func apiClient(config *configs.DefaultConfig) *api.Client {
PolygonKeyID: config.APIKeyID,
Secret: config.APISecretKey,
// OAuth: os.Getenv(EnvApiOAuth),
AuthMethod: api.AuthMethodFromString(config.AuthMethod),
}
if config.APIKeyID == "" || config.APISecretKey == "" {
// if empty, get from env vars
Expand Down
16 changes: 11 additions & 5 deletions contrib/alpacabkfeeder/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func defaultDo(c *Client, req *http.Request) (*http.Response, error) {
if c.credentials.OAuth != "" {
req.Header.Set("Authorization", "Bearer "+c.credentials.OAuth)
} else {
if strings.Contains(req.URL.String(), "sandbox") {
// Add Basic Auth
req.SetBasicAuth(c.credentials.ID, c.credentials.Secret)
} else {
if c.credentials.AuthMethod == HeaderAuth {
req.Header.Set("APCA-API-KEY-ID", c.credentials.ID)
req.Header.Set("APCA-API-SECRET-KEY", c.credentials.Secret)
} else {
// default: Basic Auth
req.SetBasicAuth(c.credentials.ID, c.credentials.Secret)
}
}

Expand Down Expand Up @@ -117,6 +117,13 @@ type Client struct {
credentials *APIKey
}

type AuthMethod int

const (
BasicAuth = iota
HeaderAuth
)

func SetBaseUrl(baseUrl string) {
base = baseUrl
}
Expand Down Expand Up @@ -292,7 +299,6 @@ func (c *Client) ListAssets(status *string) ([]v1.Asset, error) {
}

u.RawQuery = q.Encode()

resp, err := c.get(u)
if err != nil {
return nil, err
Expand Down
19 changes: 18 additions & 1 deletion contrib/alpacabkfeeder/api/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
const (
EnvApiKeyID = "APCA_API_KEY_ID"
EnvApiSecretKey = "APCA_API_SECRET_KEY"
EnvAuthMethod = "APCA_API_AUTH_METHOD"
EnvApiOAuth = "APCA_API_OAUTH"
EnvPolygonKeyID = "POLY_API_KEY_ID"
)
Expand All @@ -22,6 +23,7 @@ type APIKey struct {
Secret string
OAuth string
PolygonKeyID string
AuthMethod AuthMethod
}

// Credentials returns the user's Alpaca API key ID
Expand All @@ -33,10 +35,25 @@ func Credentials() *APIKey {
} else {
polygonKeyID = os.Getenv(EnvApiKeyID)
}
return &APIKey{
apiKey := &APIKey{
ID: os.Getenv(EnvApiKeyID),
PolygonKeyID: polygonKeyID,
Secret: os.Getenv(EnvApiSecretKey),
OAuth: os.Getenv(EnvApiOAuth),
}
if am := os.Getenv(EnvAuthMethod); am != "" {
apiKey.AuthMethod = AuthMethodFromString(am)
}
return apiKey
}

func AuthMethodFromString(s string) AuthMethod {
switch s {
case "basic":
return BasicAuth
case "header":
return HeaderAuth
default:
return BasicAuth
}
}
1 change: 1 addition & 0 deletions contrib/alpacabkfeeder/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type DefaultConfig struct {
Timeframe string `json:"timeframe"`
APIKeyID string `json:"api_key_id"`
APISecretKey string `json:"api_secret_key"`
AuthMethod string `json:"auth_method"`
OpenHourNY, OpenMinuteNY int
CloseHourNY, CloseMinuteNY int
ExtendedHours bool `json:"extended_hours"`
Expand Down
3 changes: 2 additions & 1 deletion contrib/alpacabkfeeder/feed/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (w *Worker) tryPrintErr() {
}()
}

// try calls GetQuotes endpoint of Alpaca API,
// try calls GetSnapshots endpoint of Alpaca API,
// convert the API response to a ColumnSeriesMap and write it to the marketstore.
func (w *Worker) try() error {
// check if it needs to work now
Expand All @@ -66,6 +66,7 @@ func (w *Worker) try() error {
len(symbls), err,
)
}
log.Info("successfully got snapshot data from Alpaca API. len(symbols)=%v", len(symbls))

// write SnapShot data
err = w.SnapshotWriter.Write(snapshots)
Expand Down

0 comments on commit d2880b0

Please sign in to comment.