pub struct HttpClient { /* private fields */ }Expand description
Simplified client for IG Markets API with automatic authentication
This client handles all authentication complexity internally, including:
- Initial login
- OAuth token refresh
- Re-authentication when tokens expire
- Account switching
- Rate limiting for all API requests
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub async fn new(config: Config) -> Result<Self, AppError>
pub async fn new(config: Config) -> Result<Self, AppError>
Creates a new client and performs initial authentication
§Arguments
config- Configuration containing credentials and API settings
§Returns
Ok(Client)- Authenticated client ready to useErr(AppError)- If authentication fails
§Errors
Returns AppError::Network if the underlying reqwest client cannot
be built (e.g. the system TLS backend fails to initialize), or any
AppError surfaced by the initial Auth::login call.
Sourcepub fn new_lazy(config: Config) -> Result<Self, AppError>
pub fn new_lazy(config: Config) -> Result<Self, AppError>
Creates a new client without performing initial authentication
§Errors
Returns AppError::Network if the HTTP client cannot be constructed.
Sourcepub async fn ws_info(&self) -> Result<WebsocketInfo, AppError>
pub async fn ws_info(&self) -> Result<WebsocketInfo, AppError>
Gets WebSocket connection information for Lightstreamer, reusing the cached session.
Delegates to Auth::ws_info, which returns the cached session when it
is valid and only logs in when needed.
§Returns
Ok(WebsocketInfo)- Server endpoint, authentication tokens, and account ID for the current session.Err(AppError)- If session retrieval (login / refresh) fails.
§Errors
Returns AppError when the session cannot be retrieved.
Sourcepub async fn get_ws_info(&self) -> WebsocketInfo
👎Deprecated: use ws_info() which reuses the cached session and returns a typed error instead of a default-on-error WebsocketInfo
pub async fn get_ws_info(&self) -> WebsocketInfo
use ws_info() which reuses the cached session and returns a typed error instead of a default-on-error WebsocketInfo
Gets WebSocket connection information for Lightstreamer
§Returns
WebsocketInfocontaining server endpoint, authentication tokens, and account ID
Sourcepub async fn get<T: DeserializeOwned>(
&self,
path: &str,
version: Option<u8>,
) -> Result<T, AppError>
pub async fn get<T: DeserializeOwned>( &self, path: &str, version: Option<u8>, ) -> Result<T, AppError>
Makes a GET request
Sourcepub async fn post<B: Serialize, T: DeserializeOwned>(
&self,
path: &str,
body: B,
version: Option<u8>,
) -> Result<T, AppError>
pub async fn post<B: Serialize, T: DeserializeOwned>( &self, path: &str, body: B, version: Option<u8>, ) -> Result<T, AppError>
Makes a POST request
Sourcepub async fn put<B: Serialize, T: DeserializeOwned>(
&self,
path: &str,
body: B,
version: Option<u8>,
) -> Result<T, AppError>
pub async fn put<B: Serialize, T: DeserializeOwned>( &self, path: &str, body: B, version: Option<u8>, ) -> Result<T, AppError>
Makes a PUT request
Sourcepub async fn delete<T: DeserializeOwned>(
&self,
path: &str,
version: Option<u8>,
) -> Result<T, AppError>
pub async fn delete<T: DeserializeOwned>( &self, path: &str, version: Option<u8>, ) -> Result<T, AppError>
Makes a DELETE request
Sourcepub async fn post_with_delete_method<B: Serialize, T: DeserializeOwned>(
&self,
path: &str,
body: B,
version: Option<u8>,
) -> Result<T, AppError>
pub async fn post_with_delete_method<B: Serialize, T: DeserializeOwned>( &self, path: &str, body: B, version: Option<u8>, ) -> Result<T, AppError>
Makes a POST request with _method: DELETE header
This is required by IG API for closing positions, as they don’t support DELETE requests with a body. Instead, they use POST with a special header.
§Arguments
path- API endpoint pathbody- Request body to sendversion- API version to use
§Returns
Deserialized response of type T
Sourcepub async fn request<B: Serialize, T: DeserializeOwned>(
&self,
method: Method,
path: &str,
body: Option<B>,
version: Option<u8>,
) -> Result<T, AppError>
pub async fn request<B: Serialize, T: DeserializeOwned>( &self, method: Method, path: &str, body: Option<B>, version: Option<u8>, ) -> Result<T, AppError>
Makes a request with custom API version
Sourcepub async fn switch_account(
&self,
account_id: &str,
default_account: Option<bool>,
) -> Result<(), AppError>
pub async fn switch_account( &self, account_id: &str, default_account: Option<bool>, ) -> Result<(), AppError>
Switches to a different trading account.
§Errors
Returns AppError::InvalidInput when the client was built with more
than one API key, and whatever Auth::switch_account reports
otherwise.
Switching is refused with a pool because it cannot be done coherently here: each key holds its own session, so switching only the primary leaves the other slots authenticated against the previous account and a non-trading read could rotate onto one of them and answer for the wrong account. Switching all of them instead would spend one request per key — 74 of them in this codebase’s demo setup — against the very allowance the pool exists to protect. The account also keys the shared account-wide budget, which would have to move with it. Build a client per account instead.
Sourcepub async fn get_session(&self) -> Result<Session, AppError>
pub async fn get_session(&self) -> Result<Session, AppError>
Gets the current session
Sourcepub fn auth(&self) -> &Auth
pub fn auth(&self) -> &Auth
Gets Auth reference
This is the primary slot’s Auth, which moves with the primary: the
session this exposes is always one that authenticated.
Sourcepub fn config(&self) -> &Config
pub fn config(&self) -> &Config
Returns the configuration this HTTP client was built with.
Config’s Debug / Display impls redact credentials and the database
URL, so the returned value can be rendered that way without leaking
secrets. Its Serialize impl does not redact — never serialize a
Config into logs, telemetry or an error payload.
Auto Trait Implementations§
impl !Freeze for HttpClient
impl !RefUnwindSafe for HttpClient
impl !UnwindSafe for HttpClient
impl Send for HttpClient
impl Sync for HttpClient
impl Unpin for HttpClient
impl UnsafeUnpin for HttpClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more