com.github.aoudiamoncef.apollo.plugin.util.GzipRequestInterceptor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apollo-client-maven-plugin Show documentation
Show all versions of apollo-client-maven-plugin Show documentation
Maven plugin for generating graphql clients
package com.github.aoudiamoncef.apollo.plugin.util
import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import okio.BufferedSink
import okio.GzipSink
import okio.buffer
import java.io.IOException
internal class GzipRequestInterceptor : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest: Request = chain.request()
if (originalRequest.body == null || originalRequest.header("Content-Encoding") != null) {
return chain.proceed(originalRequest)
}
val compressedRequest = originalRequest.newBuilder()
.header("Content-Encoding", "gzip")
.method(originalRequest.method, gzip(originalRequest.body))
.build()
return chain.proceed(compressedRequest)
}
private fun gzip(body: RequestBody?): RequestBody {
return object : RequestBody() {
override fun contentType(): MediaType? {
return body!!.contentType()
}
override fun contentLength(): Long {
return -1 // We don't know the compressed length in advance!
}
@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
val gzipSink = GzipSink(sink).buffer()
body!!.writeTo(gzipSink)
gzipSink.close()
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy