TokenHandler
QuantConnect.Brokerages.Authentication.TokenHandler
TokenHandler(
create_auth_header: Callable[
[TokenType, str], AuthenticationHeaderValue
] = None,
retry_interval: Optional[timedelta] = None,
)
Bases: DelegatingHandler
Provides base functionality for token-based HTTP request handling, including automatic retries and token refresh on unauthorized responses.
Initializes a new instance of the TokenHandler 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
|
retry_interval
|
Optional[timedelta]
|
An optional time interval to wait between retry attempts when fetching the token or retrying a failed request. If null, the default interval of 5 seconds is used. |
None
|
get_access_token
get_access_token(
cancellation_token: CancellationToken,
) -> TokenCredentials
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 |
|---|---|
TokenCredentials
|
A TokenCredentials instance 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. |