From 35c45b65f01a459a65ef0331063cfbd13754eba0 Mon Sep 17 00:00:00 2001 From: Colin White Date: Wed, 6 Nov 2024 13:56:40 -0800 Subject: [PATCH] Move setting headers recipe to network.md. --- coil-network-core/README.md | 45 ++++++++++++++++++++++++ docs/recipes.md | 69 ------------------------------------- 2 files changed, 45 insertions(+), 69 deletions(-) diff --git a/coil-network-core/README.md b/coil-network-core/README.md index 69bdba2c58..7a9fb193e4 100644 --- a/coil-network-core/README.md +++ b/coil-network-core/README.md @@ -73,3 +73,48 @@ OkHttpNetworkFetcherFactory( !!! Note You need to enable `coreLibraryDesugaring` to support Android API level 25 or below. Follow the docs [here](https://developer.android.com/studio/write/java8-support#library-desugaring) to enable it. + +#### Headers + +Headers can be added to your image requests in one of two ways. You can set headers for a single request: + +```kotlin +val headers = NetworkHeaders.Builder() + .set("Cache-Control", "no-cache") + .build() +val request = ImageRequest.Builder(context) + .data("https://example.com/image.jpg") + .httpHeaders(headers) + .target(imageView) + .build() +imageLoader.execute(request) +``` + +Or you can create an OkHttp [`Interceptor`](https://square.github.io/okhttp/interceptors/) that sets headers for every request executed by your `ImageLoader`: + +```kotlin +class RequestHeaderInterceptor( + private val name: String, + private val value: String, +) : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val headers = NetworkHeaders.Builder() + .set("Cache-Control", "no-cache") + .build() + val request = chain.request().newBuilder() + .httpHeaders(headers) + .build() + return chain.proceed(request) + } +} + +val imageLoader = ImageLoader.Builder(context) + .okHttpClient { + OkHttpClient.Builder() + // This header will be added to every image request. + .addNetworkInterceptor(RequestHeaderInterceptor("Cache-Control", "no-cache")) + .build() + } + .build() +``` diff --git a/docs/recipes.md b/docs/recipes.md index 7bb21fe80f..c623dc16ab 100644 --- a/docs/recipes.md +++ b/docs/recipes.md @@ -25,75 +25,6 @@ imageView.load("https://example.com/image.jpg") { } ``` -## Using a custom OkHttpClient - -If you use `io.coil-kt.coil3:coil-network-okhttp` You can specify a custom `OkHttpClient` when creating your `ImageLoader`: - -```kotlin -val imageLoader = ImageLoader.Builder(context) - // Create the OkHttpClient inside a lambda so it will be initialized lazily on a background thread. - .components { - add( - OkHttpNetworkFetcherFactory( - callFactory = { - OkHttpClient.Builder() - .addInterceptor(CustomInterceptor()) - .build() - } - ) - ) - } - .build() -``` - -!!! Note - If you already have a built `OkHttpClient`, use [`newBuilder()`](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/#customize-your-client-with-newbuilder) to build a new client that shares resources with the original. - -#### Headers - -Headers can be added to your image requests in one of two ways. You can set headers for a single request: - -```kotlin -val headers = NetworkHeaders.Builder() - .set("Cache-Control", "no-cache") - .build() -val request = ImageRequest.Builder(context) - .data("https://example.com/image.jpg") - .httpHeaders(headers) - .target(imageView) - .build() -imageLoader.execute(request) -``` - -Or you can create an OkHttp [`Interceptor`](https://square.github.io/okhttp/interceptors/) that sets headers for every request executed by your `ImageLoader`: - -```kotlin -class RequestHeaderInterceptor( - private val name: String, - private val value: String, -) : Interceptor { - - override fun intercept(chain: Interceptor.Chain): Response { - val headers = NetworkHeaders.Builder() - .set("Cache-Control", "no-cache") - .build() - val request = chain.request().newBuilder() - .httpHeaders(headers) - .build() - return chain.proceed(request) - } -} - -val imageLoader = ImageLoader.Builder(context) - .okHttpClient { - OkHttpClient.Builder() - // This header will be added to every image request. - .addNetworkInterceptor(RequestHeaderInterceptor("Cache-Control", "no-cache")) - .build() - } - .build() -``` - ## Using a Memory Cache Key as a Placeholder Using a previous request's `MemoryCache.Key` as a placeholder for a subsequent request can be useful if the two images are the same, though loaded at different sizes. For instance, if the first request loads the image at 100x100 and the second request loads the image at 500x500, we can use the first image as a synchronous placeholder for the second request.