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
impl Client
Sourcepub fn builder(credentials: Credentials) -> ClientBuilder
pub fn builder(credentials: Credentials) -> ClientBuilder
Starts configuring a client with explicit API-key credentials.
Sourcepub fn application_builder(credentials: ApplicationCredentials) -> ClientBuilder
pub fn application_builder(credentials: ApplicationCredentials) -> ClientBuilder
Starts configuring a client with authorized-application credentials.
Sourcepub fn realtime(&self, hub: Hub) -> RealtimeClient
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.
Sourcepub async fn authenticate(&self) -> Result<(), Error>
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.
Sourcepub async fn authenticate_with_validation(
&self,
period: Duration,
) -> Result<SessionValidator, Error>
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.
Sourcepub async fn validate_session(&self) -> Result<(), Error>
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.
Sourcepub async fn logout(&self) -> Result<OperationResponse, Error>
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.
Sourcepub async fn ping(&self) -> Result<(), Error>
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.
Sourcepub async fn search_active_accounts(&self) -> Result<Vec<Account>, Error>
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.
Sourcepub async fn search_accounts(
&self,
only_active_accounts: bool,
) -> Result<Vec<Account>, Error>
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.
Sourcepub async fn available_contracts(
&self,
live: bool,
) -> Result<Vec<Contract>, Error>
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.
Sourcepub async fn search_contracts(
&self,
request: &SearchContracts,
) -> Result<Vec<Contract>, Error>
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.
Sourcepub async fn contract_by_id(
&self,
contract_id: &ContractId,
) -> Result<Contract, Error>
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.
Sourcepub async fn retrieve_bars(
&self,
request: &HistoryRequest,
) -> Result<Vec<Bar>, Error>
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.
Sourcepub async fn search_orders(
&self,
request: &OrderSearch,
) -> Result<Vec<Order>, Error>
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.
Sourcepub async fn order_by_id(
&self,
account_id: AccountId,
order_id: OrderId,
) -> Result<Order, Error>
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.
Sourcepub async fn search_open_orders(
&self,
account_id: AccountId,
) -> Result<Vec<Order>, Error>
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.
Sourcepub async fn query_orders(
&self,
request: &OrderQuery,
) -> Result<OrderPage, Error>
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.
Sourcepub async fn place_order(
&self,
request: &PlaceOrder,
) -> Result<OrderResponse, Error>
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.
A returned order ID alone does not imply acceptance: provider code 2
can accompany a rejected order record and remains Error::Provider.
§Errors
Returns an error for authentication, provider, decode, or ambiguous transport outcomes.
Sourcepub async fn cancel_order(
&self,
request: &CancelOrder,
) -> Result<OperationResponse, Error>
pub async fn cancel_order( &self, request: &CancelOrder, ) -> Result<OperationResponse, Error>
Cancels an order exactly once.
The provider supports direct cancellation only for simulated accounts
that are not copy-trading followers. Unsupported accounts return
provider code 6; the client leaves account eligibility to the provider.
See the cancellation reference.
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.
Sourcepub async fn modify_order(
&self,
request: &ModifyOrder,
) -> Result<OperationResponse, Error>
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.
Sourcepub async fn search_open_positions(
&self,
account_id: AccountId,
) -> Result<Vec<Position>, Error>
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.
Sourcepub async fn close_contract(
&self,
request: &CloseContract,
) -> Result<OperationResponse, Error>
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.
Sourcepub async fn partial_close_contract(
&self,
request: &PartialCloseContract,
) -> Result<OperationResponse, Error>
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.
Sourcepub async fn search_trades(
&self,
request: &TradeSearch,
) -> Result<Vec<Trade>, Error>
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.
Sourcepub async fn query_trades(
&self,
request: &TradeQuery,
) -> Result<Vec<Trade>, Error>
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.