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

Add missing headers when using HTTP Protocol #78

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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