Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RawJson in Body and fixes #75

Merged
merged 5 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 26 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,8 @@
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.json.RawJson
import io.kuzzle.sdk.coreClasses.maps.KuzzleMap
import io.kuzzle.sdk.coreClasses.serializer.StringSerializer
import java.net.URLEncoder
Expand Down Expand Up @@ -63,7 +65,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 +79,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 +95,6 @@ class Route {
}
}
}

/**
* Build the query string
*/
Expand All @@ -113,7 +119,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 +144,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 {
OlivierCavadenti marked this conversation as resolved.
Show resolved Hide resolved
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