Skip to main content

AlorRaw

Struct AlorRaw 

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

A handle for calling any ALOR API method with a connector’s token and rate limiter.

Get it from raw() on MarketDataConnector or UserDataConnector. It is cheap to clone, and all clones share the token and the limiter. See the module docs for the rules of a call.

Implementations§

Source§

impl AlorRaw

Source

pub async fn call<T, F, Fut>(&self, method: &str, f: F) -> Result<T, Error>
where F: FnOnce(AlorHttpClient) -> Fut, Fut: Future<Output = Result<T, AlorApiError>>,

Calls a read-only API method: takes the quota for method, then runs f.

method is the name of the quota route, not a URL. Take a name from limits.toml (for example GetOrders). An unknown name spends the default route "*". f receives the connector’s [AlorHttpClient] and returns the future of one API call. Do not use this for anything that changes the account, use call_command.

§Errors
  • [Error::RateLimited] when the quota is spent (nothing was sent), or the broker answers HTTP 429.
  • [Error::Rejected] with code not_found for HTTP 404.
  • [Error::Transport] for everything else: no connection, a timeout, an unreadable answer, any other HTTP status. A read can be repeated.
  • [Error::Config] when the limits file routes the name to a bucket that does not exist, or the broker refuses the token.

The full table is in crate::error::map_err.

Source

pub async fn call_command<T, F, Fut>( &self, method: &str, f: F, ) -> Result<T, Error>
where F: FnOnce(AlorHttpClient) -> Fut, Fut: Future<Output = Result<T, AlorApiError>>,

Calls an API method that changes the account: takes the quota for method, then runs f.

Like call, but anything that may have reached the broker is [Error::Unknown]. The library never repeats the call: a retry of a lost place request could create a second order. Find out what happened (read the order, run reconcile) before you decide to send it again. The order is not tracked by the connector (see the module docs).

§Errors
  • [Error::Unknown] when there was no answer, a break or a timeout, HTTP 500 or above, or an answer that cannot be read. The command may have been done.
  • [Error::Rejected] when the broker answered and said no: code http_<status> for a status from 400 to 499 except 429, not_found for 404. Nothing was done.
  • [Error::RateLimited] when the quota is spent (nothing was sent) or the broker answers HTTP 429.
  • [Error::Config] when the limits file routes the name to a bucket that does not exist.

The full table is in crate::error::map_command_err.

§Examples

An order sent through the raw layer, refused by the broker (the mock refuses a limit order at REJECT_PRICE). The refusal is a definite [Error::Rejected], not an unknown outcome:

use brokapi_alor_protocol::http::models::{InstrumentRef, LimitOrderRequest, UserRef};
use brokapi_alor::limits::default_limits;
use brokapi_alor::{UserDataConfig, UserDataConnector};
use brokapi_conformance::REJECT_PRICE;
use brokapi_core::{Error, ShutdownMode};
use brokapi_runtime::BrokerLimiter;
use tokio::sync::mpsc;

let ud = UserDataConnector::start(UserDataConfig::new(cfg, "alor-ud"), limiter, events_tx).await?;

let order = LimitOrderRequest {
    side: "buy".into(),
    quantity: 1,
    price: REJECT_PRICE.decimal(),
    instrument: InstrumentRef { symbol: "SBER".into(), exchange: "MOEX".into(), instrument_group: Some("TQBR".into()) },
    user: UserRef { portfolio: "D00001".into() },
    comment: None,
    time_in_force: None,
    allow_margin: None,
    iceberg_fixed: None,
    iceberg_variance: None,
};
let result = ud
    .raw()
    .call_command("PlaceOrder", |http| async move {
        http.create_limit_order("my-own-request-id-1", &order).await
    })
    .await;
assert!(matches!(result, Err(Error::Rejected { .. })));
ud.shutdown(ShutdownMode::Immediate).await;
Source

pub fn connect_ws( &self, event_tx: AsyncSender<WsIncoming>, options: WsOptions, ) -> Result<WsSession, Error>

Opens one more session of the data channel /ws (for the subscriptions the connectors do not make: risks, summaries, instruments, …) with the connector’s token.

The quota name is ConnectWs. The session reconnects and resubscribes on its own. Frames arrive on event_tx, parsed. The returned [WsSession] connects in the background: it does not wait for the connection. Read the receiver of event_tx, or the session drops frames when the channel is full (see WsSession::dropped_events). The broker allows 10 connections of a user.

§Errors
  • [Error::RateLimited] when the quota ConnectWs is spent. No socket is opened.
  • [Error::Config] when the limits file routes the name to a bucket that does not exist.
Source

pub async fn connect_cws( &self, answer_timeout: Duration, ) -> Result<CwsSession, Error>

Opens the trading channel /cws with the connector’s token and authorizes it. answer_timeout is how long a command waits for its answer.

The quota name is ConnectCws. The channel gives the order commands that HTTP gives; the connectors use HTTP. See [CwsSession].

§Errors
  • [Error::RateLimited] when the quota ConnectCws is spent.
  • [Error::Transport] when the connection or the authorization fails.

Trait Implementations§

Source§

impl Clone for AlorRaw

Source§

fn clone(&self) -> AlorRaw

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> 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> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

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