Alamofire / AlamofireImage

AlamofireImage is an image component library for Alamofire
MIT License
3.99k stars 523 forks source link

Customize interceptor on ImageDownloader's Session #474

Closed tgaul closed 8 months ago

tgaul commented 9 months ago

Feature Request

When constructing a custom ImageDownloader, allow the client to pass in an optional RequestInterceptor that can be used to customize the headers of requests and retry behavior.

Background

While updating our project from Alamofire 4 to Alamofire 5 and the corresponding version of AlamofireImage, one bit of customization we had added to the ImageDownloader no longer seems to be possible (unless I'm missing something).

Specifically, we had the following code in our app initialization:

ImageDownloader.default.session.retrier = ...

This was done so that if the response indicated a 503 server error, we would do an exponential backoff and retry the image request.

API Limitation

In Alamofire 5, this capability moved to the interceptor object held by the session, which is immutable after creating the session. However, when constructing a new ImageDownloader, it is not possible to pass in an interceptor for it to use when initializing its session.

Nor can one pass an interceptor to the download API on ImageDownloader for it to attach to a specific request.

Other Uses

This would also be useful for adding headers such as authentication via the request adapter API that the interceptor provides.

jshier commented 8 months ago

When using a custom ImageDownloader, you can use the init(session:downloadPrioritization:maximumActiveDownloads:imageCache:) method to create a downloader with a completely custom Session, which allows you to customize the base interceptor that is used for all requests.