abstractHttpClient
Hierarchy
- HttpClient
Index
Constructors
constructor
Parameters
options: HttpClientOptions = {}
Configuration of the pipeline. The retry and timeout options are validated the same way the
ApifyClientconstructor validates them.
Returns HttpClient
Throws - When a retry or timeout option is out of bounds.
Properties
readonlyhttpCompressor
Compressor the request pipeline runs the bodies worth compressing through.
logger
Logger for the retry warnings.
maxRetries
How many times a failed request is retried at most.
minDelayBetweenRetriesMillis
Lower bound for the delay before the first retry in milliseconds. It doubles with every further retry.
stats
Statistics of the API calls made through this client.
timeoutMaxMillis
Upper bound for the timeout of a single attempt, in milliseconds.
timeoutMillis
Duration of each timeout tier, in milliseconds.
Methods
call
Makes an API request with automatic retries and exponential backoff.
Network errors the transport classifies as retryable, rate limits (HTTP 429) and server errors (HTTP 5xx) are retried up to maxRetries times. Any other error status is thrown as ApifyApiError right away. A request whose body is a
Readableis never retried, since part of the stream has already been consumed by the time the failure shows. Abortingconfig.signalends the attempt in flight, skips the remaining retries and rejects the call with the signal'sreason.Parameters
config: ApifyRequestConfig
The request to make.
Returns Promise<ApifyResponse<T>>
The successful response.
Throws - When the API responds with an error status the retries could not fix.
close
Releases resources owned by the transport, such as a connection pool. The default does nothing.
Returns Promise<void>
isRetryableTransportError
Whether an error thrown by the transport is worth retrying.
The default classifies nothing as retryable, so a transport that does not override it gives up on the first connection failure. Every transport should map its own transient errors here: connection resets, refused connections, timeouts and the like. Error responses are not the transport's concern, the pipeline decides on them from the status code.
Parameters
_error: unknown
Returns boolean
isTimeoutError
Whether an error thrown by the transport is a timeout.
Recognizes errors named
TimeoutError, which is whatAbortSignal.timeout()produces. Transports extend it with the timeout errors their HTTP library throws. The classification is independent of retryability: a timeout the retry loop should retry has to be covered by isRetryableTransportError too.Parameters
error: unknown
Returns boolean
sendRequest
Sends one prepared request through the underlying HTTP library.
The inherited call needs it, so every transport has to implement it. Let the library's errors propagate unwrapped: call classifies them through isRetryableTransportError and isTimeoutError. Return error responses as they are too, the pipeline turns them into ApifyApiError and decides whether to retry.
Parameters
request: HttpRequest
The request to send, with the headers merged, the body serialized and the query encoded.
Returns Promise<HttpResponse>
The response, with the body unread when
request.streamis set and as raw bytes otherwise.
setDefaultAuthorization
Sets the
Authorizationheader from the token, unless an authorization header is already configured.Parameters
token: string
The Apify API token to send as the
Bearertoken.
Returns void
Base class for the HTTP clients used by ApifyClient.
It holds the shared request pipeline: call merges the default headers in, serializes and compresses the body, encodes the query parameters, retries transient failures with exponential backoff, grows the timeout with every attempt, decodes the response body by content type, records statistics and converts error statuses to ApifyApiError. A concrete client only has to implement sendRequest, the transport that moves one prepared request over the wire, and can override the classification and lifecycle hooks isTimeoutError, isRetryableTransportError and close.
Overriding call itself is also supported and bypasses the transport hooks entirely. Such a client has to send the default headers from
defaultHeaderswith every request, otherwise theAuthorizationheader never reaches the API. The protected helpersprepareRequest(),buildUrl()andcomputeTimeoutMillis()stay available to it.