All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.com.algolia.client.configuration.internal.GzipPlugin.kt Maven / Gradle / Ivy

Go to download

"Algolia is a powerful search-as-a-service solution, made easy to use with API clients, UI libraries, and pre-built integrations. Algolia API Client for Kotlin lets you easily use the Algolia Search REST API from your JVM project, such as Android or backend implementations."

There is a newer version: 3.10.1
Show newest version
package com.algolia.client.configuration.internal

import io.ktor.client.plugins.api.*
import io.ktor.client.request.*
import io.ktor.content.*
import io.ktor.http.content.ByteArrayContent
import java.io.ByteArrayOutputStream
import java.util.zip.GZIPOutputStream

/**
 * Plugin to gzip encode body (text) requests.
 */
internal val GzipCompression: ClientPlugin = createClientPlugin("GzipCompression") {
  on(Send) { request ->
    val body = request.body
    if (body is TextContent) {
      val encoded = ByteArrayOutputStream().use { bos ->
        GZIPOutputStream(bos).bufferedWriter().use { gzip -> gzip.write(body.text) }
        bos.toByteArray()
      }
      val content = ByteArrayContent(encoded, contentType = body.contentType)
      request.header("Content-Encoding", "gzip")
      request.setBody(content)
    }
    proceed(request)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy