Skip to main content

brokapi_alor/
lib.rs

1//! ALOR Broker connectors: the market data connector and the user data connector.
2//!
3//! This crate is the connector layer for the ALOR OpenAPI. It sits on top of the protocol crate
4//! `brokapi-alor-protocol` (HTTP and WebSocket clients, wire models) and the runtime crates (`brokapi-runtime`,
5//! `brokapi-sansio`), and it implements the traits of `brokapi-core`:
6//!
7//! - [`MarketDataConnector`] implements `OrderBookSource`, `MarketDataSource` and `InstrumentSource`. It is not
8//!   tied to an account.
9//! - [`UserDataConnector`] implements `OrderExecutor`, `StopOrderExecutor`, `ExecutionSource` and
10//!   `PortfolioSource` for one portfolio of the account the refresh token belongs to.
11//!
12//! Both connectors start with an `async fn start(config, limiter, out)`. Commands return their answer directly.
13//! Subscriptions deliver `Envelope`s into the channel `out`.
14//!
15//! Anything the shared traits cannot express is reachable through `connector.raw()` ([`AlorRaw`]): the same
16//! token, endpoint and rate limiter, one API method at a time. [`limits::default_limits`] gives the bundled rate
17//! limits. Give every connector of one ALOR login the same limiter, because the limits belong to the login.
18//!
19//! The full guide is at <https://brokapi.ru/docs/>.
20//!
21//! # Examples
22//!
23//! The example runs against an in-process imitation of the broker, so it needs no network and no account. With a
24//! real account, `cfg` is built with `AlorConfig::from_env()` instead.
25//!
26//! ```
27//! use std::sync::Arc;
28//! use brokapi_alor::limits::default_limits;
29//! use brokapi_alor::{MarketDataConfig, MarketDataConnector, UserDataConfig, UserDataConnector};
30//! use brokapi_core::{PortfolioSource, ShutdownMode};
31//! use brokapi_runtime::BrokerLimiter;
32//! use tokio::sync::mpsc;
33//! # use brokapi_alor_protocol::config::AlorConfig;
34//!
35//! # #[tokio::main(flavor = "current_thread")]
36//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
37//! # let server = brokapi_mocks::alor::MockServer::start(Default::default()).await;
38//! # let cfg = AlorConfig::for_mock("mock-refresh-token", &server.http_base);
39//! // One limiter for the whole login.
40//! let limiter: Arc<BrokerLimiter> = BrokerLimiter::new(&default_limits());
41//! let (md_tx, _md_events) = mpsc::channel(256);
42//! let (ud_tx, _ud_events) = mpsc::channel(256);
43//!
44//! let md = MarketDataConnector::start(
45//!     MarketDataConfig::new(cfg.clone(), "alor-md"),
46//!     Arc::clone(&limiter),
47//!     md_tx,
48//! )
49//! .await?;
50//! let ud = UserDataConnector::start(UserDataConfig::new(cfg, "alor-ud"), limiter, ud_tx).await?;
51//!
52//! let portfolio = ud.portfolio().await?;
53//! println!("{} balances", portfolio.balances.len());
54//!
55//! md.shutdown(ShutdownMode::Immediate).await;
56//! ud.shutdown(ShutdownMode::Immediate).await;
57//! # Ok(())
58//! # }
59//! ```
60
61#![warn(missing_docs)]
62
63pub mod cid;
64pub mod config;
65pub mod core;
66pub mod error;
67pub mod limits;
68pub mod map;
69pub mod md;
70pub mod pump;
71pub mod raw;
72pub mod subs;
73pub mod ud;
74pub mod util;
75
76pub use cid::AlorCidCodec;
77pub use config::{MarketDataConfig, UserDataConfig};
78pub use core::{MdCore, UdCore};
79pub use md::MarketDataConnector;
80pub use raw::AlorRaw;
81pub use ud::UserDataConnector;