Report includes the type/nature of row #17

Merged
natercio merged 11 commits from record-type into main 2025-11-24 16:29:44 +00:00
2 changed files with 136 additions and 18 deletions
Showing only changes of commit 23614d51db - Show all commits

View File

@@ -6,28 +6,23 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"time"
) )
type OpenFIGI struct { type OpenFIGI struct {
client http.Client client *http.Client
cache map[string]string
} }
func NewOpenFIGI() *OpenFIGI { func NewOpenFIGI(c *http.Client) *OpenFIGI {
return &OpenFIGI{ return &OpenFIGI{
client: http.Client{ client: c,
Timeout: 5 * time.Second,
},
} }
} }
func (of *OpenFIGI) Category(ctx context.Context, isin, ticker string) (string, error) { func (of *OpenFIGI) SecurityTypeByISIN(ctx context.Context, isin string) (string, error) {
rawBody, err := json.Marshal(mappingRequestBody{ rawBody, err := json.Marshal([]mappingRequestBody{{
IdType: "ID_ISIN", IDType: "ID_ISIN",
IdValue: isin, IDValue: isin,
}) }})
if err != nil { if err != nil {
return "", fmt.Errorf("marshal mapping request body: %w", err) return "", fmt.Errorf("marshal mapping request body: %w", err)
} }
@@ -37,24 +32,46 @@ func (of *OpenFIGI) Category(ctx context.Context, isin, ticker string) (string,
return "", fmt.Errorf("create mapping request: %w", err) return "", fmt.Errorf("create mapping request: %w", err)
} }
req.Header.Add("Content-Type", "application/json")
res, err := of.client.Do(req) res, err := of.client.Do(req)
if err != nil { if err != nil {
return "", fmt.Errorf("make mapping request: %w", err) return "", fmt.Errorf("make mapping request: %w", err)
} }
defer res.Body.Close() defer res.Body.Close()
var resBody mappingResponseBody if res.StatusCode >= 400 {
return "", fmt.Errorf("bad mapping response status code: %s", res.Status)
}
var resBody []mappingResponseBody
err = json.NewDecoder(res.Body).Decode(&resBody) err = json.NewDecoder(res.Body).Decode(&resBody)
if err != nil { if err != nil {
return "", fmt.Errorf("unmarshal response: %w", err) return "", fmt.Errorf("unmarshal response: %w", err)
} }
return "", nil if len(resBody) == 0 {
return "", fmt.Errorf("missing top-level elements")
}
if len(resBody[0].Data) == 0 {
return "", fmt.Errorf("missing data elements")
}
// It is not possible that an isin is assign to diferent security types, therefore we can assume
// all entries have the same securityType value.
return resBody[0].Data[0].SecurityType, nil
} }
type mappingRequestBody struct { type mappingRequestBody struct {
IdType string `json:"idType"` IDType string `json:"idType"`
IdValue string `json:"idValue"` IDValue string `json:"idValue"`
} }
type mappingResponseBody struct{} type mappingResponseBody struct {
Data []struct {
FIGI string `json:"figi"`
SecurityType string `json:"securityType"`
Ticker string `json:"ticker"`
} `json:"data"`
}

101
internal/open_figi_test.go Normal file
View File

@@ -0,0 +1,101 @@
package internal_test
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"time"
"github.com/nmoniz/any2anexoj/internal"
)
func TestOpenFIGI_SecurityTypeByISIN(t *testing.T) {
tests := []struct {
name string // description of this test case
response *http.Response
isin string
want string
wantErr bool
}{
{
name: "all good",
response: &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`[{"data":[{"figi":"BBG000BJJR23","name":"AIRBUS SE","ticker":"EADSF","exchCode":"US","compositeFIGI":"BBG000BJJR23","securityType":"Common Stock","marketSector":"Equity","shareClassFIGI":"BBG001S8TFZ6","securityType2":"Common Stock","securityDescription":"EADSF"},{"figi":"BBG000BJJXJ2","name":"AIRBUS SE","ticker":"EADSF","exchCode":"PQ","compositeFIGI":"BBG000BJJR23","securityType":"Common Stock","marketSector":"Equity","shareClassFIGI":"BBG001S8TFZ6","securityType2":"Common Stock","securityDescription":"EADSF"}]}]`)),
},
isin: "NL0000235190",
want: "Common Stock",
},
{
name: "bas status code",
response: &http.Response{
Status: http.StatusText(http.StatusTooManyRequests),
StatusCode: http.StatusTooManyRequests,
},
isin: "NL0000235190",
wantErr: true,
},
{
name: "empty top-level",
response: &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`[]`)),
},
isin: "NL0000235190",
wantErr: true,
},
{
name: "empty data elements",
response: &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`[{"data":[]}]`)),
},
isin: "NL0000235190",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewTestClient(t, func(req *http.Request) (*http.Response, error) {
return tt.response, nil
})
of := internal.NewOpenFIGI(c)
got, gotErr := of.SecurityTypeByISIN(context.Background(), tt.isin)
if gotErr != nil {
if !tt.wantErr {
t.Errorf("want success but failed: %v", gotErr)
}
return
}
if tt.wantErr {
t.Fatal("want error but none")
}
if tt.want != got {
t.Fatalf("want security type to be %s but got %s", tt.want, got)
}
})
}
}
type RoundTripFunc func(req *http.Request) (*http.Response, error)
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func NewTestClient(t testing.TB, fn RoundTripFunc) *http.Client {
t.Helper()
return &http.Client{
Timeout: time.Second,
Transport: fn,
}
}