Skip to main content

brokapi_tbank_protocol/
lib.rs

1//! Low-level T-Bank (T-Invest) API wire protocol: the gRPC client generated from the official `.proto`.
2//!
3//! This is the protocol layer of the T-Bank connector. It knows how to talk to the broker and
4//! nothing about orders, books or events. The connector crate `brokapi-tbank` sits on top of it and
5//! turns these messages into the shared `brokapi-core` types. Use this crate directly only when you
6//! need a call the connectors do not wrap.
7//!
8//! - [`v1`] - generated messages, enums and gRPC clients. Every service and method of the official
9//!   contract is generated. Their documentation comes from the `.proto` comments.
10//! - [`TbankConfig`] - endpoint (production, sandbox or a local mock), token, account, timeouts.
11//! - [`TbankClient`] - one shared HTTP/2 channel with Bearer-token authorization. Each accessor
12//!   (for example [`TbankClient::orders`]) returns a ready generated client.
13//! - [`AuthInterceptor`] - the piece that adds `authorization: Bearer <token>` to every request.
14//! - [`money`] - `Quotation` and `MoneyValue` (units and nano) converted to and from `Decimal`, without `f64` on the way.
15//! - [`ProtocolError`] - configuration and transport errors raised while building the channel.
16//!
17//! The full guide is in the book: <https://brokapi.ru/docs/>.
18//!
19//! # Examples
20//!
21//! The example below starts the in-process T-Bank mock, builds a client for it and lists the
22//! accounts the token can see. Nothing touches the network and no money is involved.
23//!
24//! ```
25//! use brokapi_tbank_protocol::{TbankClient, TbankConfig};
26//! # use brokapi_mocks::tbank::{MockServer, TOKEN};
27//!
28//! # #[tokio::main(flavor = "current_thread")]
29//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
30//! # let server = MockServer::start(Default::default()).await;
31//! # let cfg = TbankConfig::for_mock(TOKEN, &server.endpoint);
32//! // In real code: `TbankConfig::sandbox("t.your-token")` or `TbankConfig::production(..)`.
33//! let client = TbankClient::new(&cfg)?;
34//! let accounts = client.list_accounts().await?;
35//! assert!(!accounts.is_empty());
36//! for account in &accounts {
37//!     println!("account {}", account.id);
38//! }
39//! # Ok(())
40//! # }
41//! ```
42
43#![warn(missing_docs)]
44
45pub mod client;
46pub mod config;
47pub mod error;
48pub mod money;
49
50/// Messages, enums and gRPC clients generated from the official T-Bank `.proto` files.
51///
52/// The documentation of each item is the comment from the `.proto` file, in the broker's own
53/// words (Russian). Prices are [`Quotation`] and [`MoneyValue`] (units and nano), see [`crate::money`].
54///
55/// [`Quotation`]: crate::v1::Quotation
56/// [`MoneyValue`]: crate::v1::MoneyValue
57#[allow(clippy::all, clippy::pedantic, rustdoc::all, missing_docs)]
58pub mod v1 {
59    tonic::include_proto!("tinkoff.public.invest.api.contract.v1");
60}
61
62/// The descriptors of every service and message of the API, as a serialized `FileDescriptorSet` (the
63/// `.proto` files this crate is built from, with their imports). Load it into a descriptor pool to call a
64/// method by its name, see [`TbankClient::grpc`].
65pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/descriptor.bin"));
66
67pub use client::{AuthInterceptor, Authed, TbankClient};
68pub use config::{PROD_ENDPOINT, SANDBOX_ENDPOINT, TbankConfig};
69pub use error::ProtocolError;