/
openfigi.go
243 lines (214 loc) · 5.95 KB
/
openfigi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Package openfigi provides access to the OpenFIGI API.
// See https://openfigi.com/api for details.
package openfigi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/gomodule/redigo/redis"
)
const apiURL = "https://api.openfigi.com/v1/mapping"
const defaultTimeout = 10 * time.Second
// ErrNotValidIdentifier is returned when the requested identifier type is invalid.
var ErrNotValidIdentifier = errors.New("not a valid identifier")
// ErrWrongStatus is returned whenever the openFIGI api returns a non 200 error.
var ErrWrongStatus = errors.New("wrong status received from api")
// ErrAPIError is returned when the openFIGI api results could not be parsed.
var ErrAPIError = errors.New("unknown api error occured")
// ErrNoIdentifierFound is returned when the requested identifier could not be found by the api.
var ErrNoIdentifierFound = errors.New("no identifier found")
// ErrCacheError is returned when we can't cache openFIGI api results in redis.
var ErrCacheError = errors.New("redis cache error")
// FIGI is the main OpenFIGI data.
type FIGI struct {
FIGI string `json:"figi"`
SecurityType string `json:"securityType"`
MarketSector string `json:"marketSector"`
Ticker string `json:"ticker"`
Name string `json:"name"`
UniqueID string `json:"uniqueID"`
ExchangeCode string `json:"exchCode"`
ShareClassFIGI string `json:"shareClassFIGI"`
CompositeFIGI string `json:"compositeFIGI"`
SecurityType2 string `json:"securityType2"`
SecurityDescription string `json:"securityDescription"`
UniqueIDFutOpt string `json:"uniqueIDFutOpt"`
}
// FIGIRequest is an OpenFIGI API Request.
type FIGIRequest struct {
IDType string `json:"idType,omitempty"`
IDValue string `json:"idValue,omitempty"`
ExchangeCode string `json:"exchCode,omitempty"`
MICCode string `json:"micCode,omitempty"`
Currency string `json:"currency,omitempty"`
MarketSector string `json:"marketSecDes,omitempty"`
// private vars
apiKey string
timeout time.Duration
}
// ValidIdentifiers is a list of valid openFIGI Request Identifiers.
var ValidIdentifiers = []string{
"ID_ISIN",
"ID_BB_UNIQUE",
"ID_SEDOL",
"ID_COMMON",
"ID_WERTPAPIER",
"ID_CUSIP",
"ID_CINS",
"ID_BB",
"ID_ITALY",
"ID_EXCH_SYMBOL",
"ID_FULL_EXCHANGE_SYMBOL",
"COMPOSITE_ID_BB_GLOBAL",
"ID_BB_GLOBAL_SHARE_CLASS_LEVEL",
"ID_BB_GLOBAL",
"ID_BB_SEC_NUM_DES",
"TICKER",
"ID_CUSIP_8_CHR",
"OCC_SYMBOL",
"UNIQUE_ID_FUT_OPT",
"OPRA_SYMBOL",
"TRADING_SYSTEM_IDENTIFIER",
}
func isValidIdentifier(v string) bool {
for _, i := range ValidIdentifiers {
if v == i {
return true
}
}
return false
}
// NewRequest initializes a new request and checks if the idtype is valid.
func NewRequest(idtype, idvalue string) (*FIGIRequest, error) {
if !isValidIdentifier(idtype) {
return nil, ErrNotValidIdentifier
}
return &FIGIRequest{IDType: idtype, IDValue: idvalue, timeout: defaultTimeout}, nil
}
// APIKey sets the API key for openFIGI requests.
// Note that openFIGI works perfectly fine without API Key
// but requests are rate limited without one.
func (fr *FIGIRequest) APIKey(key string) {
fr.apiKey = key
}
// Exchange sets the ExchangeCode for the openFIGI request.
func (fr *FIGIRequest) Exchange(exch string) {
fr.ExchangeCode = exch
}
// Do performs the openFIGI request. Although openFIGI supports up
// to 5 queries per request, this implementation only supports 1.
// Errors returned are one of the package wide errors or generic
// http i/o or json parsing errors.
func (fr *FIGIRequest) Do() ([]*FIGI, error) {
reqdata := []*FIGIRequest{fr}
js, err := json.Marshal(reqdata)
if err != nil {
return nil, err
}
cached, err := getCache(js)
if err != nil {
return nil, err
}
if cached != nil {
return cached, err
}
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(js))
req.Header.Set("Content-Type", "application/json")
if fr.apiKey != "" {
req.Header.Set("X-OPENFIGI-APIKEY", fr.apiKey)
}
client := &http.Client{
Timeout: fr.timeout,
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, ErrWrongStatus
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
data := []map[string][]*FIGI{}
err = json.Unmarshal(body, &data)
if err != nil {
// No data was found, check if we maybe have an error.
data := []map[string]string{}
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, ErrAPIError
}
e := data[0]["error"]
if e == "No identifier found." {
// This is the most common error.
return nil, ErrNoIdentifierFound
} else if e != "" {
return nil, fmt.Errorf(data[0]["error"])
}
return nil, ErrAPIError
}
if len(data) == 0 {
return nil, ErrAPIError
}
if err := setCache(js, data[0]["data"]); err != nil {
return nil, ErrCacheError
}
return data[0]["data"], nil
}
var rpool *redis.Pool
// RedisCache sets up Redis to use as a cache for openFIGI data.
// If you don't call this on startup, no caching is performed.
func RedisCache(addr string) error {
rpool = &redis.Pool{
MaxIdle: 5,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", addr)
},
}
return nil
}
func getCache(key []byte) ([]*FIGI, error) {
if rpool == nil {
return nil, nil
}
c := rpool.Get()
defer c.Close() // nolint: errcheck
res, err := c.Do("GET", string(key))
if err != nil {
return nil, err
}
if res == nil {
return nil, nil
}
b, err := redis.Bytes(res, err)
if err != nil {
return nil, err
}
data := []*FIGI{}
err = json.Unmarshal(b, &data)
return data, err
}
func setCache(key []byte, data []*FIGI) error {
if rpool == nil {
return nil
}
c := rpool.Get()
defer c.Close() // nolint: errcheck
js, err := json.Marshal(data)
if err != nil {
return err
}
_, err = c.Do("SET", string(key), string(js))
return err
}