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
impl AlorRaw
Sourcepub async fn call<T, F, Fut>(&self, method: &str, f: F) -> Result<T, Error>
pub async fn call<T, F, Fut>(&self, method: &str, f: F) -> Result<T, Error>
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 codenot_foundfor 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.
Sourcepub async fn call_command<T, F, Fut>(
&self,
method: &str,
f: F,
) -> Result<T, Error>
pub async fn call_command<T, F, Fut>( &self, method: &str, f: F, ) -> Result<T, Error>
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: codehttp_<status>for a status from 400 to 499 except 429,not_foundfor 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;Sourcepub fn connect_ws(
&self,
event_tx: AsyncSender<WsIncoming>,
options: WsOptions,
) -> Result<WsSession, Error>
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 quotaConnectWsis spent. No socket is opened. - [
Error::Config] when the limits file routes the name to a bucket that does not exist.
Sourcepub async fn connect_cws(
&self,
answer_timeout: Duration,
) -> Result<CwsSession, Error>
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 quotaConnectCwsis spent. - [
Error::Transport] when the connection or the authorization fails.