Skip to content

LeanTokenHandler

QuantConnect.Brokerages.Authentication.LeanTokenHandler

LeanTokenHandler(
    create_auth_header: Callable[
        [TokenType, str], AuthenticationHeaderValue
    ] = None,
    handler: Any = None,
)

Bases: Generic[QuantConnect_Brokerages_Authentication_LeanTokenHandler_T]

Provides base functionality for token-based HTTP request handling. Token acquisition and retry logic are delegated entirely to get_access_token, implemented by derived classes (e.g., LeanOAuthTokenHandler).

Initializes a new instance of the LeanTokenHandler class.

This codeEntityType is protected.

Parameters:

Name Type Description Default
create_auth_header Callable[[TokenType, str], AuthenticationHeaderValue]

An optional delegate for creating an AuthenticationHeaderValue from the token type and access token. If not provided, a default implementation is used.

None

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_LeanTokenHandler_T

Retrieves a valid access token for authenticating HTTP requests. Must be implemented by derived classes to provide token type and value, with optional support for caching and refresh logic.

Parameters:

Name Type Description Default
cancellation_token CancellationToken

A cancellation token that can be used to cancel the token retrieval operation.

required

Returns:

Type Description
QuantConnect_Brokerages_Authentication_LeanTokenHandler_T

A TokenCredentials instance 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.