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)
|
||||
|
||||
@@ -110,7 +110,7 @@ func TestOpenFIGI_SecurityTypeByISIN(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
of := internal.NewOpenFIGI(tt.client)
|
||||
of := internal.NewOpenFIGI(tt.client, "")
|
||||
|
||||
got, gotErr := of.SecurityTypeByISIN(context.Background(), tt.isin)
|
||||
if gotErr != nil {
|
||||
@@ -145,7 +145,7 @@ func TestOpenFIGI_SecurityTypeByISIN_Cache(t *testing.T) {
|
||||
}, nil
|
||||
})
|
||||
|
||||
of := internal.NewOpenFIGI(c)
|
||||
of := internal.NewOpenFIGI(c, "")
|
||||
|
||||
got, gotErr := of.SecurityTypeByISIN(t.Context(), "NL0000235190")
|
||||
if gotErr != nil {
|
||||
@@ -166,6 +166,55 @@ func TestOpenFIGI_SecurityTypeByISIN_Cache(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenFIGI_SecurityTypeByISIN_APIKey(t *testing.T) {
|
||||
t.Run("with API key", func(t *testing.T) {
|
||||
wantAPIKey := "123abc-456xyz"
|
||||
|
||||
c := NewTestClient(t, func(req *http.Request) (*http.Response, error) {
|
||||
value, ok := req.Header[internal.OpenFIGIAPIKeyHeader]
|
||||
if !ok {
|
||||
t.Fatalf("want %q header but got none: %v", internal.OpenFIGIAPIKeyHeader, req.Header)
|
||||
}
|
||||
if len(value) != 1 {
|
||||
t.Fatalf("want exactly one %q header value but got %d", internal.OpenFIGIAPIKeyHeader, len(value))
|
||||
}
|
||||
if value[0] != wantAPIKey {
|
||||
t.Fatalf("want %q header value %q but got %q", internal.OpenFIGIAPIKeyHeader, wantAPIKey, value[0])
|
||||
}
|
||||
return &http.Response{
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewBufferString(`[{"data":[{"securityType":"Common Stock"}]}]`)),
|
||||
}, nil
|
||||
})
|
||||
of := internal.NewOpenFIGI(c, wantAPIKey)
|
||||
|
||||
_, err := of.SecurityTypeByISIN(t.Context(), "US1234567890")
|
||||
if err != nil {
|
||||
t.Fatalf("want success but got an error: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("without API key", func(t *testing.T) {
|
||||
c := NewTestClient(t, func(req *http.Request) (*http.Response, error) {
|
||||
_, ok := req.Header[internal.OpenFIGIAPIKeyHeader]
|
||||
if ok {
|
||||
t.Fatalf("want no %s header but got one", internal.OpenFIGIAPIKeyHeader)
|
||||
}
|
||||
return &http.Response{
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewBufferString(`[{"data":[{"securityType":"Common Stock"}]}]`)),
|
||||
}, nil
|
||||
})
|
||||
of := internal.NewOpenFIGI(c, "")
|
||||
_, err := of.SecurityTypeByISIN(t.Context(), "US1234567890")
|
||||
if err != nil {
|
||||
t.Fatalf("want success but got an error: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type RoundTripFunc func(req *http.Request) (*http.Response, error)
|
||||
|
||||
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
|
||||
@@ -223,7 +223,7 @@ func NewFigiClientSecurityTypeStub(t testing.TB, securityType string) *internal.
|
||||
}),
|
||||
}
|
||||
|
||||
return internal.NewOpenFIGI(c)
|
||||
return internal.NewOpenFIGI(c, "")
|
||||
}
|
||||
|
||||
func NewFigiClientErrorStub(t testing.TB, err error) *internal.OpenFIGI {
|
||||
@@ -236,5 +236,5 @@ func NewFigiClientErrorStub(t testing.TB, err error) *internal.OpenFIGI {
|
||||
}),
|
||||
}
|
||||
|
||||
return internal.NewOpenFIGI(c)
|
||||
return internal.NewOpenFIGI(c, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user