Skip to content

LeanOAuthTokenHandler

QuantConnect.Brokerages.Authentication.LeanOAuthTokenHandler

LeanOAuthTokenHandler(
    api_client: ApiConnection,
    request: OAuthTokenRequest,
    token_lifetime: timedelta,
)

Bases: Generic[QuantConnect_Brokerages_Authentication_LeanOAuthTokenHandler_T], LeanTokenHandler[QuantConnect_Brokerages_Authentication_LeanOAuthTokenHandler_T]

Handles OAuth token retrieval and caching by interacting with the Lean platform. Implements retry and expiration logic for secure HTTP communication.

max_retry_count

max_retry_count: int

The maximum number of retry attempts when fetching an access token.

This codeEntityType is protected.

retry_interval

retry_interval: timedelta

The time interval to wait between retry attempts when fetching an access token.

This codeEntityType is protected.

offset_before_expiration

offset_before_expiration: timedelta

Some padding before expiration to request a new token

authentication_failed

authentication_failed: _EventContainer[
    Callable[[Object, Exception], Any], Any
]

Raised when authentication fails after all retry attempts are exhausted. Subscribers can use this to trigger graceful application shutdown.

get_access_token

get_access_token(
    cancellation_token: CancellationToken,
) -> QuantConnect_Brokerages_Authentication_LeanOAuthTokenHandler_T

Retrieves a valid access token from the Lean platform. Caches and reuses tokens until expiration to minimize unnecessary requests. Retries up to max_retry_count times on failure, and is thread-safe via double-checked locking.

Parameters:

Name Type Description Default
cancellation_token CancellationToken

A token used to observe cancellation requests.

required

Returns:

Type Description
QuantConnect_Brokerages_Authentication_LeanOAuthTokenHandler_T

A TokenCredentials containing the token type and access token string.

on_authentication_failed

on_authentication_failed(exception: Exception) -> None

Invokes the authentication_failed event. Derived classes call this when authentication fails after exhausting all retry attempts.

This codeEntityType is protected.

Parameters:

Name Type Description Default
exception Exception

The exception that caused the authentication failure.

required

send

send(
    request: Any, cancellation_token: CancellationToken
) -> Any

Sends an HTTP request synchronously, applying token-based authentication.

This codeEntityType is protected.

Parameters:

Name Type Description Default
request Any

The HTTP request message to send.

required
cancellation_token CancellationToken

A cancellation token to cancel operation.

required

Returns:

Type Description
Any

The HTTP response message.

send_async

send_async(
    request: Any, cancellation_token: CancellationToken
) -> Task[HttpResponseMessage]

Sends an HTTP request asynchronously by internally invoking the synchronous send(HttpRequestMessage, CancellationToken) method. This is useful for compatibility with components that require an asynchronous pipeline, even though the core logic is synchronous.

This codeEntityType is protected.

Parameters:

Name Type Description Default
request Any

The HTTP request message to send.

required
cancellation_token CancellationToken

A cancellation token to cancel the operation.

required

Returns:

Type Description
Task[HttpResponseMessage]

A task representing the asynchronous operation, containing the HTTP response message.