jvmMain.com.algolia.client.configuration.internal.GzipPlugin.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of algoliasearch-client-kotlin-jvm Show documentation
Show all versions of algoliasearch-client-kotlin-jvm Show documentation
"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."
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