NetworkClient
public protocol NetworkClient
Protocol for sending network requests and handling responses.
-
Sends a
URLRequestas is and completes with aNetworkResult. It returns aDisposablethat 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
RequestInterceptorlogic.Declaration
Swift
func sendRequest(_ request: URLRequest, completion: @escaping (NetworkResult) -> Void) -> any DisposableParameters
requestthe
URLRequestthat is sent in theURLSession.dataTaskcompletionthe 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
maxRetryCountis reached), or with a.cancellederror if the returnedDisposableis disposed firstReturn Value
the
Disposablethat can be used to dispose the request and cancel the dataTask and future retries. -
Builds the given
RequestBuilderand sends the resultingURLRequest, completing with aNetworkResult.Building happens in the client, so a malformed URL or any other build failure completes with
.failure(.unknown)and returns an already-disposedDisposable— the completion is always called. This is the preferred entry point for custom (e.g. gzipped) requests: build withRequestBuilderand hand the builder here.Declaration
Swift
func sendRequest(_ request: RequestBuilder, completion: @escaping (NetworkResult) -> Void) -> any DisposableParameters
requestthe
RequestBuilderdescribing the request to build and send.completionthe block called once the request is completed, or immediately with a failure if it can’t be built.
Return Value
the
Disposablethat can be used to cancel the request. -
Creates a new
NetworkClientfrom this instance which will use a specific logger.Declaration
Swift
func newClient(withLogger logger: LoggerProtocol) -> SelfParameters
loggerThe logger that will be used for this instance
Return Value
A new
NetworkClientinstance. -
Creates a new
NetworkClientfrom 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:- The given interceptors come after this instance’s, so they are consulted first for retry decisions
(see
RequestInterceptor.shouldRetry(_:consecutiveRetryCount:with:)). onAttemptResultis called on all the interceptors in list order.
This instance is unaffected: its requests keep going only through its own interceptors.
Declaration
Swift
func newClient(withAdditionalInterceptors interceptors: [RequestInterceptor], maxRetryCount: Int?) -> SelfParameters
interceptorsThe interceptors to add after this instance’s interceptors.
maxRetryCountthe maximum number of retries of a request, whichever interceptor proposes them.
nilmeans no limit. Not inherited from this instance.Return Value
A new
NetworkClientinstance. - The given interceptors come after this instance’s, so they are consulted first for retry decisions
(see
-
makeNetworkHelper()Extension methodCreates a
NetworkHelperProtocolthat 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() -> NetworkHelperProtocolReturn Value
a new
NetworkHelperProtocolthat sends its requests through this client. -
newClient(withAdditionalInterceptors:Extension method) Same as
newClient(withAdditionalInterceptors:maxRetryCount:), with no limit on the number of retries.Declaration
Swift
func newClient(withAdditionalInterceptors interceptors: [RequestInterceptor]) -> Self
View on GitHub