Skip to content

Commit

Permalink
fix: headers format in response
Browse files Browse the repository at this point in the history
- We have mixed formats in headers (map of string/list of strings, and map of string/string), we convert all in map of string, string.
  • Loading branch information
OlivierCavadenti committed Jan 17, 2023
1 parent 1feab19 commit 39bca78
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ open class Kuzzle {
response.requestId = eventRequestId
response.status = event.status
if (event.headers != null) {
response.headers = event.headers as Map<String?, Any?>
response.headers = event.getHeadersMap() as Map<String?, Any?>?
if (response.headers!!.containsKey("X-Kuzzle-Volatile")) {
response.Volatile = response.headers!!["X-Kuzzle-Volatile"] as Map<String?, Any?>?
}
Expand All @@ -103,11 +103,13 @@ open class Kuzzle {
response.index = event.payload["index"] as String?
response.collection = event.payload["collection"] as String?
} else {
if (! jsonObject.containsKey("headers") && event.headers != null) {
jsonObject = jsonObject.plus("headers" to event.headers)
val jsonObjectEventHeaders = jsonObject.toMutableMap()
if (event.headers != null) {
jsonObjectEventHeaders["headers"] = event.getHeadersMap()
}

response.apply {
fromMap(jsonObject)
fromMap(jsonObjectEventHeaders)
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/kotlin/io/kuzzle/sdk/events/MessageReceivedEvent.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package io.kuzzle.sdk.events

import io.ktor.http.*

data class MessageReceivedEvent(
var message: String?,
var payload: Map<String?, Any?> = emptyMap(),
var status: Int = 200,
var headers: Map<String, List<String>>? = null
)
var headers: Headers? = null
) {
fun getHeadersMap() : Map<String, String> {
if(headers == null) {
return emptyMap()
}
return headers!!.entries().associate { it.key to it.value.first() }
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ open class Http : AbstractProtocol {
response.receive(),
payload,
response.status.value,
response.headers.toMap()
response.headers
)
)
} catch (e: Exception) {
Expand Down Expand Up @@ -266,7 +266,7 @@ open class Http : AbstractProtocol {
response.receive(),
payload,
response.status.value,
response.headers.toMap()
response.headers
)
)
} catch (e: Exception) {
Expand Down

0 comments on commit 39bca78

Please sign in to comment.