Skip to main content

FinamRaw

Struct FinamRaw 

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

Handle for calling any Finam method with the connector’s token, endpoint and rate limiter.

Get it from MarketDataConnector::raw or UserDataConnector::raw. Cheap to clone. It stays usable while the connector it came from is alive. See the module docs for the rules of a call.

§Examples

A command goes through call_command, a read through call:

use brokapi_core::Error;
use brokapi_finam::limits::default_limits;
use brokapi_finam::{UserDataConfig, UserDataConnector};
use brokapi_runtime::BrokerLimiter;
use brokapi_finam_protocol::pb::grpc::tradeapi::v1::accounts::GetAccountRequest;
use tokio::sync::mpsc;

let (events, _rx) = mpsc::channel(16);
let ud = UserDataConnector::start(
    UserDataConfig::new(api, "ACC1", "finam-ud"),
    BrokerLimiter::new(&default_limits()),
    events,
)
.await?;
let raw = ud.raw();
assert_eq!(raw.account_id(), Some("ACC1"));

let account = raw
    .call("GetAccount", |api| async move {
        api.accounts().get_account(GetAccountRequest { account_id: "ACC1".into() }).await
    })
    .await?;
assert_eq!(account.account_id, "ACC1");

// An account the broker does not know is a refusal.
let err = raw
    .call("GetAccount", |api| async move {
        api.accounts().get_account(GetAccountRequest { account_id: "NOPE".into() }).await
    })
    .await
    .unwrap_err();
assert!(matches!(err, Error::Rejected { .. }));

Implementations§

Source§

impl FinamRaw

Source

pub fn account_id(&self) -> Option<&str>

Account of the connector this came from. None for a market data connector.

Source

pub fn timeout(&self) -> Duration

Deadline applied to every call (FinamApiConfig::request_timeout of the connector).

Source

pub fn limiter(&self) -> &Arc<BrokerLimiter> ⓘ

The limiter shared with the connector.

Source

pub async fn call<T, F, Fut>(&self, method: &str, f: F) -> Result<T, Error>
where F: Fn(FinamApi) -> Fut, Fut: Future<Output = Result<Response<T>, Status>>,

Reads: quota for method, deadline, one re-auth retry, error mapping.

f may run twice (after a re-auth), hence Fn. For a server-streaming method the result is tonic::Streaming<_>: the quota covers opening the stream. A stream opened here is not supervised: when it breaks or the token changes, open it again yourself.

Do not use it for a call that changes the account, because of the repeat. Use call_command.

§Errors
  • [Error::RateLimited]: the local quota for method is spent. Nothing was sent.
  • [Error::Rejected]: the broker refused (bad argument, unknown id, no permission, or the token was refused again after the renewal).
  • [Error::Transport]: connection failure, or no answer within timeout.

A failed token renewal is reported with the same mapping.

§Examples

See the module docs.

Source

pub async fn call_command<T, F, Fut>( &self, method: &str, f: F, ) -> Result<T, Error>
where F: Fn(FinamApi) -> Fut, Fut: Future<Output = Result<Response<T>, Status>>,

Commands: quota for method, deadline, NO retry. Anything that may have reached the broker (timeout, break, server fault) is Error::Unknown.

There is no repeat on purpose: a second send could change the account twice. When the answer is Unknown, read the state (orders, positions) before deciding anything.

The order index of the connector does not see what you place here: no events, no reconcile. For plain and stop orders use the shared traits.

§Errors
  • [Error::RateLimited]: the local quota for method is spent. Nothing was sent.
  • [Error::Rejected]: the broker certainly did not process the request (for example INVALID_ARGUMENT or NOT_FOUND, which is the code not_found).
  • [Error::Config]: UNAUTHENTICATED, the token is refused. A command is not repeated after a token renewal.
  • [Error::Unknown]: timeout, broken connection or server fault. The command may have been applied.
§Examples
use brokapi_core::Error;
use brokapi_finam::limits::default_limits;
use brokapi_finam::{MarketDataConfig, MarketDataConnector};
use brokapi_runtime::BrokerLimiter;
use brokapi_finam_protocol::pb::grpc::tradeapi::v1::marketdata::QuoteRequest;
use tokio::sync::mpsc;

// Same shape as a read, but never repeated. The result of a real command is handled the same way.
let sent = md
    .raw()
    .call_command("LastQuote", |api| async move {
        api.market_data().last_quote(QuoteRequest { symbol: "SBER@MISX".into() }).await
    })
    .await;
match sent {
    Ok(reply) => assert_eq!(reply.symbol, "SBER@MISX"),
    Err(Error::Unknown) => { /* read the state before acting */ }
    Err(other) => panic!("refused: {other}"),
}

Trait Implementations§

Source§

impl Clone for FinamRaw

Source§

fn clone(&self) -> FinamRaw

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