Repository: square/okhttp
File: okhttp/src/commonJvmAndroid/kotlin/okhttp3/CompressionInterceptor.kt (Kotlin)

Code before:
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package okhttp3

import okhttp3.ResponseBody.Companion.asResponseBody
import okhttp3.internal.http.promisesBody
import okio.BufferedSource
import okio.GzipSource
import okio.Source
import okio.buffer

/**
 * Transparent Compressed response support.
 *
 * The algorithm map will be turned into a heading such as "Accept-Encoding: br;q=1.0, gzip;q=0.8, *;q=0.1"
 *
 * If [algorithms] is empty returns "identity" disabling any later compression.
 *
 * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Accept-Encoding
 */
open class CompressionInterceptor(
  val algorithms: Map<DecompressionAlgorithm, Double?>,
) : Interceptor {
  constructor(
    vararg algorithms: DecompressionAlgorithm,
  ) : this(
    algorithms.associateWithTo(LinkedHashMap()) {
      null
    },
  )

  internal val acceptEncoding =
    if (algorithms.isEmpty()) {
      "identity"
    } else {
      algorithms
        .map { (algorithm, priority) ->
          if (priority != null) {
            "${algorithm.encoding};q=$priority"
          } else {
            algorithm.encoding
          }
        }.joinToString(separator = ", ")
    }

  private val algorithmIndex = algorithms.keys.toTypedArray()


Review comment (suggestion):
An alternative to this Map, would be for DecompressionAlgorithm to have a priority, defaulting to null. But also a `withPriority()` method to clone and assign. 

Code after:
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package okhttp3

import okhttp3.ResponseBody.Companion.asResponseBody
import okhttp3.internal.http.promisesBody
import okio.BufferedSource
import okio.GzipSource
import okio.Source
import okio.buffer

/**
 * Transparent Compressed response support.
 *
 * The algorithm map will be turned into a heading such as "Accept-Encoding: br, gzip"
 *
 * If [algorithms] is empty this interceptor has no effect. To disable compression set
 * a specific "Accept-Encoding: identity" or similar.
 *
 * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Accept-Encoding
 */
open class CompressionInterceptor(
  vararg val algorithms: DecompressionAlgorithm,
) : Interceptor {
  internal val acceptEncoding =
    algorithms
      .map {
        it.encoding
      }.joinToString(separator = ", ")

  override fun intercept(chain: Interceptor.Chain): Response =
    if (algorithms.isNotEmpty() && chain.request().header("Accept-Encoding") == null) {
      val request =
        chain
          .request()
          .newBuilder()
          .header("Accept-Encoding", acceptEncoding)
          .build()

      val response = chain.proceed(request)

      decompress(response)
    } else {
      chain.proceed(chain.request())
    }

  /**