Back to Alamofire

RequestInterceptor

docs/Protocols/RequestInterceptor.html

5.12.012.3 KB
Original Source

RequestInterceptor

public protocol RequestInterceptor : RequestAdapter, RequestRetrier

Type that provides both RequestAdapter and RequestRetrier functionality.

`

                adapt(_:for:completion:)
                ` Extension method 

Declaration

Swift

@preconcurrency
public func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping @Sendable (Result<URLRequest, any Error>) -> Void)

`

                retry(_:for:dueTo:completion:)
                ` Extension method 

Declaration

Swift

@preconcurrency
public func retry(_ request: Request,
                  for session: Session,
                  dueTo error: any Error,
                  completion: @escaping @Sendable (RetryResult) -> Void)

Available where Self == OfflineRetrier

`

                offlineRetrier(monitor:maximumWait:isOfflineError:)
                ` Extension method 

Creates an instance from the provided NWPathMonitor, maximum wait for connectivity, and offline error predicate.

Declaration

Swift

public static func offlineRetrier(
    monitor: @autoclosure @escaping () -> NWPathMonitor = NWPathMonitor(),
    maximumWait: DispatchTimeInterval = OfflineRetrier.defaultWait,
    isOfflineError: @escaping @Sendable (_ error: any Error) -> Bool = OfflineRetrier.defaultIsOfflineError
) -> OfflineRetrier

Parameters

| monitor |

NWPathMonitor() to use to detect connectivity. A new instance is created each time a request fails and retry may be needed.

| | maximumWait |

DispatchTimeInterval to wait for connectivity before timeout.

| | isOfflineError |

Predicate closure used to determine whether a paricular any Error indicates connectivity is offline. Returning false moves to the next retrier, if any.

|

`

                offlineRetrier(requiredInterfaceType:maximumWait:isOfflineError:)
                ` Extension method 

Creates an instance using an NWPathMonitor configured with the provided InterfaceType, maximum wait for connectivity, and offline error predicate.

Declaration

Swift

public static func offlineRetrier(
    requiredInterfaceType: NWInterface.InterfaceType,
    maximumWait: DispatchTimeInterval = OfflineRetrier.defaultWait,
    isOfflineError: @escaping @Sendable (_ error: any Error) -> Bool = OfflineRetrier.defaultIsOfflineError
) -> OfflineRetrier

Parameters

| monitor |

NWInterface.InterfaceType used to configured the NWPathMonitor each time one is needed.

| | maximumWait |

DispatchTimeInterval to wait for connectivity before timeout.

| | isOfflineError |

Predicate closure used to determine whether a paricular any Error indicates connectivity is offline. Returning false moves to the next retrier, if any.

|

`

                offlineRetrier(prohibitedInterfaceTypes:maximumWait:isOfflineError:)
                ` Extension method 

Creates an instance using an NWPathMonitor configured with the provided InterfaceTypes, maximum wait for connectivity, and offline error predicate.

Declaration

Swift

@available(macOS 11, iOS 14, tvOS 14, watchOS 7, visionOS 1, *)
public static func offlineRetrier(
    prohibitedInterfaceTypes: [NWInterface.InterfaceType],
    maximumWait: DispatchTimeInterval = OfflineRetrier.defaultWait,
    isOfflineError: @escaping @Sendable (_ error: any Error) -> Bool = OfflineRetrier.defaultIsOfflineError
) -> OfflineRetrier

Parameters

| monitor |

[NWInterface.InterfaceType] used to configured the NWPathMonitor each time one is needed.

| | maximumWait |

DispatchTimeInterval to wait for connectivity before timeout.

| | isOfflineError |

Predicate closure used to determine whether a paricular any Error indicates connectivity is offline. Returning false moves to the next retrier, if any.

|

Available where Self == DeflateRequestCompressor

`

                deflateCompressor
                ` Extension method 

Create a DeflateRequestCompressor with default duplicateHeaderBehavior and shouldCompressBodyData values.

Declaration

Swift

public static var deflateCompressor: DeflateRequestCompressor { get }

`

                deflateCompressor(duplicateHeaderBehavior:shouldCompressBodyData:)
                ` Extension method 

Creates a DeflateRequestCompressor with the provided DuplicateHeaderBehavior and shouldCompressBodyData closure.

Declaration

Swift

public static func deflateCompressor(
    duplicateHeaderBehavior: DeflateRequestCompressor.DuplicateHeaderBehavior = .error,
    shouldCompressBodyData: @escaping @Sendable (_ bodyData: Data) -> Bool = { _ in true }
) -> DeflateRequestCompressor

Parameters

| duplicateHeaderBehavior |

DuplicateHeaderBehavior to use.

| | shouldCompressBodyData |

Closure which determines whether the outgoing body data should be compressed. true by default.

|

Return Value

The DeflateRequestCompressor.

Available where Self == Interceptor

`

                interceptor(adapter:retrier:)
                ` Extension method 

Creates an Interceptor using the provided AdaptHandler and RetryHandler closures.

Declaration

Swift

@preconcurrency
public static func interceptor(adapter: @escaping AdaptHandler, retrier: @escaping RetryHandler) -> Interceptor

Parameters

| adapter |

AdapterHandlerto use to adapt the request.

| | retrier |

RetryHandler to use to retry the request.

|

Return Value

The Interceptor.

`

                interceptor(adapter:retrier:)
                ` Extension method 

Creates an Interceptor using the provided RequestAdapter and RequestRetrier instances.

Declaration

Swift

@preconcurrency
public static func interceptor(adapter: any RequestAdapter, retrier: any RequestRetrier) -> Interceptor

Parameters

| adapter |

RequestAdapter to use to adapt the request

| | retrier |

RequestRetrier to use to retry the request.

|

Return Value

The Interceptor.

`

                interceptor(adapters:retriers:interceptors:)
                ` Extension method 

Creates an Interceptor using the provided RequestAdapters, RequestRetriers, and RequestInterceptors.

Declaration

Swift

@preconcurrency
public static func interceptor(adapters: [any RequestAdapter] = [],
                               retriers: [any RequestRetrier] = [],
                               interceptors: [any RequestInterceptor] = []) -> Interceptor

Parameters

| adapters |

RequestAdapters to use to adapt the request. These adapters will be run until one fails.

| | retriers |

RequestRetriers to use to retry the request. These retriers will be run one at a time until a retry is triggered.

| | interceptors |

RequestInterceptors to use to intercept the request.

|

Return Value

The Interceptor.

Available where Self == RetryPolicy

`

                retryPolicy
                ` Extension method 

Provides a default RetryPolicy instance.

Declaration

Swift

public static var retryPolicy: RetryPolicy { get }

`

                retryPolicy(retryLimit:exponentialBackoffBase:exponentialBackoffScale:retryableHTTPMethods:retryableHTTPStatusCodes:retryableURLErrorCodes:)
                ` Extension method 

Creates an RetryPolicy from the specified parameters.

Declaration

Swift

public static func retryPolicy(retryLimit: UInt = RetryPolicy.defaultRetryLimit,
                               exponentialBackoffBase: UInt = RetryPolicy.defaultExponentialBackoffBase,
                               exponentialBackoffScale: Double = RetryPolicy.defaultExponentialBackoffScale,
                               retryableHTTPMethods: Set<HTTPMethod> = RetryPolicy.defaultRetryableHTTPMethods,
                               retryableHTTPStatusCodes: Set<Int> = RetryPolicy.defaultRetryableHTTPStatusCodes,
                               retryableURLErrorCodes: Set<URLError.Code> = RetryPolicy.defaultRetryableURLErrorCodes) -> RetryPolicy

Parameters

| retryLimit |

The total number of times the request is allowed to be retried. 2 by default.

| | exponentialBackoffBase |

The base of the exponential backoff policy. 2 by default.

| | exponentialBackoffScale |

The scale of the exponential backoff. 0.5 by default.

| | retryableHTTPMethods |

The HTTP methods that are allowed to be retried. RetryPolicy.defaultRetryableHTTPMethods by default.

| | retryableHTTPStatusCodes |

The HTTP status codes that are automatically retried by the policy. RetryPolicy.defaultRetryableHTTPStatusCodes by default.

| | retryableURLErrorCodes |

The URL error codes that are automatically retried by the policy. RetryPolicy.defaultRetryableURLErrorCodes by default.

|

Return Value

The RetryPolicy

Available where Self == ConnectionLostRetryPolicy

`

                connectionLostRetryPolicy
                ` Extension method 

Provides a default ConnectionLostRetryPolicy instance.

Declaration

Swift

public static var connectionLostRetryPolicy: ConnectionLostRetryPolicy { get }

`

                connectionLostRetryPolicy(retryLimit:exponentialBackoffBase:exponentialBackoffScale:retryableHTTPMethods:)
                ` Extension method 

Creates a ConnectionLostRetryPolicy instance from the specified parameters.

Declaration

Swift

public static func connectionLostRetryPolicy(retryLimit: UInt = RetryPolicy.defaultRetryLimit,
                                             exponentialBackoffBase: UInt = RetryPolicy.defaultExponentialBackoffBase,
                                             exponentialBackoffScale: Double = RetryPolicy.defaultExponentialBackoffScale,
                                             retryableHTTPMethods: Set<HTTPMethod> = RetryPolicy.defaultRetryableHTTPMethods) -> ConnectionLostRetryPolicy

Parameters

| retryLimit |

The total number of times the request is allowed to be retried. RetryPolicy.defaultRetryLimit by default.

| | exponentialBackoffBase |

The base of the exponential backoff policy. RetryPolicy.defaultExponentialBackoffBase by default.

| | exponentialBackoffScale |

The scale of the exponential backoff. RetryPolicy.defaultExponentialBackoffScale by default.

| | retryableHTTPMethods |

The idempotent http methods to retry.

|

Return Value

The ConnectionLostRetryPolicy.