Skip to content

OAuthTokenHandler

QuantConnect.Brokerages.Authentication.OAuthTokenHandler

OAuthTokenHandler(
    api_client: ApiConnection,
    model_request: QuantConnect_Brokerages_Authentication_OAuthTokenHandler_TRequest,
)

Bases: Generic[QuantConnect_Brokerages_Authentication_OAuthTokenHandler_TRequest, QuantConnect_Brokerages_Authentication_OAuthTokenHandler_TResponse], TokenHandler

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

Initializes a new instance of the OAuthTokenHandler{TRequest, TResponse} class.

Parameters:

Name Type Description Default
api_client ApiConnection

The API client used to communicate with the Lean platform.

required
model_request QuantConnect_Brokerages_Authentication_OAuthTokenHandler_TRequest

The request model used to generate the access token.

required

get_access_token

get_access_token(
    cancellation_token: CancellationToken,
) -> TokenCredentials

Retrieves a valid access token from the Lean platform. Caches and reuses tokens until expiration to minimize unnecessary requests.

Parameters:

Name Type Description Default
cancellation_token CancellationToken

A token used to observe cancellation requests.

required

Returns:

Type Description
TokenCredentials

A tuple containing the token type and access token string.

send

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

Sends an HTTP request synchronously with retry support. This override includes token-based authentication and refresh logic on 401 Unauthorized responses.

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.