Skip to main content

trading_ig/client/
mod.rs

1//! The top-level [`IgClient`] — entry point for all REST domain APIs.
2//!
3//! Construction goes through [`IgClientBuilder`]:
4//!
5//! ```no_run
6//! use trading_ig::{IgClient, Environment, Credentials};
7//!
8//! # async fn run() -> trading_ig::Result<()> {
9//! let client = IgClient::builder()
10//!     .environment(Environment::Demo)
11//!     .api_key("YOUR_API_KEY")
12//!     .credentials(Credentials::password("user", "pass"))
13//!     .build()?;
14//!
15//! client.session().login().await?;
16//! # Ok(()) }
17//! ```
18
19mod builder;
20pub(crate) mod http;
21
22pub use builder::IgClientBuilder;
23
24use std::sync::Arc;
25
26use crate::accounts::AccountsApi;
27use crate::client_sentiment::ClientSentimentApi;
28use crate::config::IgConfig;
29use crate::dealing::DealingApi;
30use crate::history::HistoryApi;
31use crate::markets::MarketsApi;
32use crate::operations::OperationsApi;
33use crate::prices::PricesApi;
34use crate::repeat_dealing::RepeatDealingApi;
35use crate::session::{Credentials, SessionApi, SessionHandle, SharedSession};
36#[cfg(feature = "stream")]
37use crate::streaming::client::StreamingApi;
38use crate::watchlists::WatchlistsApi;
39
40use http::Transport;
41
42/// Cheap-to-clone handle to an authenticated (or about-to-be-authenticated)
43/// IG client. Internally a shared transport plus shared session state.
44#[derive(Debug, Clone)]
45pub struct IgClient {
46    pub(crate) transport: Transport,
47    pub(crate) session: SharedSession,
48    pub(crate) credentials: Option<Credentials>,
49    pub(crate) config: Arc<IgConfig>,
50}
51
52impl IgClient {
53    pub fn builder() -> IgClientBuilder {
54        IgClientBuilder::default()
55    }
56
57    /// Read-only access to the resolved configuration.
58    pub fn config(&self) -> &IgConfig {
59        &self.config
60    }
61
62    /// Accounts API: list accounts and manage preferences.
63    pub fn accounts(&self) -> AccountsApi<'_> {
64        AccountsApi { client: self }
65    }
66
67    /// Client sentiment API: long/short percentages for IG markets.
68    pub fn client_sentiment(&self) -> ClientSentimentApi<'_> {
69        ClientSentimentApi { client: self }
70    }
71
72    /// Dealing API: positions, working orders.
73    pub fn dealing(&self) -> DealingApi<'_> {
74        DealingApi::new(self)
75    }
76
77    /// History API: activity (v1 + v3) and transactions (v1 + v2).
78    pub fn history(&self) -> HistoryApi<'_> {
79        HistoryApi { client: self }
80    }
81
82    /// Markets API: search, fetch, and navigate IG market instruments.
83    pub fn markets(&self) -> MarketsApi<'_> {
84        MarketsApi { client: self }
85    }
86
87    /// Operations API: manage API application keys.
88    pub fn operations(&self) -> OperationsApi<'_> {
89        OperationsApi { client: self }
90    }
91
92    /// Historical prices API: v1, v2, v3 endpoints and auto-pagination.
93    pub fn prices(&self) -> PricesApi<'_> {
94        PricesApi { client: self }
95    }
96
97    /// Repeat dealing API: windows for re-trading recently dealt instruments.
98    pub fn repeat_dealing(&self) -> RepeatDealingApi<'_> {
99        RepeatDealingApi { client: self }
100    }
101
102    /// Session API: login, refresh, switch account, logout.
103    pub fn session(&self) -> SessionApi {
104        SessionApi {
105            handle: SessionHandle {
106                transport: self.transport.clone(),
107                session: self.session.clone(),
108                credentials: self.credentials.clone(),
109            },
110        }
111    }
112
113    /// Watchlists API: list, create, delete watchlists; add/remove markets.
114    pub fn watchlists(&self) -> WatchlistsApi<'_> {
115        WatchlistsApi { client: self }
116    }
117
118    /// Streaming API: connect to the Lightstreamer endpoint and subscribe to
119    /// live market, chart, account, and trade updates.
120    ///
121    /// Requires the `stream` feature.
122    #[cfg(feature = "stream")]
123    #[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
124    pub fn streaming(&self) -> StreamingApi<'_> {
125        StreamingApi { client: self }
126    }
127}