Table of Contents

Class HttpPullWorker

Namespace
Cryville.EEW
Assembly
Cryville.EEW.dll
A source worker that pulls events with HTTP GET requests.
public abstract class HttpPullWorker : IDisposable
Inheritance
HttpPullWorker
Implements
Inherited Members

Constructors

HttpPullWorker(Uri)

Creates an instance of the HttpPullWorker class.
protected HttpPullWorker(Uri uri)

Parameters

uri Uri
The base URI of the source.

Properties

BaseUri

The base URI.
protected Uri BaseUri { get; }

Property Value

Uri

Client

The HTTP client that interacts with the source.
protected HttpClient Client { get; }

Property Value

HttpClient

DefaultPeriod

The default pulling period.
protected virtual TimeSpan DefaultPeriod { get; }

Property Value

TimeSpan

ForceDefaultPeriod

Whether to force the pulling period to be DefaultPeriod.
protected virtual bool ForceDefaultPeriod { get; }

Property Value

bool

Remarks

If set to false, the client determines the period from the max-age directive in the Cache-Control header of the response if it exists, and clamps it to be at least MinimumPeriod. The period falls back to DefaultPeriod if the directive is not found.

MinimumPeriod

The minimum pulling period.
protected virtual TimeSpan MinimumPeriod { get; }

Property Value

TimeSpan

Methods

AfterHandled(CancellationToken)

Called when a response is handled successfully, or when the server responses with a non-error status code (100~399).
protected virtual Task AfterHandled(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Returns

Task

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()

Dispose(bool)

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

GetUri()

Gets the URI of the next request, usually based on BaseUri.
protected virtual Uri GetUri()

Returns

Uri
The URI of the next request.

Remarks

If not overridden, the request URI is always BaseUri.

Handle(Stream, HttpResponseHeaders, CancellationToken)

Handles a response.
protected abstract Task Handle(Stream stream, HttpResponseHeaders headers, CancellationToken cancellationToken)

Parameters

stream Stream
The stream that contains the content of the response.
headers HttpResponseHeaders
The response headers.
cancellationToken CancellationToken
A cancellation token.

Returns

Task
The task.

HandleRawResponse(HttpResponseMessage, CancellationToken)

Handles a raw response message.
protected virtual Task HandleRawResponse(HttpResponseMessage response, CancellationToken cancellationToken)

Parameters

response HttpResponseMessage
The response message.
cancellationToken CancellationToken
A cancellation token.

Returns

Task
A task.

OnError(Exception)

Called when an error occurs.
protected abstract void OnError(Exception ex)

Parameters

ex Exception
The exception.

OnHeartbeat()

Called when a response is received.
protected abstract void OnHeartbeat()

RunAsync(CancellationToken)

Runs the worker.
public Task RunAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task
The task.

Exceptions

InvalidOperationException
The server responses with an unhandled status code.

TryGetAsync(Uri, CancellationToken, HttpRetryStrategies, HttpRetryStrategies, int)

Try to send a GET request to the specified URI.
protected Task<HttpResponseMessage?> TryGetAsync(Uri uri, CancellationToken cancellationToken, HttpRetryStrategies clientErrorRetryStrategies = HttpRetryStrategies.Throw, HttpRetryStrategies serverErrorRetryStrategies = HttpRetryStrategies.Report | HttpRetryStrategies.RetryBeforeExit, int retries = 5)

Parameters

uri Uri
The URI.
cancellationToken CancellationToken
The cancellation token.
clientErrorRetryStrategies HttpRetryStrategies
The retry strategy on client error.
serverErrorRetryStrategies HttpRetryStrategies
The retry strategy on server error.
retries int
Times to retry before the request fails.

Returns

Task<HttpResponseMessage>
The response message, or null if the request fails.

TrySendAsync(Func<HttpRequestMessage>, CancellationToken, HttpRetryStrategies, HttpRetryStrategies, int)

Try to send a request to the specified URI.
protected Task<HttpResponseMessage?> TrySendAsync(Func<HttpRequestMessage> msgFactory, CancellationToken cancellationToken, HttpRetryStrategies clientErrorRetryStrategies = HttpRetryStrategies.Throw, HttpRetryStrategies serverErrorRetryStrategies = HttpRetryStrategies.Report | HttpRetryStrategies.RetryBeforeExit, int retries = 5)

Parameters

msgFactory Func<HttpRequestMessage>
A function that creates the request message.
cancellationToken CancellationToken
The cancellation token.
clientErrorRetryStrategies HttpRetryStrategies
The retry strategy on client error.
serverErrorRetryStrategies HttpRetryStrategies
The retry strategy on server error.
retries int
Times to retry before the request fails.

Returns

Task<HttpResponseMessage>
The response message, or null if the request fails.