Skip to content

Commit

Permalink
Merge pull request #75 from kuzzleio/fix/responses-fixes
Browse files Browse the repository at this point in the history
Support RawJson in Body and fixes
  • Loading branch information
OlivierCavadenti authored May 13, 2022
2 parents ab23a34 + c402bf8 commit d7f66c4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ open class Kuzzle {

private fun onMessageReceived(event: MessageReceivedEvent) {
val message = event.message
val jsonObject: Map<String?, Any?>
var jsonObject: Map<String?, Any?>
try {
jsonObject = JsonSerializer.deserialize(message) as Map<String?, Any?>
} catch (e: Exception) {
Expand All @@ -77,6 +77,13 @@ open class Kuzzle {
}
return
}

// If the message is empty, we take the requestId of the event,
// to avoid error in fromMap function.
if (! jsonObject.containsKey("requestId") && event.requestId != null) {
jsonObject = jsonObject.plus("requestId" to event.requestId)
}

val response = Response().apply {
fromMap(jsonObject)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RealtimeController(kuzzle: Kuzzle) : BaseController(kuzzle) {
)

init {
kuzzle.protocol.addListener<UnhandledResponseEvent>() {
kuzzle.protocol.addListener<UnhandledResponseEvent> {
val response = Response().apply {
fromMap(JsonSerializer.deserialize(it.message) as Map<String?, Any?>)
}
Expand All @@ -50,7 +50,7 @@ class RealtimeController(kuzzle: Kuzzle) : BaseController(kuzzle) {
}
}

kuzzle.protocol.addListener<NetworkStateChangeEvent>() {
kuzzle.protocol.addListener<NetworkStateChangeEvent> {
if (it.state == ProtocolState.CLOSE) {
currentSubscriptions.clear()
}
Expand Down
30 changes: 25 additions & 5 deletions src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kuzzle.sdk.coreClasses.http

import io.kuzzle.sdk.coreClasses.exceptions.MissingURLParamException
import io.kuzzle.sdk.coreClasses.json.JsonSerializer
import io.kuzzle.sdk.coreClasses.maps.KuzzleMap
import io.kuzzle.sdk.coreClasses.serializer.StringSerializer
import java.net.URLEncoder
Expand Down Expand Up @@ -63,7 +64,6 @@ class Route {
*/
fun buildRequest(request: KuzzleMap): HttpRequest {
val headers = request.optMap("headers", KuzzleMap())

val queryArgs = KuzzleMap()
for (key: String? in request.keys) {
// Skip if a key or value is null
Expand All @@ -78,7 +78,13 @@ class Route {
"headers" -> headers.putAll(request.optMap("headers", KuzzleMap()))
"body" -> {
if (verb == "GET") {
queryArgs.putAll(request.optMap("body", KuzzleMap()))
var body = request["body"]
if (body != null) {
if (body !is Map<*, *>) {
body = JsonSerializer.deserialize(JsonSerializer.serialize(body))
}
queryArgs.putAll(body as Map<String?, Any?>)
}
}
}
else -> {
Expand All @@ -88,7 +94,6 @@ class Route {
}
}
}

/**
* Build the query string
*/
Expand All @@ -113,7 +118,7 @@ class Route {
return HttpRequest(
verb,
if (queryArgs.isEmpty()) staticURL else "$staticURL?$queryString",
if (verb != "GET") request.optMap("body", KuzzleMap()) else null,
if (verb != "GET") getBody(request) else null,
headers
)
}
Expand All @@ -138,11 +143,26 @@ class Route {
return HttpRequest(
verb,
if (queryArgs.isEmpty()) urlBuilder.toString() else "$urlBuilder?$queryString",
if (verb != "GET") request.optMap("body", KuzzleMap()) else null,
if (verb != "GET") getBody(request) else null,
headers
)
}

private fun getBody(request: KuzzleMap): KuzzleMap {
return when (val body = request["body"]) {
null -> {
KuzzleMap()
}
// If it's not a map, it can be a RawJson or Serializable object.
!is Map<*, *> -> {
KuzzleMap.from(JsonSerializer.deserialize(JsonSerializer.serialize(body)) as Map<String?, Any?>)
}
else -> {
KuzzleMap.from(body as Map<String?, Any?>)
}
}
}

companion object {
/**
* Parse Kuzzle url with the format /:index/:collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package io.kuzzle.sdk.coreClasses.json
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import io.kuzzle.sdk.coreClasses.RequestPayload
import io.kuzzle.sdk.coreClasses.maps.KuzzleMap

object JsonSerializer {
private var gson: Gson? = null
fun deserialize(rawJson: String?): Map<*, *> {
return gson!!.fromJson(rawJson, Map::class.java)
return if (rawJson == null || rawJson.isBlank()) {
KuzzleMap()
} else {
gson!!.fromJson(rawJson, Map::class.java)
}
}

fun serialize(obj: Any): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class KuzzleMap : HashMap<String?, Any?> {
/**
* Create a new instance of CustomMap
*/
constructor() : super() {}
constructor() : super()

/**
* Create a new instance of CustomMap from a Map<String></String>,
Expand Down

0 comments on commit d7f66c4

Please sign in to comment.