Skip to main content

TbankConfig

Struct TbankConfig 

Source
pub struct TbankConfig {
    pub token: String,
    pub endpoint: String,
    pub sandbox: bool,
    pub account_id: Option<String>,
    pub app_name: Option<String>,
    pub request_timeout: Duration,
    pub connect_timeout: Duration,
    pub root_certificate_pem: Option<Vec<u8>>,
}
Expand description

gRPC client configuration. The token is never printed by Debug.

One config describes one token and one endpoint. Several accounts of the same token share the token and differ in account_id: build one config per account with with_account.

§Examples

use brokapi_tbank_protocol::{TbankConfig, PROD_ENDPOINT};

let prod = TbankConfig::production("t.my-token");
assert_eq!(prod.endpoint, PROD_ENDPOINT);
assert!(!prod.sandbox);

// Two accounts of one token: same token, different `account_id`.
let main = prod.clone().with_account("acc-main");
let second = prod.with_account("acc-second");
assert_eq!(main.token, second.token);
assert_ne!(main.account_id, second.account_id);

Fields§

§token: String

API token, issued in the T-Bank Invest settings (a sandbox token for the sandbox endpoint).

It is sent in the authorization: Bearer header of every request. Treat it as a secret: keep it out of logs and source control. Debug prints <redacted> instead of it.

§endpoint: String

Full URL, https://host:443 (TLS) or http://host:port (plain, for mocks).

§sandbox: bool

Sandbox flag. Selects the sandbox endpoint in new, and tells the connector the account is virtual.

§account_id: Option<String>

Brokerage account id. When None the connector picks the first open account of the token.

§app_name: Option<String>

x-app-name header (<github nick>.<repo> is the T-Bank convention).

§request_timeout: Duration

Deadline of one unary call. Default 30 s.

TbankClient does not apply it. The connectors in brokapi-tbank cut every call at this deadline. For a command (place, cancel, replace) a timeout means the outcome is unknown, because the broker may have acted. See the connector docs.

§connect_timeout: Duration

How long the channel waits for the TCP and TLS connection to be set up. Default 10 s.

It applies to every (re)connect of the lazy channel. There is no builder method, set the field.

§root_certificate_pem: Option<Vec<u8>>

An extra root certificate (PEM) the TLS connection trusts besides the bundled web PKI roots. None by default.

Set it when the broker’s endpoint is served with a certificate of a national root (the Russian Ministry of Digital Development one) that the bundled roots do not know. Not verified against the live endpoint which certificate it presents. See with_root_certificate_pem.

Implementations§

Source§

impl TbankConfig

Source

pub fn new(token: impl Into<String>, sandbox: bool) -> Self

Production or sandbox endpoint chosen by sandbox.

Timeouts take their defaults (30 s per request, 10 s to connect). No account is chosen and no app name is set.

§Examples
use brokapi_tbank_protocol::{TbankConfig, PROD_ENDPOINT, SANDBOX_ENDPOINT};

assert_eq!(TbankConfig::new("t.token", true).endpoint, SANDBOX_ENDPOINT);
assert_eq!(TbankConfig::new("t.token", false).endpoint, PROD_ENDPOINT);
Source

pub fn sandbox(token: impl Into<String>) -> Self

Sandbox endpoint (SANDBOX_ENDPOINT). Accounts there are virtual. Use a sandbox token.

§Examples
let cfg = brokapi_tbank_protocol::TbankConfig::sandbox("t.sandbox-token");
assert!(cfg.sandbox);
Source

pub fn production(token: impl Into<String>) -> Self

Production endpoint (PROD_ENDPOINT). Orders placed through it are real and cost money.

§Examples
let cfg = brokapi_tbank_protocol::TbankConfig::production("t.token");
assert!(!cfg.sandbox);
Source

pub fn for_mock(token: impl Into<String>, endpoint: impl Into<String>) -> Self

Plain-HTTP endpoint of a local mock server (tests and examples).

endpoint is a full URL such as http://127.0.0.1:50051. The config is marked as sandbox, so a connector built from it reports sandbox: true in its capabilities. Do not use it with a real token.

§Examples
use brokapi_tbank_protocol::TbankConfig;

let cfg = TbankConfig::for_mock(TOKEN, &server.endpoint);
assert!(cfg.endpoint.starts_with("http://"));
assert!(cfg.sandbox);
Source

pub fn from_env() -> Result<Self, ProtocolError>

Reads the config from the environment. Loads a .env file of the working directory first, if there is one (parent directories are not searched, see from_env_file for another path).

Variables: TBANK_TOKEN (required), TBANK_SANDBOX (true, 1 or yes, default false, so production), TBANK_ENDPOINT (overrides the endpoint when not empty, must start with https://), TBANK_ACCOUNT_ID and TBANK_APP_NAME (both optional, empty means not set).

Note the default: without TBANK_SANDBOX the config points at production.

§Errors

ProtocolError::Config when TBANK_TOKEN is not set or is not valid unicode, or when TBANK_ENDPOINT is set and is not an https:// address (a token must not go over plain text because of a stray variable). The address of a local imitation is set in code, with for_mock.

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

let cfg = TbankConfig::from_env()?;
assert_eq!(cfg.endpoint, SANDBOX_ENDPOINT);
assert_eq!(cfg.account_id.as_deref(), Some("acc-main"));

// A plain-text endpoint in the environment is refused.
assert!(TbankConfig::from_env().is_err());

// Without a token the call fails, it never falls back to a default.
assert!(TbankConfig::from_env().is_err());
Source

pub fn from_env_file(path: impl AsRef<Path>) -> Result<Self, ProtocolError>

Like from_env, but loads the .env file at path (a file that is not there is skipped, variables that are already set are kept).

§Errors

As from_env.

§Examples
use brokapi_tbank_protocol::TbankConfig;

// No such file: only the environment is read, and it has no token here.
assert!(TbankConfig::from_env_file("/no/such/.env").is_err());
Source

pub fn with_account(self, account_id: impl Into<String>) -> Self

Pins the brokerage account this config works with.

A user data connector trades on exactly this account. Without it the connector takes the token’s only open account and refuses to start when there are several. Use TbankClient::list_accounts to see the ids.

§Examples
let cfg = brokapi_tbank_protocol::TbankConfig::sandbox("t.token").with_account("acc-main");
assert_eq!(cfg.account_id.as_deref(), Some("acc-main"));
Source

pub fn with_app_name(self, app_name: impl Into<String>) -> Self

Sets the x-app-name header sent with every request.

The T-Bank convention is <github nick>.<repo>. The name must be a valid header value, otherwise TbankClient::new returns ProtocolError::Config.

§Examples
let cfg = brokapi_tbank_protocol::TbankConfig::sandbox("t.token").with_app_name("me.my-robot");
assert_eq!(cfg.app_name.as_deref(), Some("me.my-robot"));
Source

pub fn with_root_certificate_pem(self, pem: impl Into<Vec<u8>>) -> Self

Trusts one more root certificate for the TLS connection, in PEM form (the text between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----).

The bundled web PKI roots stay trusted. Use it when the endpoint’s certificate chains to a root they do not contain (for example the national root certificate). TbankClient::new refuses a PEM text that holds no certificate.

§Examples
let cfg = brokapi_tbank_protocol::TbankConfig::production("t.token")
    .with_root_certificate_pem(b"-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----\n".to_vec());
assert!(cfg.root_certificate_pem.is_some());
Source

pub fn with_request_timeout(self, timeout: Duration) -> Self

Sets the deadline of one unary call, see request_timeout.

§Examples
use std::time::Duration;

let cfg = brokapi_tbank_protocol::TbankConfig::sandbox("t.token").with_request_timeout(Duration::from_secs(5));
assert_eq!(cfg.request_timeout, Duration::from_secs(5));

Trait Implementations§

Source§

impl Clone for TbankConfig

Source§

fn clone(&self) -> TbankConfig

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
Source§

impl Debug for TbankConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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