finlight-client 0.2.0

Breaking release: speaks finlight API v3 only (0.1.x speaks v2). Official Rust client for the finlight.me API: financial news search, lookups, real-time streams and webhook verification.
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Wire models. Field names follow the wire keys in `snake_case`; serde writes the wire keys back.
//!
//! Readers are tolerant: unknown keys are ignored at every level, missing optional keys read as
//! `None` (lists as empty), and only the required keys (`id`, `link`, `title`, `source.domain`;
//! `companyId` and `name`; `domain`; event `type`) fail an entry. Timestamps keep the exact
//! string the server sent; the `*_time()` accessors parse it strictly (RFC 3339) into a
//! `SystemTime` and give `None` when it is not readable.

use std::sync::Arc;
use std::time::SystemTime;

use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::value::RawValue;

use crate::error::Error;
use crate::field::Field;
use crate::time::parse_opt;
use crate::vocab::{
    CompanyRole, EventDirection, EventType, Importance, SentimentLabel, SourceType, StreamEventName,
};

fn null_as_empty<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
    D: Deserializer<'de>,
    T: Deserialize<'de>,
{
    Ok(Option::<Vec<T>>::deserialize(deserializer)?.unwrap_or_default())
}

/// A news article (REST, streams and webhooks share this shape).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Article {
    /// Stable id (22 characters, base64url).
    pub id: String,
    /// Article URL.
    pub link: String,
    /// Where the article comes from.
    pub source: ArticleSource,
    /// Title in the original language.
    pub title: String,
    /// Summary; `None` when there is none (never an empty string from the server).
    #[serde(default)]
    pub summary: Option<String>,
    /// ISO 639-1 language.
    #[serde(default)]
    pub language: Option<String>,
    /// Images, at most 3.
    #[serde(default, deserialize_with = "null_as_empty")]
    pub media: Vec<Media>,
    /// Publication time claimed by the source, as sent (legacy data can carry 1970-01-01).
    #[serde(default)]
    pub published_at: Option<String>,
    /// Ingest time, as sent.
    #[serde(default)]
    pub first_seen_at: Option<String>,
    /// The source's revision time, as sent.
    #[serde(default)]
    pub revised_at: Option<String>,
    /// When the enriched article first became available, as sent (always `None` on raw).
    #[serde(default)]
    pub available_at: Option<String>,
    /// Geographic focus of the story (ISO alpha-2).
    #[serde(default, deserialize_with = "null_as_empty")]
    pub countries: Vec<String>,
    /// Categories (open vocabulary).
    #[serde(default, deserialize_with = "null_as_empty")]
    pub categories: Vec<String>,
    /// Full text: absent unless requested with `include` and entitled; `null` when there is none.
    #[serde(default, skip_serializing_if = "Field::is_absent")]
    pub content: Field<String>,
    /// Sentiment: absent unless entitled; `null` when not computed.
    #[serde(default, skip_serializing_if = "Field::is_absent")]
    pub sentiment: Field<Sentiment>,
    /// Tagged companies: `None` when absent (not requested or not entitled); `Some([])` means
    /// tagged, no company.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub companies: Option<Vec<Company>>,
    /// Tagged events: `None` when absent; `Some([])` means classified, no event.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub events: Option<Vec<EventTag>>,
    #[serde(skip)]
    raw: Option<Arc<str>>,
}

impl Article {
    /// `true` when the `content` key was in the JSON (`null` included).
    pub fn content_present(&self) -> bool {
        self.content.is_present()
    }

    /// `true` when the `sentiment` key was in the JSON (`null` included).
    pub fn sentiment_present(&self) -> bool {
        self.sentiment.is_present()
    }

    /// `true` when the `companies` key was in the JSON.
    pub fn companies_present(&self) -> bool {
        self.companies.is_some()
    }

    /// `true` when the `events` key was in the JSON.
    pub fn events_present(&self) -> bool {
        self.events.is_some()
    }

    /// `published_at` parsed strictly; `None` when absent or not RFC 3339.
    pub fn published_at_time(&self) -> Option<SystemTime> {
        parse_opt(self.published_at.as_deref())
    }

    /// `first_seen_at` parsed strictly.
    pub fn first_seen_at_time(&self) -> Option<SystemTime> {
        parse_opt(self.first_seen_at.as_deref())
    }

    /// `revised_at` parsed strictly.
    pub fn revised_at_time(&self) -> Option<SystemTime> {
        parse_opt(self.revised_at.as_deref())
    }

    /// `available_at` parsed strictly.
    pub fn available_at_time(&self) -> Option<SystemTime> {
        parse_opt(self.available_at.as_deref())
    }

    /// The article JSON exactly as received, unknown keys included.
    pub fn raw_json(&self) -> Option<&str> {
        self.raw.as_deref()
    }
}

/// Source of an article.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ArticleSource {
    /// Domain, e.g. `www.reuters.com`; the join key to `sources().list()`.
    pub domain: String,
    /// Source type when the article was indexed (open). `None` when the source is not
    /// classified or the article is older than source types. Can lag `Source::type` after a
    /// reclassification.
    #[serde(default)]
    pub r#type: Option<SourceType>,
}

/// An image of an article.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Media {
    /// Image URL.
    #[serde(default)]
    pub url: Option<String>,
}

/// Sentiment of an article.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Sentiment {
    /// Label (open), `None` when not set.
    #[serde(default)]
    pub label: Option<SentimentLabel>,
    /// Confidence, full precision.
    #[serde(default)]
    pub confidence: Option<f64>,
}

/// A company tagged in an article.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Company {
    /// Company id; join key to `companies().get()` and `EventTag::company_ids`.
    pub company_id: i64,
    /// Name.
    pub name: String,
    /// ISIN.
    #[serde(default)]
    pub isin: Option<String>,
    /// Primary listing; `None` for private companies.
    #[serde(default)]
    pub primary_listing: Option<Listing>,
    /// Tagging confidence.
    #[serde(default)]
    pub confidence: Option<f64>,
    /// Position in the story (open); `None` for articles tagged without roles.
    #[serde(default)]
    pub role: Option<CompanyRole>,
    /// Aboutness, 0 to 1; `None` for articles tagged without roles.
    #[serde(default)]
    pub relevance: Option<f64>,
}

/// A listing of a company.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Listing {
    /// Ticker symbol.
    #[serde(default)]
    pub ticker: Option<String>,
    /// Exchange (MIC).
    #[serde(default)]
    pub mic: Option<String>,
    /// Exchange country.
    #[serde(default)]
    pub country: Option<String>,
    /// Whether the listing is active; only set in `CompanyRecord::listings`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub active: Option<bool>,
}

/// An event tagged in an article.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct EventTag {
    /// Event type (open).
    pub r#type: EventType,
    /// Direction (open).
    #[serde(default)]
    pub direction: Option<EventDirection>,
    /// Importance (open, ordered).
    #[serde(default)]
    pub importance: Option<Importance>,
    /// Confidence.
    #[serde(default)]
    pub confidence: Option<f64>,
    /// Ids of the companies the event is about. `None` when absent: it is only sent when the
    /// `companies` block is in the same payload.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub company_ids: Option<Vec<i64>>,
}

/// Company master data from `companies()`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CompanyRecord {
    /// Company id.
    pub company_id: i64,
    /// Name.
    pub name: String,
    /// Country of domicile.
    #[serde(default)]
    pub country: Option<String>,
    /// Sector.
    #[serde(default)]
    pub sector: Option<String>,
    /// Industry.
    #[serde(default)]
    pub industry: Option<String>,
    /// ISINs.
    #[serde(default, deserialize_with = "null_as_empty")]
    pub isins: Vec<String>,
    /// Primary listing; `None` for private companies.
    #[serde(default)]
    pub primary_listing: Option<Listing>,
    /// One listing per exchange, the primary first.
    #[serde(default, deserialize_with = "null_as_empty")]
    pub listings: Vec<Listing>,
    /// Last update, as sent.
    #[serde(default)]
    pub updated_at: Option<String>,
    #[serde(skip)]
    raw: Option<Arc<str>>,
    #[serde(skip)]
    cache: Option<CacheInfo>,
}

impl CompanyRecord {
    /// `updated_at` parsed strictly.
    pub fn updated_at_time(&self) -> Option<SystemTime> {
        parse_opt(self.updated_at.as_deref())
    }

    /// The JSON exactly as received.
    pub fn raw_json(&self) -> Option<&str> {
        self.raw.as_deref()
    }

    /// ETag data of a record from `companies().get()` or `get_if_none_match()`; `None` for
    /// other records.
    pub fn cache_info(&self) -> Option<&CacheInfo> {
        self.cache.as_ref()
    }

    pub(crate) fn set_cache_info(&mut self, info: CacheInfo) {
        self.cache = Some(info);
    }
}

/// ETag data of a record from `companies().get()` or `get_if_none_match()`, for callers who
/// keep records themselves. The SDK keeps no cache of company records.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct CacheInfo {
    /// `ETag` header; pass it to `companies().get_if_none_match()` later.
    pub etag: Option<String>,
    /// `Cache-Control` header (`max-age` says how long your own copy stays fresh).
    pub cache_control: Option<String>,
}

/// A source from `sources().list()`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Source {
    /// Domain.
    pub domain: String,
    /// Display name; `None` when the source has none.
    #[serde(default)]
    pub name: Option<String>,
    /// Source type (open); `None` when the source is not classified.
    #[serde(default)]
    pub r#type: Option<SourceType>,
    /// Part of the default pool.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_default: Option<bool>,
    /// Whether the source has scraped content. `None` means unknown (your plan has no
    /// `fullArticleAccess`), never "no content".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub has_content: Option<bool>,
    /// A custom source granted to your subscription.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_custom: Option<bool>,
    /// Country.
    #[serde(default)]
    pub country: Option<String>,
    /// Languages, most common first.
    #[serde(default, deserialize_with = "null_as_empty")]
    pub languages: Vec<String>,
    #[serde(skip)]
    raw: Option<Arc<str>>,
}

impl Source {
    /// The JSON exactly as received.
    pub fn raw_json(&self) -> Option<&str> {
        self.raw.as_deref()
    }
}

/// Pagination of a search page.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Pagination {
    /// Page size used.
    #[serde(default)]
    pub limit: Option<u32>,
    /// Articles on this page.
    #[serde(default)]
    pub returned: Option<u32>,
    /// More pages follow.
    pub has_more: bool,
    /// Cursor for the next page; `None` at the end.
    #[serde(default)]
    pub next_cursor: Option<String>,
}

/// An entry of a list that could not be decoded. It is left out of the list, logged as a
/// warning and kept here.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct SkippedEntry {
    /// Position in the list as received.
    pub index: usize,
    /// The entry's JSON as received.
    pub raw: String,
    /// Why it was skipped.
    pub reason: String,
}

/// One page of `articles().search()`.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ArticlePage {
    /// Articles of this page.
    pub articles: Vec<Article>,
    /// Pagination.
    pub pagination: Pagination,
    /// Entries that could not be decoded.
    pub skipped: Vec<SkippedEntry>,
}

macro_rules! item_list {
    ($(#[$meta:meta])* $name:ident, $item:ty) => {
        $(#[$meta])*
        #[derive(Debug, Clone, PartialEq)]
        pub struct $name {
            items: Vec<$item>,
            skipped: Vec<SkippedEntry>,
        }

        impl $name {
            pub(crate) fn new(items: Vec<$item>, skipped: Vec<SkippedEntry>) -> Self {
                Self { items, skipped }
            }

            /// Entries that could not be decoded.
            pub fn skipped(&self) -> &[SkippedEntry] {
                &self.skipped
            }

            /// The entries as a `Vec`.
            pub fn into_vec(self) -> Vec<$item> {
                self.items
            }
        }

        impl std::ops::Deref for $name {
            type Target = [$item];
            fn deref(&self) -> &[$item] {
                &self.items
            }
        }

        impl IntoIterator for $name {
            type Item = $item;
            type IntoIter = std::vec::IntoIter<$item>;
            fn into_iter(self) -> Self::IntoIter {
                self.items.into_iter()
            }
        }

        impl<'a> IntoIterator for &'a $name {
            type Item = &'a $item;
            type IntoIter = std::slice::Iter<'a, $item>;
            fn into_iter(self) -> Self::IntoIter {
                self.items.iter()
            }
        }
    };
}

item_list!(
    /// Result of `companies().search()` and `companies().get_many()`: the records plus `skipped()`.
    CompanyList,
    CompanyRecord
);
item_list!(
    /// Result of `sources().list()`: the sources plus `skipped()`.
    SourceList,
    Source
);

/// Kind of a stream element.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum StreamEventKind {
    /// `article.created`.
    Created,
    /// `article.updated`.
    Updated,
    /// Any other event; `data` is `None` and the frame is reachable through `raw_json()`.
    Other,
}

impl StreamEventKind {
    /// `created`, `updated` or `other`.
    pub fn as_str(&self) -> &'static str {
        match self {
            StreamEventKind::Created => "created",
            StreamEventKind::Updated => "updated",
            StreamEventKind::Other => "other",
        }
    }
}

/// One element of a stream.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct StreamEvent {
    /// `Created`, `Updated` or `Other`.
    pub kind: StreamEventKind,
    /// The wire event name (open).
    pub event: StreamEventName,
    /// The article; `None` for `Other`.
    pub data: Option<Article>,
    raw: Arc<str>,
}

impl StreamEvent {
    pub(crate) fn new(
        kind: StreamEventKind,
        event: StreamEventName,
        data: Option<Article>,
        raw: Arc<str>,
    ) -> Self {
        StreamEvent {
            kind,
            event,
            data,
            raw,
        }
    }

    /// The whole frame exactly as received.
    pub fn raw_json(&self) -> &str {
        &self.raw
    }
}

// ---- decoding -------------------------------------------------------------------------

/// Decodes one JSON value with a path for errors.
pub(crate) fn decode_value<T: DeserializeOwned>(json: &str, path: &str) -> Result<T, Error> {
    let mut de = serde_json::Deserializer::from_str(json);
    match serde_path_to_error::deserialize::<_, T>(&mut de) {
        Ok(v) => Ok(v),
        Err(e) => {
            let inner = e.path().to_string();
            let full = match (path.is_empty(), inner.as_str()) {
                (true, _) => inner.clone(),
                (false, ".") => path.to_owned(),
                (false, _) => format!("{path}.{inner}"),
            };
            let message = format!(
                "could not decode {}: {}",
                if full.is_empty() {
                    "the response"
                } else {
                    &full
                },
                e.inner()
            );
            Err(Error::decode(
                message,
                Some(full),
                Some(json.to_owned()),
                Some(Box::new(e.into_inner())),
            ))
        }
    }
}

pub(crate) fn decode_article(raw: &RawValue, path: &str) -> Result<Article, Error> {
    let mut article: Article = decode_value(raw.get(), path)?;
    article.raw = Some(Arc::from(raw.get()));
    Ok(article)
}

pub(crate) fn decode_company(raw: &RawValue, path: &str) -> Result<CompanyRecord, Error> {
    let mut record: CompanyRecord = decode_value(raw.get(), path)?;
    record.raw = Some(Arc::from(raw.get()));
    Ok(record)
}

pub(crate) fn decode_source(raw: &RawValue, path: &str) -> Result<Source, Error> {
    let mut source: Source = decode_value(raw.get(), path)?;
    source.raw = Some(Arc::from(raw.get()));
    Ok(source)
}

/// Decodes every entry of a list; broken entries are skipped, logged and returned apart.
pub(crate) fn decode_entries<T>(
    entries: &[Box<RawValue>],
    list: &str,
    decode: impl Fn(&RawValue, &str) -> Result<T, Error>,
) -> (Vec<T>, Vec<SkippedEntry>) {
    let mut items = Vec::with_capacity(entries.len());
    let mut skipped = Vec::new();
    for (index, raw) in entries.iter().enumerate() {
        match decode(raw, &format!("{list}[{index}]")) {
            Ok(item) => items.push(item),
            Err(err) => {
                tracing::warn!(target: "finlight", list, index, "skipped an entry that could not be decoded: {err}");
                skipped.push(SkippedEntry {
                    index,
                    raw: raw.get().to_owned(),
                    reason: err.to_string(),
                });
            }
        }
    }
    (items, skipped)
}

#[derive(Deserialize)]
struct SearchEnvelope {
    articles: Vec<Box<RawValue>>,
    pagination: Pagination,
}

#[derive(Deserialize)]
struct ArticleEnvelope {
    article: Box<RawValue>,
}

#[derive(Deserialize)]
struct CompaniesEnvelope {
    companies: Vec<Box<RawValue>>,
}

#[derive(Deserialize)]
struct CompanyEnvelope {
    company: Box<RawValue>,
}

#[derive(Deserialize)]
struct SourcesEnvelope {
    sources: Vec<Box<RawValue>>,
}

pub(crate) fn decode_search_page(body: &str) -> Result<ArticlePage, Error> {
    let env: SearchEnvelope = decode_value(body, "")?;
    let (articles, skipped) = decode_entries(&env.articles, "articles", decode_article);
    Ok(ArticlePage {
        articles,
        pagination: env.pagination,
        skipped,
    })
}

pub(crate) fn decode_article_envelope(body: &str) -> Result<Article, Error> {
    let env: ArticleEnvelope = decode_value(body, "")?;
    decode_article(&env.article, "article")
}

pub(crate) fn decode_companies(body: &str) -> Result<CompanyList, Error> {
    let env: CompaniesEnvelope = decode_value(body, "")?;
    let (items, skipped) = decode_entries(&env.companies, "companies", decode_company);
    Ok(CompanyList::new(items, skipped))
}

pub(crate) fn decode_company_envelope(body: &str) -> Result<CompanyRecord, Error> {
    let env: CompanyEnvelope = decode_value(body, "")?;
    decode_company(&env.company, "company")
}

pub(crate) fn decode_sources(body: &str) -> Result<SourceList, Error> {
    let env: SourcesEnvelope = decode_value(body, "")?;
    let (items, skipped) = decode_entries(&env.sources, "sources", decode_source);
    Ok(SourceList::new(items, skipped))
}