Skip to content

Commit

Permalink
with HTTP protocol use headers from the HTTP Request when headers are…
Browse files Browse the repository at this point in the history
… not provided in the body
  • Loading branch information
Shiranuit committed Jun 15, 2022
1 parent 8227e58 commit 7e7ec1f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ open class Kuzzle {
return
}

if (! jsonObject.containsKey("headers") && event.headers != null) {
jsonObject = jsonObject.plus("headers" to event.headers)
}

// 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.kuzzle.sdk.events

data class MessageReceivedEvent(var message: String?, var requestId: String?)
data class MessageReceivedEvent(var message: String?, var requestId: String? = null, var headers: Map<String, List<String>>? = null)
32 changes: 16 additions & 16 deletions src/main/kotlin/io/kuzzle/sdk/protocol/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.*
import io.kuzzle.sdk.coreClasses.exceptions.*
import io.kuzzle.sdk.coreClasses.http.Route
import io.kuzzle.sdk.coreClasses.json.JsonSerializer
Expand Down Expand Up @@ -222,7 +223,7 @@ open class Http : AbstractProtocol {
this.body = JsonSerializer.serialize(payload)
}
// trigger messageReceived
super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?))
super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?, response.headers.toMap()))
} catch (e: Exception) {
super.trigger(RequestErrorEvent(e, payload["requestId"] as String?))
} finally {
Expand Down Expand Up @@ -253,7 +254,7 @@ open class Http : AbstractProtocol {
this.body = if (requestInfo.body != null) JsonSerializer.serialize(requestInfo.body) else ""
}
// trigger messageReceived
super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?))
super.trigger(MessageReceivedEvent(response.receive(), payload["requestId"] as String?, response.headers.toMap()))
} catch (e: Exception) {
super.trigger(RequestErrorEvent(e, payload["requestId"] as String?))
} finally {
Expand Down Expand Up @@ -303,20 +304,19 @@ open class Http : AbstractProtocol {

val route = this.routes["$controller:$action"]

if (route == null) {
super.trigger(RequestErrorEvent(URLNotFoundException(controller, action), payload["requestId"] as String?))
return
}

try {
val requestInfo = route.buildRequest(KuzzleMap.from(payload))
queryHTTPEndpoint(payload, requestInfo)
} catch (e: MissingURLParamException) {
/**
* Fallback if we could not find a matching route with the given parameters
* Try to make the request using directly using /_query endpoint
*/
query(payload)
if (route != null) {
try {
val requestInfo = route.buildRequest(KuzzleMap.from(payload))
queryHTTPEndpoint(payload, requestInfo)
return
} catch (e: MissingURLParamException) {
// Fallback to query
}
}
/**
* Fallback if we could not find a matching route with the given parameters
* Try to make the request using directly using /_query endpoint
*/
query(payload)
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/io/kuzzle/sdk/protocol/WebSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ open class WebSocket : AbstractProtocol {
for (frame in incoming) {
when (frame) {
// @TODO Create enums for events
is Frame.Text -> super.trigger(MessageReceivedEvent(frame.readText(), null))
is Frame.Text -> super.trigger(MessageReceivedEvent(frame.readText()))
// @TODO Create enums for events
is Frame.Binary -> super.trigger(MessageReceivedEvent(frame.readBytes().toString(), null))
is Frame.Binary -> super.trigger(MessageReceivedEvent(frame.readBytes().toString()))
is Frame.Close -> TODO()
is Frame.Ping -> TODO()
is Frame.Pong -> TODO()
Expand Down

0 comments on commit 7e7ec1f

Please sign in to comment.