Skip to content

Commit

Permalink
feat(client): configure the entire server url rather than just host a…
Browse files Browse the repository at this point in the history
…nd port
  • Loading branch information
d1snin committed Apr 6, 2023
1 parent 23b52aa commit 0ecb146
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ val webSocketClient = HttpClient(CIO) {
}

install(WebSocketEvents) {
host = "localhost"
port = TEST_SERVER_PORT
url = "ws://localhost:$TEST_SERVER_PORT"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import io.ktor.http.*

/**
* Opens a [block] with [DefaultClientWebSocketSession] associated with the given [event reference][reference] and optional [path].
Expand Down Expand Up @@ -53,10 +54,12 @@ public suspend fun HttpClient.webSocketEvents(
parameter(Routes.PRINCIPAL_QUERY_PARAMETER, reference.principal)
}

val url = URLBuilder(webSocketEventsConfiguration.requiredBaseUrl).apply {
path(path)
}.buildString()

webSocket(
host = webSocketEventsConfiguration.requiredHost,
port = webSocketEventsConfiguration.requiredPort,
path = path,
urlString = url,
request = requestConfiguration
) {
block()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,15 @@ public val WebSocketEvents: ClientPlugin<WebSocketEventsConfiguration> =

public class WebSocketEventsConfiguration {

public var host: String? = null
public var url: String? = null

public var port: Int? = null

internal val requiredHost
get() = requireNotNull(host) {
"Host is not configured."
}

internal val requiredPort
get() = requireNotNull(port) {
"Port is not configured."
internal val requiredBaseUrl
get() = requireNotNull(url) {
"URL is not configured."
}

internal fun validate() {
requiredHost
requiredPort
requiredBaseUrl
}
}

Expand Down

0 comments on commit 0ecb146

Please sign in to comment.