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: StringAPI 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: StringFull URL, https://host:443 (TLS) or http://host:port (plain, for mocks).
sandbox: boolSandbox 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: DurationDeadline 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: DurationHow 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
impl TbankConfig
Sourcepub fn new(token: impl Into<String>, sandbox: bool) -> Self
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);Sourcepub fn sandbox(token: impl Into<String>) -> Self
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);Sourcepub fn production(token: impl Into<String>) -> Self
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);Sourcepub fn for_mock(token: impl Into<String>, endpoint: impl Into<String>) -> Self
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);Sourcepub fn from_env() -> Result<Self, ProtocolError>
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());Sourcepub fn from_env_file(path: impl AsRef<Path>) -> Result<Self, ProtocolError>
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());Sourcepub fn with_account(self, account_id: impl Into<String>) -> Self
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"));Sourcepub fn with_app_name(self, app_name: impl Into<String>) -> Self
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"));Sourcepub fn with_root_certificate_pem(self, pem: impl Into<Vec<u8>>) -> Self
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());Sourcepub fn with_request_timeout(self, timeout: Duration) -> Self
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
impl Clone for TbankConfig
Source§fn clone(&self) -> TbankConfig
fn clone(&self) -> TbankConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for TbankConfig
impl RefUnwindSafe for TbankConfig
impl Send for TbankConfig
impl Sync for TbankConfig
impl Unpin for TbankConfig
impl UnsafeUnpin for TbankConfig
impl UnwindSafe for TbankConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].