pub struct CwsSession { /* private fields */ }Expand description
An authorized connection to the trading channel.
§Examples
use std::time::Duration;
use serde_json::json;
use brokapi_alor_protocol::http::client::AlorHttpClient;
use brokapi_alor_protocol::ws::cws::{CwsSession, opcodes};
let http = AlorHttpClient::new(cfg)?;
let cws = CwsSession::connect(http, Duration::from_secs(5)).await?;
let reply = cws
.command(opcodes::CREATE_LIMIT, json!({
"side": "buy", "quantity": 1, "price": 100.0,
"instrument": {"symbol": "SBER", "exchange": "MOEX", "instrumentGroup": "TQBR"},
"user": {"portfolio": "D00001"},
}))
.await?;
assert_eq!(reply.code, 200);
assert!(reply.order_number.is_some());Implementations§
Source§impl CwsSession
impl CwsSession
Sourcepub async fn connect(
http: AlorHttpClient,
answer_timeout: Duration,
) -> Result<Self, AlorApiError>
pub async fn connect( http: AlorHttpClient, answer_timeout: Duration, ) -> Result<Self, AlorApiError>
Opens the connection and authorizes it. answer_timeout is how long a command waits for its answer.
§Errors
AlorApiError::Ws when the connection or the authorization fails; the errors of
AlorHttpClient::get_access_token when there is no token.
Authorizes the connection with a fresh access token.
§Errors
AlorApiError::Ws when the broker refuses (code 401) or does not answer in time.
Sourcepub async fn command(
&self,
opcode: &str,
fields: Value,
) -> Result<CwsReply, AlorApiError>
pub async fn command( &self, opcode: &str, fields: Value, ) -> Result<CwsReply, AlorApiError>
Sends a command and waits for its answer. fields is the object of the order (side, quantity,
price, instrument, user, …); the session adds opcode and a new guid.
A refusal of the broker (code 400) is returned as a reply, not as an error: the caller reads
CwsReply::code and CwsReply::message.
§Errors
AlorApiError::Ws when the connection breaks or no answer comes in time. Both are an unknown
outcome for the command.