Skip to main content

TbankClient

Struct TbankClient 

Source
pub struct TbankClient { /* private fields */ }
Expand description

Shared HTTP/2 channel. Cheap to clone; each accessor returns a fresh generated client.

All generated clients made from one TbankClient share one connection and one token. The channel connects lazily and reconnects on failure, so TbankClient::new never blocks and never fails because the server is down.

This type does not limit the request rate and does not apply request_timeout. The connectors in brokapi-tbank do both. If you call the generated clients directly, you must keep to the broker’s rate limits yourself.

§Examples

use brokapi_tbank_protocol::v1::GetAccountsRequest;
use brokapi_tbank_protocol::{TbankClient, TbankConfig};

let client = TbankClient::new(&cfg)?;
// Any method of the API is reachable through the generated clients.
let reply = client.users().get_accounts(GetAccountsRequest { status: None }).await?;
assert!(!reply.into_inner().accounts.is_empty());

Implementations§

Source§

impl TbankClient

Source

pub fn new(cfg: &TbankConfig) -> Result<Self, ProtocolError>

Builds the channel without touching the network (connects on first use, reconnects on failure).

An https:// endpoint gets TLS with the bundled web PKI roots, and the extra root certificate of TbankConfig::root_certificate_pem if there is one. An http:// endpoint is plain text and meant for a local mock. HTTP/2 keep-alive pings run every 5 minutes and only while a call or a stream is open: a server counts pings on an idle connection as abuse and closes it (too_many_pings), so none are sent when nothing is open. A dead stream is found by the silence limit of the connector.

§Errors
§Examples
use brokapi_tbank_protocol::{ProtocolError, TbankClient, TbankConfig};

// The sandbox endpoint is not contacted here: the channel is lazy.
assert!(TbankClient::new(&TbankConfig::sandbox("t.my-token")).is_ok());

// A bad endpoint is reported at once.
let mut cfg = TbankConfig::sandbox("t.my-token");
cfg.endpoint = "not a url".into();
assert!(matches!(TbankClient::new(&cfg), Err(ProtocolError::Config(_))));
Source

pub fn grpc(&self) -> Grpc<Authed>

A generic gRPC client over the same channel and token, for a call that is not made through a generated client: the method is named by a path (/package.Service/Method) and the messages are whatever codec you give it. With FILE_DESCRIPTOR_SET it calls any method of the API by name.

Source

pub fn market_data(&self) -> MarketDataServiceClient<Authed>

Client of MarketDataService: unary market data: order book snapshot, last trades, candles, trading status.

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn market_data_stream(&self) -> MarketDataStreamServiceClient<Authed>

Client of MarketDataStreamService: the bidirectional market data stream (order book, trades, candles subscriptions).

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn orders(&self) -> OrdersServiceClient<Authed>

Client of OrdersService: orders: place, cancel, replace, order state, active orders.

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn orders_stream(&self) -> OrdersStreamServiceClient<Authed>

Client of OrdersStreamService: server streams of own order states and own trades.

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn operations(&self) -> OperationsServiceClient<Authed>

Client of OperationsService: portfolio, positions, operation history.

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn operations_stream(&self) -> OperationsStreamServiceClient<Authed>

Client of OperationsStreamService: server streams of portfolio and positions changes.

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn users(&self) -> UsersServiceClient<Authed>

Client of UsersService: accounts, user info, tariff and limits.

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn instruments(&self) -> InstrumentsServiceClient<Authed>

Client of InstrumentsService: the instrument directory: search, listings, schedules, reference data.

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn sandbox(&self) -> SandboxServiceClient<Authed>

Client of SandboxService: sandbox methods: open virtual accounts, top up virtual money (meant for the sandbox endpoint).

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn stop_orders(&self) -> StopOrdersServiceClient<Authed>

Client of StopOrdersService: stop orders: place, cancel, list (unary only, no stream).

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source

pub fn signals(&self) -> SignalServiceClient<Authed>

Client of SignalService: signals and strategies.

Returns a fresh generated client on the shared channel. It is cheap, create one per call.

Source§

impl TbankClient

Source

pub async fn list_accounts(&self) -> Result<Vec<Account>, Status>

UsersService/GetAccounts: every account the token can see (a token covers several accounts; pick the id to put into the connector config as account_id).

No status filter is sent. Whether the broker then returns only open accounts or all of them is not verified against the live API, so check status of each Account before you trade on it. The call is not rate limited here.

§Errors

The tonic::Status of the failed call: Unauthenticated for a bad token, Unavailable or DeadlineExceeded for network trouble. The call is a read, so repeating it is safe.

§Examples
use brokapi_tbank_protocol::{TbankClient, TbankConfig};

let client = TbankClient::new(&cfg)?;
let ids: Vec<String> = client.list_accounts().await?.into_iter().map(|a| a.id).collect();
// One connector per account: put each id into `TbankConfig::with_account`.
assert!(ids.len() >= 2);

// A wrong token is refused by the server and shows as a status, not a panic.
let bad = TbankClient::new(&TbankConfig::for_mock("wrong-token", &server.endpoint))?;
let status = bad.list_accounts().await.unwrap_err();
assert_eq!(status.code(), tonic::Code::Unauthenticated);

Trait Implementations§

Source§

impl Clone for TbankClient

Source§

fn clone(&self) -> TbankClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more