Skip to content

Commit d862a15

Browse files
feat: Gzip compression for Graphql request body (#328)
## Summary Turn on Gzip compression for Graphql request body. Gzip is little better for images. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds gzip-compressed request payload support (enabled by default) to `GraphQLClient.execute`, updating headers and content length accordingly. > > - **GraphQL client (`sdk/@launchdarkly/observability-android/.../GraphQLClient.kt`)**: > - `execute(...)`: adds `compress: Boolean = true`; gzips request body when enabled; updates `Content-Length`, `Content-Encoding: gzip`, and `setFixedLengthStreamingMode` to match compressed payload. > - Adds private `gzip` helper using `GZIPOutputStream`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit aa32a89. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 08c4d22 commit d862a15

File tree

1 file changed

+19
-4
lines changed
  • sdk/@launchdarkly/observability-android/lib/src/main/kotlin/com/launchdarkly/observability/network

1 file changed

+19
-4
lines changed

sdk/@launchdarkly/observability-android/lib/src/main/kotlin/com/launchdarkly/observability/network/GraphQLClient.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import kotlinx.serialization.json.Json
77
import kotlinx.serialization.Serializable
88
import kotlinx.serialization.json.JsonElement
99
import java.io.IOException
10+
import java.io.ByteArrayOutputStream
1011
import java.net.HttpURLConnection
1112
import java.net.URL
13+
import java.util.zip.GZIPOutputStream
1214

1315
@Serializable
1416
data class GraphQLRequest(
@@ -71,7 +73,8 @@ class GraphQLClient(
7173
suspend fun <T> execute(
7274
queryFileName: String,
7375
variables: Map<String, JsonElement> = emptyMap(),
74-
dataSerializer: KSerializer<T>
76+
dataSerializer: KSerializer<T>,
77+
compress: Boolean = true
7578
): GraphQLResponse<T> = withContext(DispatcherProviderHolder.current.io) {
7679
var connection: HttpURLConnection? = null
7780
try {
@@ -83,12 +86,16 @@ class GraphQLClient(
8386

8487
val requestJson = json.encodeToString(request)
8588
val requestBytes = requestJson.toByteArray(Charsets.UTF_8)
89+
val payloadBytes = if (compress) gzip(requestBytes) else requestBytes
8690
val connectionLocal = connectionProvider.openConnection(endpoint).also { connection = it }
8791

8892
connectionLocal.apply {
8993
requestMethod = "POST"
90-
setRequestProperty("Content-Length", requestBytes.size.toString())
94+
setRequestProperty("Content-Length", payloadBytes.size.toString())
9195
setRequestProperty("Content-Type", "application/json")
96+
if (compress) {
97+
setRequestProperty("Content-Encoding", "gzip")
98+
}
9299

93100
// Add custom headers
94101
headers.forEach { (key, value) ->
@@ -98,12 +105,12 @@ class GraphQLClient(
98105
doOutput = true
99106
connectTimeout = CONNECT_TIMEOUT
100107
readTimeout = READ_TIMEOUT
101-
setFixedLengthStreamingMode(requestBytes.size)
108+
setFixedLengthStreamingMode(payloadBytes.size)
102109
}
103110

104111
// Send request
105112
connectionLocal.outputStream.use { outputStream ->
106-
outputStream.write(requestBytes)
113+
outputStream.write(payloadBytes)
107114
}
108115

109116
// Read response
@@ -141,4 +148,12 @@ class GraphQLClient(
141148
it.readText()
142149
} ?: throw IllegalStateException("Could not load GraphQL query file: $queryFilepath")
143150
}
151+
152+
private fun gzip(data: ByteArray): ByteArray {
153+
val byteStream = ByteArrayOutputStream()
154+
GZIPOutputStream(byteStream).use { gzipStream ->
155+
gzipStream.write(data)
156+
}
157+
return byteStream.toByteArray()
158+
}
144159
}

0 commit comments

Comments
 (0)