Class PollingHttpClient<TRequest, TResponse>
- Namespace
- FoundationaLLM.Common.Clients
- Assembly
- FoundationaLLM.Common.dll
Provides a generic HTTP client that can be used to poll for a response.
public class PollingHttpClient<TRequest, TResponse> where TRequest : CompletionRequestBase where TResponse : CompletionResponseBase
Type Parameters
TRequest
The type of the payload to send to start the operation.
TResponse
The type of the response received when the operation is completed.
- Inheritance
-
PollingHttpClient<TRequest, TResponse>
- Inherited Members
- Extension Methods
Constructors
PollingHttpClient(HttpClient, HttpClient, TRequest?, string, string, TimeSpan, TimeSpan, ILogger)
Provides a generic HTTP client that can be used to poll for a response.
public PollingHttpClient(HttpClient operationStarterHttpClient, HttpClient operationRetrieverHttpClient, TRequest? request, string operationStarterPath, string operationRetrieverPathTemplate, TimeSpan pollingInterval, TimeSpan maxWaitTime, ILogger logger)
Parameters
operationStarterHttpClient
HttpClientThe HttpClient used to start the operation.
operationRetrieverHttpClient
HttpClientThe HttpClient used to retrieve the operation (including its status).
request
TRequestThe
TRequest
request to send to the service.operationStarterPath
stringThe path used to start the operation.
operationRetrieverPathTemplate
stringThe path template used to retrieve the operation (including its status).
pollingInterval
TimeSpanThe TimeSpan interval to poll for the response.
maxWaitTime
TimeSpanThe TimeSpan maximum time to wait for the response.
logger
ILoggerThe logger used for logging.
Methods
ExecuteOperationAsync(CancellationToken)
Executes an operation and waits for the response using a polling mechanism. The polling mechanism is based on the following assumptions:
- The {operationStarterPath} endpoint will accept a POST with aTRequest
object as payload and will return a 202 Accepted status code when the operation is started.- The returned response will contain a LongRunningOperation object with the operation id.
- The polling endpoint is available at {operationStarterPath}/{operationId}/status.
- The polling endpoint will return a 200 status code when the operation is found.
- The returned response will contain a LongRunningOperation object with the current status of the operation, possibly including the result if the operation completed.
- The polling endpoint will return a 404 Not Found status code when the operation is not found.
public Task<TResponse?> ExecuteOperationAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenThe CancellationToken indicating the need to cancel the process.
Returns
- Task<TResponse>
The
TResponse
object containing the response.
Exceptions
GetOperationStatusAsync(string, CancellationToken)
Gets the status of a running operation.
public Task<LongRunningOperation> GetOperationStatusAsync(string operationId, CancellationToken cancellationToken = default)
Parameters
operationId
stringThe identifier of the running operation.
cancellationToken
CancellationTokenThe CancellationToken indicating the need to cancel the process.
Returns
- Task<LongRunningOperation>
A LongRunningOperationobject providing details about the running operation.
StartOperationAsync(CancellationToken)
Starts an operation and returns immediately.
public Task<LongRunningOperation> StartOperationAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenThe CancellationToken indicating the need to cancel the process.
Returns
- Task<LongRunningOperation>
A LongRunningOperationobject providing details about the newly started operation.