Skip to content

Commit

Permalink
Better version of @yassenb's attempt to respect the encoding while ge…
Browse files Browse the repository at this point in the history
…tting the response

Works around the npe that happens in ktor client for empty responses (head request)


closes #62
  • Loading branch information
jillesvangurp committed Mar 13, 2023
1 parent 762a573 commit 74a4912
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jillesvangurp.ktsearch

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.content.*
Expand All @@ -16,12 +17,12 @@ expect fun defaultKtorHttpClient(
* Ktor-client implementation of the RestClient.
*/
class KtorRestClient(
private vararg val nodes: Node= arrayOf(Node("localhost",9200)),
private vararg val nodes: Node = arrayOf(Node("localhost", 9200)),
private val https: Boolean = false,
private val user: String? = null,
private val password: String? = null,
private val logging: Boolean = false,
private val client: HttpClient = defaultKtorHttpClient(logging=logging,user=user, password = password),
private val client: HttpClient = defaultKtorHttpClient(logging = logging, user = user, password = password),
private val nodeSelector: NodeSelector = RoundRobinNodeSelector(nodes),
) : RestClient {
constructor(
Expand All @@ -31,14 +32,14 @@ class KtorRestClient(
user: String? = null,
password: String? = null,
logging: Boolean = false,
client: HttpClient = defaultKtorHttpClient(logging=logging,user=user, password = password),
) : this(
client=client,
https=https,
user=user,
password=password,
logging=logging,
nodes= arrayOf( Node(host, port))
client: HttpClient = defaultKtorHttpClient(logging = logging, user = user, password = password),
) : this(
client = client,
https = https,
user = user,
password = password,
logging = logging,
nodes = arrayOf(Node(host, port))
)

override fun nextNode(): Node = nodeSelector.selectNode()
Expand All @@ -50,7 +51,7 @@ class KtorRestClient(
parameters: Map<String, Any>?,
payload: String?,
contentType: ContentType,
headers:Map<String,Any>?
headers: Map<String, Any>?
): RestResponse {

val response = client.request {
Expand All @@ -68,13 +69,13 @@ class KtorRestClient(
}

}
headers?.let {providedHeaders ->
headers?.let { providedHeaders ->
headers {
providedHeaders.forEach {
val value = it.value
if(value is String) {
if (value is String) {
append(it.key, value)
} else if(value is List<*>) {
} else if (value is List<*>) {
appendAll(it.key, value as List<String>)
}
}
Expand All @@ -85,7 +86,13 @@ class KtorRestClient(
}
}

val responseBody = response.readBytes()

val responseBody = try {
response.body<ByteArray>()
} catch (e: IllegalStateException) {
ByteArray(0)
}

return when (response.status) {
HttpStatusCode.OK -> RestResponse.Status2XX.OK(responseBody)
HttpStatusCode.Created -> RestResponse.Status2XX.Created(responseBody)
Expand All @@ -95,13 +102,16 @@ class KtorRestClient(
responseBody,
response.headers["Location"]
)

HttpStatusCode.TemporaryRedirect -> RestResponse.Status3XX.TemporaryRedirect(
responseBody,
response.headers["Location"]
)

HttpStatusCode.NotModified -> RestResponse.Status3XX.NotModified(
responseBody
)

HttpStatusCode.BadRequest -> RestResponse.Status4XX.BadRequest(responseBody)
HttpStatusCode.Unauthorized -> RestResponse.Status4XX.UnAuthorized(responseBody)
HttpStatusCode.Forbidden -> RestResponse.Status4XX.Forbidden(responseBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ import com.jillesvangurp.jsondsl.JsonDsl
import io.kotest.matchers.shouldBe
import kotlin.test.Test

class IndexTemplateTest: SearchTestBase() {
class IndexTemplateTest : SearchTestBase() {

@Test
fun shouldCreateDataStream() = coRun {


val settingsTemplateId = "my-settings"
val mappingsTemplateId = "my-mappings"
val templateId = "my-template"
val dataStreamName = "logs"

runCatching { client.deleteDataStream("logs") }
runCatching { client.deleteIndexTemplate(templateId) }
runCatching { client.deleteComponentTemplate(mappingsTemplateId) }
runCatching { client.deleteComponentTemplate(settingsTemplateId) }

client.updateComponentTemplate(settingsTemplateId) {
settings {
replicas=4
replicas = 4
}
}
client.updateComponentTemplate(mappingsTemplateId) {
Expand Down

0 comments on commit 74a4912

Please sign in to comment.