Skip to main content

Client

Struct Client 

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

Authenticated ProjectX REST client.

The client uses the caller’s Tokio runtime and keeps its bearer token private. Call Client::authenticate before calling authenticated methods. Safe authenticated queries wait asynchronously for shared rate-limit capacity before their HTTP timeout begins. Money-moving mutations instead return Error::LocallyRateLimited without sending when capacity is full.

Implementations§

Source§

impl Client

Source

pub fn builder(credentials: Credentials) -> ClientBuilder

Starts configuring a client with explicit API-key credentials.

Source

pub fn application_builder(credentials: ApplicationCredentials) -> ClientBuilder

Starts configuring a client with authorized-application credentials.

Source

pub fn realtime(&self, hub: Hub) -> RealtimeClient

Creates a real-time client sharing this client’s rotating bearer token.

The returned hub snapshots the current token immediately before every initial connection and reconnect. Call Self::authenticate first.

Source

pub async fn authenticate(&self) -> Result<(), Error>

Authenticates with the endpoint selected by the configured credential type.

API-key credentials send exactly userName and apiKey to /api/Auth/loginKey. Authorized-application credentials send exactly userName, password, deviceId, appId, and verifyKey to /api/Auth/loginApp.

§Errors

Returns an error when transport fails, credentials are rejected, or the provider omits a usable token.

Source

pub async fn authenticate_with_validation( &self, period: Duration, ) -> Result<SessionValidator, Error>

Authenticates and starts periodic token validation.

The validator is cancelled when the returned guard is dropped. Prefer SessionValidator::shutdown when graceful task completion matters.

§Errors

Returns an error when the validation period is zero or cannot be represented by Tokio’s clock, or when authentication fails.

Source

pub async fn validate_session(&self) -> Result<(), Error>

Validates the current bearer token and applies provider token rotation.

§Errors

Returns an error when unauthenticated, transport fails, or validation is rejected. Once the validation request may have reached the provider, cancellation or an untrustworthy response invalidates that exact token revision and requires authentication before it can be used again.

Source

pub async fn logout(&self) -> Result<OperationResponse, Error>

Logs out the current provider session exactly once.

A request that may have reached the provider invalidates only the exact bearer-token revision it used, including when the future is cancelled or the response is ambiguous. A definitive connection failure or HTTP 429 retains that revision because the provider did not admit the logout.

§Errors

Returns an error when unauthenticated, locally or remotely rate limited, rejected by the provider, or when no trustworthy response is available.

Source

pub async fn ping(&self) -> Result<(), Error>

Checks whether the provider REST service is responsive.

This unauthenticated operation expects the provider’s exact pong response and applies the configured response-size bound.

§Errors

Returns an error for URL, transport, HTTP status, response-size, or unexpected-response failures.

Source

pub async fn search_active_accounts(&self) -> Result<Vec<Account>, Error>

Retrieves active accounts for the authenticated user.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn search_accounts( &self, only_active_accounts: bool, ) -> Result<Vec<Account>, Error>

Searches accounts for the authenticated user.

Set only_active_accounts to false to include inactive accounts. Use Self::search_active_accounts when only active accounts are required.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn available_contracts( &self, live: bool, ) -> Result<Vec<Contract>, Error>

Lists contracts available to the selected live or simulated data feed.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn search_contracts( &self, request: &SearchContracts, ) -> Result<Vec<Contract>, Error>

Searches contracts using provider-native search text.

The provider returns at most 20 matching contracts per request.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn contract_by_id( &self, contract_id: &ContractId, ) -> Result<Contract, Error>

Retrieves one contract by its explicit provider identifier.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn retrieve_bars( &self, request: &HistoryRequest, ) -> Result<Vec<Bar>, Error>

Retrieves historical bars for an explicit provider contract.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn search_orders( &self, request: &OrderSearch, ) -> Result<Vec<Order>, Error>

Searches historical orders for an account and time range.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn order_by_id( &self, account_id: AccountId, order_id: OrderId, ) -> Result<Order, Error>

Retrieves one order by its provider account and order identifiers.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn search_open_orders( &self, account_id: AccountId, ) -> Result<Vec<Order>, Error>

Searches currently open orders for an account.

The provider excludes Suspended orders from this legacy endpoint, including inactive bracket children. Use Self::query_orders and explicitly select every non-terminal status needed by the application when building a complete working-order reconciliation view.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn query_orders( &self, request: &OrderQuery, ) -> Result<OrderPage, Error>

Queries filtered orders through /api/Order/v2/query.

For a complete non-terminal working-order reconciliation, filter on OrderStatus::Open, OrderStatus::Pending, OrderStatus::PendingCancellation, and OrderStatus::Suspended. The last of these includes inactive bracket children omitted by Self::search_open_orders. Paginated callers must continue until the returned page is exhausted; request a total count when an explicit completion check is useful.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn place_order( &self, request: &PlaceOrder, ) -> Result<OrderResponse, Error>

Places an order exactly once.

This method never retries. An untrustworthy response is returned as Error::AmbiguousMutation; callers must reconcile open and recent orders before deciding whether another submission is safe.

§Errors

Returns an error for authentication, provider, decode, or ambiguous transport outcomes.

Source

pub async fn cancel_order( &self, request: &CancelOrder, ) -> Result<OperationResponse, Error>

Cancels an order exactly once.

This method never retries. If the provider may have admitted the request but no trustworthy result is available, it returns Error::AmbiguousMutation. Reconcile provider state before retrying.

§Errors

Returns an error for authentication, provider rejection, local rate limiting, or an ambiguous post-admission outcome.

Source

pub async fn modify_order( &self, request: &ModifyOrder, ) -> Result<OperationResponse, Error>

Modifies an open order exactly once.

This method never retries. If the provider may have admitted the request but no trustworthy result is available, it returns Error::AmbiguousMutation. Reconcile provider state before retrying.

§Errors

Returns an error for authentication, provider rejection, local rate limiting, or an ambiguous post-admission outcome.

Source

pub async fn search_open_positions( &self, account_id: AccountId, ) -> Result<Vec<Position>, Error>

Searches currently open positions for an account.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn close_contract( &self, request: &CloseContract, ) -> Result<OperationResponse, Error>

Closes the open position for an explicit account and contract exactly once.

This method never retries. If the provider may have admitted the request but no trustworthy result is available, it returns Error::AmbiguousMutation. Reconcile provider state before retrying.

§Errors

Returns an error for authentication, provider rejection, local rate limiting, or an ambiguous post-admission outcome.

Source

pub async fn partial_close_contract( &self, request: &PartialCloseContract, ) -> Result<OperationResponse, Error>

Partially closes an open position for an account and contract exactly once.

This method never retries. If the provider may have admitted the request but no trustworthy result is available, it returns Error::AmbiguousMutation. Reconcile provider state before retrying.

§Errors

Returns an error for authentication, provider rejection, local rate limiting, or an ambiguous post-admission outcome.

Source

pub async fn search_trades( &self, request: &TradeSearch, ) -> Result<Vec<Trade>, Error>

Searches executions for an account and time range.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Source

pub async fn query_trades( &self, request: &TradeQuery, ) -> Result<Vec<Trade>, Error>

Searches trades with optional start and end timestamp bounds.

Unlike Self::search_trades, a TradeQuery can omit either or both timestamp bounds to express the provider’s complete request schema.

§Errors

Returns an error for authentication, transport, provider, or decode failures.

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Self

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
Source§

impl Debug for Client

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

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.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

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
Source§

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
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more