NetworkClient

public protocol NetworkClient

Protocol for sending network requests and handling responses.

  • Sends a URLRequest as is and completes with a NetworkResult. It returns a Disposable that can be disposed to cancel the request sent.

    Request lifecycle events are sent to the request interceptors and they are retried, when necessary, following the RequestInterceptor logic.

    Declaration

    Swift

    func sendRequest(_ request: URLRequest, completion: @escaping (NetworkResult) -> Void) -> any Disposable

    Parameters

    request

    the URLRequest that is sent in the URLSession.dataTask

    completion

    the block that is called once the request is completed: with a success, with a failure that the interceptors don’t retry (including a retryable one once they stop retrying it, for example when maxRetryCount is reached), or with a .cancelled error if the returned Disposable is disposed first

    Return Value

    the Disposable that can be used to dispose the request and cancel the dataTask and future retries.

  • Builds the given RequestBuilder and sends the resulting URLRequest, completing with a NetworkResult.

    Building happens in the client, so a malformed URL or any other build failure completes with .failure(.unknown) and returns an already-disposed Disposable — the completion is always called. This is the preferred entry point for custom (e.g. gzipped) requests: build with RequestBuilder and hand the builder here.

    Declaration

    Swift

    func sendRequest(_ request: RequestBuilder, completion: @escaping (NetworkResult) -> Void) -> any Disposable

    Parameters

    request

    the RequestBuilder describing the request to build and send.

    completion

    the block called once the request is completed, or immediately with a failure if it can’t be built.

    Return Value

    the Disposable that can be used to cancel the request.

  • Creates a new NetworkClient from this instance which will use a specific logger.

    Declaration

    Swift

    func newClient(withLogger logger: LoggerProtocol) -> Self

    Parameters

    logger

    The logger that will be used for this instance

    Return Value

    A new NetworkClient instance.

  • Creates a new NetworkClient from this instance which will also use the given interceptors.

    The new client uses the same transport (URLSession) as this instance. Its interceptor list is this instance’s interceptors followed by the given ones:

    This instance is unaffected: its requests keep going only through its own interceptors.

    Declaration

    Swift

    func newClient(withAdditionalInterceptors interceptors: [RequestInterceptor], maxRetryCount: Int?) -> Self

    Parameters

    interceptors

    The interceptors to add after this instance’s interceptors.

    maxRetryCount

    the maximum number of retries of a request, whichever interceptor proposes them. nil means no limit. Not inherited from this instance.

    Return Value

    A new NetworkClient instance.

  • makeNetworkHelper() Extension method

    Creates a NetworkHelperProtocol that sends every request through this client.

    Typically used with a client derived with newClient(withAdditionalInterceptors:), to use the helper API with custom retry behavior.

    Declaration

    Swift

    func makeNetworkHelper() -> NetworkHelperProtocol

    Return Value

    a new NetworkHelperProtocol that sends its requests through this client.

  • Same as newClient(withAdditionalInterceptors:maxRetryCount:), with no limit on the number of retries.

    Declaration

    Swift

    func newClient(withAdditionalInterceptors interceptors: [RequestInterceptor]) -> Self