support openfigi api key
This commit is contained in:
@@ -13,9 +13,12 @@ import (
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
// OpenFIGI is a small adapter for the openfigi.com api
|
||||
var OpenFIGIAPIKeyHeader = http.CanonicalHeaderKey("X-OPENFIGI-APIKEY")
|
||||
|
||||
// OpenFIGI is a small adapter for the openfigi.com api.
|
||||
type OpenFIGI struct {
|
||||
client *http.Client
|
||||
apiKey string
|
||||
mappingLimiter *rate.Limiter
|
||||
|
||||
mu sync.RWMutex
|
||||
@@ -25,11 +28,18 @@ type OpenFIGI struct {
|
||||
securityTypeCache map[string]string
|
||||
}
|
||||
|
||||
func NewOpenFIGI(c *http.Client) *OpenFIGI {
|
||||
return &OpenFIGI{
|
||||
client: c,
|
||||
mappingLimiter: rate.NewLimiter(rate.Every(time.Minute), 25), // https://www.openfigi.com/api/documentation#rate-limits
|
||||
// NewOpenFIGI creates an OpenFIGI client that uses the API key if provided
|
||||
func NewOpenFIGI(c *http.Client, apiKey string) *OpenFIGI {
|
||||
// Rate limits as per https://www.openfigi.com/api/documentation#rate-limits
|
||||
limiter := rate.NewLimiter(rate.Every(time.Minute), 25)
|
||||
if len(apiKey) > 0 {
|
||||
limiter = rate.NewLimiter(rate.Every(time.Second*6), 25)
|
||||
}
|
||||
|
||||
return &OpenFIGI{
|
||||
client: c,
|
||||
apiKey: apiKey,
|
||||
mappingLimiter: limiter,
|
||||
securityTypeCache: make(map[string]string),
|
||||
}
|
||||
}
|
||||
@@ -71,6 +81,10 @@ func (of *OpenFIGI) SecurityTypeByISIN(ctx context.Context, isin string) (string
|
||||
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
if len(of.apiKey) > 0 {
|
||||
req.Header.Add(OpenFIGIAPIKeyHeader, of.apiKey)
|
||||
}
|
||||
|
||||
err = of.mappingLimiter.Wait(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("wait for mapping request capacity: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user