Skip to main content

brokapi_finam_protocol/
lib.rs

1//! Finam Trade API gRPC protocol layer.
2//!
3//! - [`pb`]: messages and clients generated from the official `.proto` (vendored in `proto/`,
4//!   see `proto/README.md`), compiled without `protoc` by `build.rs`.
5//! - [`auth`]: secret -> JWT, refreshed ahead of expiry; shareable between connectors.
6//! - [`client`]: channel setup and service clients that attach the current JWT to every call.
7//! - [`error`]: [`FinamError`], the error type of this layer.
8//!
9//! This is the protocol layer. The connectors that implement the shared traits (`brokapi-finam`) sit
10//! on top of it. Use this crate directly only when you need a Finam call the connector does not wrap
11//! (the connector's `raw()` is usually easier, it also keeps the rate limit).
12//!
13//! The book with the full guide: <https://brokapi.ru/docs/>
14//!
15//! # Examples
16//!
17//! Log in and ask for the last quote of an instrument. The example runs against an in-process
18//! imitation of the broker, so it needs no account and no network:
19//!
20//! ```
21//! use brokapi_finam_protocol::pb::grpc::tradeapi::v1::marketdata::QuoteRequest;
22//! use brokapi_finam_protocol::{ApiEndpoint, FinamApi, start_token_manager};
23//! # use brokapi_mocks::finam::{MockServer, SECRET};
24//!
25//! # #[tokio::main(flavor = "current_thread")]
26//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
27//! # let server = MockServer::start().await;
28//! # let endpoint = ApiEndpoint::new(server.url.clone());
29//! # let secret = SECRET;
30//! // For the real broker: `ApiEndpoint::default()` and your API secret.
31//! let tokens = start_token_manager(&endpoint, secret, "my-app").await?;
32//! let api = FinamApi::connect(&endpoint, tokens, false)?;
33//! let reply = api
34//!     .market_data()
35//!     .last_quote(QuoteRequest { symbol: "SBER@MISX".into() })
36//!     .await?
37//!     .into_inner();
38//! assert_eq!(reply.symbol, "SBER@MISX");
39//! # Ok(())
40//! # }
41//! ```
42
43#![warn(missing_docs)]
44
45pub mod auth;
46pub mod client;
47pub mod error;
48
49/// Generated messages and gRPC clients/servers.
50#[allow(clippy::all, clippy::pedantic, missing_docs)]
51pub mod pb {
52    include!(concat!(env!("OUT_DIR"), "/all.rs"));
53}
54
55/// The descriptors of every service and message of the API, as a serialized `FileDescriptorSet` (the
56/// `.proto` files this crate is built from, with their imports). Load it into a descriptor pool to call a
57/// method by its name, see [`FinamApi::grpc`].
58pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/descriptor.bin"));
59
60pub use auth::{GrpcTokenApi, TokenApi, TokenInfo, TokenManager};
61pub use client::{ApiEndpoint, Authorized, DEFAULT_ENDPOINT, FinamApi, start_token_manager};
62pub use error::FinamError;