finance-query 3.3.0

A Rust library for querying financial data
Documentation
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! OpenFIGI — keyless CUSIP/ISIN/SEDOL/FIGI to ticker mapping.
//!
//! Requires the **`openfigi`** feature flag.
//!
//! Exposed through [`crate::openfigi`], alongside [`crate::edgar`] and
//! [`crate::fred`], rather than through the Providers API: identifier
//! resolution is not tied to a symbol handle and maps onto no existing
//! [`Capability`](crate::Capability).
//!
//! # Keys
//!
//! None required. `OPENFIGI_API_KEY` is optional and raises the quota from
//! 25 requests/minute and 10 jobs per request to 25 requests/6 seconds and
//! 100 jobs per request.

pub(crate) mod client;
pub(crate) mod models;

use std::sync::{Arc, OnceLock};
use std::time::Duration;

use crate::error::{FinanceError, Result};
use crate::models::discovery::figi::{MappingFilter, SecurityIdKind, SecurityMapping};
use crate::rate_limiter::RateLimiter;
use client::OpenFigiClient;
use models::{FigiRecord, MappingJob};

const OPENFIGI_ANONYMOUS_RATE_PER_SEC: f64 = 25.0 / 60.0;
const OPENFIGI_AUTHENTICATED_RATE_PER_SEC: f64 = 25.0 / 6.0;

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

static ANONYMOUS_LIMITER: OnceLock<Arc<RateLimiter>> = OnceLock::new();
static AUTHENTICATED_LIMITER: OnceLock<Arc<RateLimiter>> = OnceLock::new();

fn limiter(authenticated: bool) -> Arc<RateLimiter> {
    let (slot, rate) = if authenticated {
        (&AUTHENTICATED_LIMITER, OPENFIGI_AUTHENTICATED_RATE_PER_SEC)
    } else {
        (&ANONYMOUS_LIMITER, OPENFIGI_ANONYMOUS_RATE_PER_SEC)
    };
    Arc::clone(slot.get_or_init(|| Arc::new(RateLimiter::new(rate))))
}

/// Build a client against the live API, reusing the shared token bucket.
///
/// The optional key is read per call, so exporting it mid-process takes
/// effect immediately.
fn client() -> Result<OpenFigiClient> {
    let api_key = std::env::var("OPENFIGI_API_KEY")
        .ok()
        .filter(|k| !k.trim().is_empty());
    let request_limiter = limiter(api_key.is_some());
    OpenFigiClient::new(
        DEFAULT_TIMEOUT,
        request_limiter,
        client::OPENFIGI_BASE,
        api_key,
    )
}

/// Map an OpenFIGI record onto the public [`SecurityMapping`].
fn to_mapping(record: FigiRecord) -> SecurityMapping {
    SecurityMapping {
        figi: record.figi,
        ticker: record.ticker,
        name: record.name,
        exchange_code: record.exch_code,
        composite_figi: record.composite_figi,
        share_class_figi: record.share_class_figi,
        security_type: record.security_type,
        market_sector: record.market_sector,
    }
}

/// Resolve one identifier to every instrument carrying it.
///
/// An identifier that is well-formed but matches nothing yields an empty
/// list; a malformed one is an error.
pub(crate) async fn resolve(kind: SecurityIdKind, id: &str) -> Result<Vec<SecurityMapping>> {
    let mut results = resolve_many(kind, std::slice::from_ref(&id)).await?;
    Ok(results.pop().unwrap_or_default())
}

/// Resolve several identifiers of the same kind in as few requests as
/// possible.
///
/// The returned vector is positional: element `i` answers `ids[i]`, with an
/// empty list where nothing matched. A malformed identifier fails the whole
/// call rather than silently reading as "no match".
pub(crate) async fn resolve_many(
    kind: SecurityIdKind,
    ids: &[&str],
) -> Result<Vec<Vec<SecurityMapping>>> {
    resolve_many_with(kind, ids, &MappingFilter::default()).await
}

/// [`resolve_many`] narrowed by a [`MappingFilter`].
pub(crate) async fn resolve_many_with(
    kind: SecurityIdKind,
    ids: &[&str],
    filter: &MappingFilter,
) -> Result<Vec<Vec<SecurityMapping>>> {
    if ids.is_empty() {
        return Ok(Vec::new());
    }

    let client = client()?;
    let max_jobs = client.max_jobs_per_request();
    let mut out: Vec<Vec<SecurityMapping>> = Vec::with_capacity(ids.len());

    for chunk in ids.chunks(max_jobs) {
        let jobs: Vec<MappingJob<'_>> = chunk
            .iter()
            .map(|id| MappingJob {
                id_type: kind.as_str(),
                id_value: id,
                exch_code: filter.exchange_code.as_deref(),
                include_unlisted_equities: filter.include_unlisted,
            })
            .collect();

        for (result, id) in client.map(&jobs).await?.into_iter().zip(chunk) {
            if let Some(error) = result.error {
                return Err(FinanceError::InvalidParameter {
                    param: format!("{kind}"),
                    reason: format!("OpenFIGI rejected '{id}': {error}"),
                });
            }
            // A `warning` means the identifier was well-formed but matched
            // nothing — an empty result, not a failure.
            if let Some(warning) = result.warning {
                tracing::debug!("OpenFIGI: {kind} '{id}' matched nothing ({warning})");
            }
            out.push(
                result
                    .data
                    .unwrap_or_default()
                    .into_iter()
                    .map(to_mapping)
                    .collect(),
            );
        }
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rate_limiter::RateLimiter;
    use std::sync::Arc;

    fn test_client(base_url: &str) -> OpenFigiClient {
        OpenFigiClient::new(
            Duration::from_secs(5),
            Arc::new(RateLimiter::new(100.0)),
            base_url,
            None,
        )
        .unwrap()
    }

    fn keyed_test_client(base_url: &str) -> OpenFigiClient {
        OpenFigiClient::new(
            Duration::from_secs(5),
            Arc::new(RateLimiter::new(100.0)),
            base_url,
            Some("test-key".to_string()),
        )
        .unwrap()
    }

    #[test]
    fn api_key_enables_the_documented_batch_size() {
        let limiter = Arc::new(RateLimiter::new(100.0));
        let anonymous = OpenFigiClient::new(
            Duration::from_secs(5),
            Arc::clone(&limiter),
            client::OPENFIGI_BASE,
            None,
        )
        .unwrap();
        let authenticated = OpenFigiClient::new(
            Duration::from_secs(5),
            limiter,
            client::OPENFIGI_BASE,
            Some("test-key".to_string()),
        )
        .unwrap();

        assert_eq!(anonymous.max_jobs_per_request(), 10);
        assert_eq!(authenticated.max_jobs_per_request(), 100);
    }

    #[tokio::test]
    async fn filters_are_sent_only_when_set() {
        let mut server = mockito::Server::new_async().await;
        let filtered = server
            .mock("POST", "/mapping")
            .match_body(mockito::Matcher::Json(serde_json::json!([{
                "idType": "TICKER",
                "idValue": "HNT",
                "exchCode": "US",
                "includeUnlistedEquities": true
            }])))
            .with_body(r#"[{"data":[{"figi":"BBG000CYNBV7","exchCode":"US","compositeFIGI":"BBG000CYNBV7","name":"HEALTH NET INC/OLD","securityType":"Common Stock"}]}]"#)
            .create_async()
            .await;
        let plain = server
            .mock("POST", "/mapping")
            .match_body(mockito::Matcher::Json(serde_json::json!([{
                "idType": "TICKER",
                "idValue": "AAPL"
            }])))
            .with_body(r#"[{"warning":"No identifier found."}]"#)
            .create_async()
            .await;
        let client = test_client(&server.url());
        let us_unlisted = MappingFilter::default()
            .exchange_code("US")
            .include_unlisted(true);
        let jobs = [MappingJob {
            id_type: "TICKER",
            id_value: "HNT",
            exch_code: us_unlisted.exchange_code.as_deref(),
            include_unlisted_equities: us_unlisted.include_unlisted,
        }];
        let results = client.map(&jobs).await.unwrap();
        let record = &results[0].data.as_ref().unwrap()[0];
        assert_eq!(record.composite_figi.as_deref(), Some("BBG000CYNBV7"));
        let jobs = [MappingJob {
            id_type: "TICKER",
            id_value: "AAPL",
            exch_code: None,
            include_unlisted_equities: false,
        }];
        client.map(&jobs).await.unwrap();
        filtered.assert_async().await;
        plain.assert_async().await;
    }

    #[tokio::test]
    async fn api_key_is_sent_in_the_documented_header() {
        let mut server = mockito::Server::new_async().await;
        let _mock = server
            .mock("POST", "/mapping")
            .match_header("X-OPENFIGI-APIKEY", "test-key")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"[{"warning":"No identifier found."}]"#)
            .create_async()
            .await;
        let jobs = [MappingJob {
            id_type: "TICKER",
            id_value: "UNKNOWN",
            exch_code: None,
            include_unlisted_equities: false,
        }];

        keyed_test_client(&server.url()).map(&jobs).await.unwrap();
    }

    fn apple_payload() -> String {
        // Verbatim shape from api.openfigi.com/v3/mapping.
        serde_json::json!([{
            "data": [
                { "figi": "BBG000B9XRY4", "name": "APPLE INC", "ticker": "AAPL",
                  "exchCode": "US", "compositeFIGI": "BBG000B9XRY4",
                  "securityType": "Common Stock", "marketSector": "Equity",
                  "shareClassFIGI": "BBG001S5N8V8", "securityType2": "Common Stock",
                  "securityDescription": "AAPL" },
                { "figi": "BBG000B9XVV8", "name": "APPLE INC", "ticker": "AAPL",
                  "exchCode": "UN", "compositeFIGI": "BBG000B9XRY4",
                  "securityType": "Common Stock", "marketSector": "Equity",
                  "shareClassFIGI": "BBG001S5N8V8", "securityType2": "Common Stock",
                  "securityDescription": "AAPL" }
            ]
        }])
        .to_string()
    }

    #[test]
    fn id_kinds_use_openfigis_own_type_names() {
        assert_eq!(SecurityIdKind::Cusip.as_str(), "ID_CUSIP");
        assert_eq!(SecurityIdKind::Isin.as_str(), "ID_ISIN");
        assert_eq!(SecurityIdKind::Sedol.as_str(), "ID_SEDOL");
        // A FIGI is looked up under Bloomberg's own id type.
        assert_eq!(SecurityIdKind::Figi.as_str(), "ID_BB_GLOBAL");
        assert_eq!(SecurityIdKind::Ticker.as_str(), "TICKER");
    }

    #[tokio::test]
    async fn a_cusip_resolves_to_every_listing_it_covers() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("POST", "/mapping")
            .match_body(mockito::Matcher::PartialJson(serde_json::json!([{
                "idType": "ID_CUSIP",
                "idValue": "037833100"
            }])))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(apple_payload())
            .create_async()
            .await;

        let jobs = vec![MappingJob {
            id_type: "ID_CUSIP",
            id_value: "037833100",
            exch_code: None,
            include_unlisted_equities: false,
        }];
        let results = test_client(&server.url()).map(&jobs).await.unwrap();
        let mappings: Vec<_> = results[0]
            .data
            .clone()
            .unwrap()
            .into_iter()
            .map(to_mapping)
            .collect();

        // One CUSIP covers many venue listings.
        assert_eq!(mappings.len(), 2);
        assert_eq!(mappings[0].figi, "BBG000B9XRY4");
        assert_eq!(mappings[0].ticker.as_deref(), Some("AAPL"));
        assert_eq!(mappings[0].name.as_deref(), Some("APPLE INC"));
        assert_eq!(mappings[0].exchange_code.as_deref(), Some("US"));
        assert_eq!(mappings[1].exchange_code.as_deref(), Some("UN"));
        // Both roll up to the same composite and share class. Asserted
        // against the literal values, not against each other: OpenFIGI
        // capitalises these keys (`compositeFIGI`), so a rename bug would
        // leave both `None` and pass an equality-only check.
        assert_eq!(mappings[0].composite_figi.as_deref(), Some("BBG000B9XRY4"));
        assert_eq!(mappings[1].composite_figi.as_deref(), Some("BBG000B9XRY4"));
        assert_eq!(
            mappings[0].share_class_figi.as_deref(),
            Some("BBG001S5N8V8")
        );
    }

    #[tokio::test]
    async fn a_length_mismatch_is_rejected_rather_than_mispaired() {
        // Results are positional; a short array would silently pair answers
        // with the wrong identifiers.
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("POST", "/mapping")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::json!([{ "warning": "No identifier found." }]).to_string())
            .create_async()
            .await;

        let jobs = vec![
            MappingJob {
                id_type: "ID_CUSIP",
                id_value: "037833100",
                exch_code: None,
                include_unlisted_equities: false,
            },
            MappingJob {
                id_type: "ID_CUSIP",
                id_value: "594918104",
                exch_code: None,
                include_unlisted_equities: false,
            },
        ];
        let err = test_client(&server.url()).map(&jobs).await.unwrap_err();
        assert!(
            matches!(err, FinanceError::ResponseStructureError { .. }),
            "{err:?}"
        );
    }

    #[tokio::test]
    async fn an_oversized_batch_explains_the_limit() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("POST", "/mapping")
            .with_status(413)
            .create_async()
            .await;

        let jobs = vec![MappingJob {
            id_type: "ID_CUSIP",
            id_value: "037833100",
            exch_code: None,
            include_unlisted_equities: false,
        }];
        let err = test_client(&server.url()).map(&jobs).await.unwrap_err();
        match err {
            FinanceError::InvalidParameter { reason, .. } => {
                assert!(reason.contains("at most 10"), "got {reason}");
            }
            other => panic!("expected InvalidParameter, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn rate_limit_maps_to_rate_limited() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("POST", "/mapping")
            .with_status(429)
            .create_async()
            .await;

        let jobs = vec![MappingJob {
            id_type: "ID_CUSIP",
            id_value: "037833100",
            exch_code: None,
            include_unlisted_equities: false,
        }];
        let err = test_client(&server.url()).map(&jobs).await.unwrap_err();
        assert!(matches!(err, FinanceError::RateLimited { .. }), "{err:?}");
    }

    #[tokio::test]
    async fn resolving_nothing_makes_no_request() {
        // No mock server exists, so any HTTP call would fail the test.
        assert!(
            resolve_many(SecurityIdKind::Cusip, &[])
                .await
                .unwrap()
                .is_empty()
        );
    }
}