Skip to main content

finlight_client/
client.rs

1use std::sync::Arc;
2
3use crate::config::Config;
4use crate::error::Error;
5use crate::http::ApiClient;
6use crate::services::{ArticleService, SourceService};
7use crate::websocket::{RawWebSocketClient, WebSocketClient, WebSocketOptions};
8
9/// Entry point to the finlight API.
10pub struct Client {
11    /// REST access to articles.
12    pub articles: ArticleService,
13    /// REST access to sources.
14    pub sources: SourceService,
15    /// Enhanced article stream, default options. For custom options use
16    /// [`WebSocketClient::new`].
17    pub websocket: WebSocketClient,
18    /// Raw article stream, default options. For custom options use
19    /// [`RawWebSocketClient::new`].
20    pub raw_websocket: RawWebSocketClient,
21}
22
23impl Client {
24    /// Validates `config` and returns a ready-to-use client.
25    pub fn new(config: Config) -> Result<Self, Error> {
26        if config.api_key.is_empty() {
27            return Err(Error::MissingApiKey);
28        }
29        let api = Arc::new(ApiClient::new(config.clone())?);
30        Ok(Self {
31            articles: ArticleService { api: api.clone() },
32            sources: SourceService { api },
33            websocket: WebSocketClient::new(config.clone(), WebSocketOptions::default()),
34            raw_websocket: RawWebSocketClient::new(config, WebSocketOptions::default()),
35        })
36    }
37}