Skip to main content

brokapi_bcs_protocol/
lib.rs

1//! BCS wire protocol: REST and WebSocket clients, serde models and small helpers.
2//!
3//! This is the protocol layer for the BCS Trade API (a Russian broker). It talks to the broker in the
4//! broker's own words and does nothing else: no order state, no mapping to common types, no retry of
5//! orders. The connector crate `brokapi-bcs` sits on top of it and maps these types to the
6//! broker-independent ones. Use this crate directly when you want the raw API.
7//!
8//! The reference book is at <https://brokapi.ru/docs/>.
9//!
10//! # Parts
11//!
12//! * Module [`config`]: [`BcsConfig`](config::BcsConfig), the refresh token and the base URLs.
13//! * Module [`http`]: [`BcsHttpClient`](http::client::BcsHttpClient) with one method per REST endpoint, and the
14//!   request and response models in [`http::models`].
15//! * Module [`ws`]: [`BcsWsClient`](ws::client::BcsWsClient) and [`WsSession`](ws::session::WsSession), a
16//!   background WebSocket session that reconnects and restores subscriptions, and the frame models in
17//!   [`ws::models`].
18//! * Module [`models`]: enums and small types shared by REST and WebSocket.
19//! * Module [`utils`]: price-step arithmetic, the date format of the API and a token-bucket rate limiter.
20//! * Module [`error`]: [`BcsApiError`](error::BcsApiError).
21//!
22//! # Authorization in one paragraph
23//!
24//! The broker gives you a long-lived refresh token (90 days by the broker docs). The client trades it
25//! for an access token (24 hours) at the first call, sends the access token as
26//! `Authorization: Bearer ...`, and asks for a new one when the broker answers 401. You never handle
27//! the access token yourself.
28//!
29//! # Example
30//!
31//! The example runs against the in-process imitation of the broker (`brokapi-mocks`), so it needs no
32//! network and no account. With a real account you build `cfg` with
33//! [`BcsConfig::from_env`](config::BcsConfig::from_env) or [`BcsConfig::new`](config::BcsConfig::new).
34//!
35//! ```
36//! # #[tokio::main(flavor = "current_thread")]
37//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
38//! # let server = brokapi_mocks::bcs::MockServer::start(Default::default()).await;
39//! # let cfg = brokapi_bcs_protocol::config::BcsConfig::for_mock("mock-refresh-token", &server.http_base);
40//! use brokapi_bcs_protocol::http::client::BcsHttpClient;
41//! use brokapi_bcs_protocol::http::models::OrderBookRequest;
42//!
43//! let client = BcsHttpClient::new(cfg)?;
44//! let book = client
45//!     .get_order_book(&OrderBookRequest {
46//!         ticker: "SBER".into(),
47//!         class_code: "TQBR".into(),
48//!         depth: 5,
49//!     })
50//!     .await?;
51//! assert_eq!(book.ticker, "SBER");
52//! assert!(!book.bids.is_empty());
53//! # Ok(())
54//! # }
55//! ```
56
57#![warn(missing_docs)]
58
59pub mod config;
60pub mod error;
61pub mod http;
62pub mod models;
63pub mod utils;
64pub mod ws;