From da2d9eab27108e5c9a1eba56ff8070079cd65621 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Mon, 21 Jun 2021 14:28:54 +0200 Subject: [PATCH] Reuse okhttp3 instead of khttp GitOrigin-RevId: 95644cca4ee8762295769a460439d92746db2fe7 --- .../kotlin.performance-tests.iml | 32 +++++++++------- .../kotlin/idea/perf/util/ESUploader.kt | 38 ++++++++++++------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/plugins/kotlin/performance-tests/kotlin.performance-tests.iml b/plugins/kotlin/performance-tests/kotlin.performance-tests.iml index ac173ad5ec9c..40804fabc811 100644 --- a/plugins/kotlin/performance-tests/kotlin.performance-tests.iml +++ b/plugins/kotlin/performance-tests/kotlin.performance-tests.iml @@ -48,20 +48,24 @@ - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/util/ESUploader.kt b/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/util/ESUploader.kt index ffcbe0c5604b..98cca19478bd 100644 --- a/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/util/ESUploader.kt +++ b/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/util/ESUploader.kt @@ -2,9 +2,9 @@ package org.jetbrains.kotlin.idea.perf.util -import khttp.structures.authorization.BasicAuthorization -import java.io.FileInputStream -import java.util.* +import okhttp3.* +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.RequestBody.Companion.toRequestBody object ESUploader { var host: String? = null @@ -13,6 +13,9 @@ object ESUploader { var indexName = "kotlin_ide_benchmarks" + private val JSON: MediaType = "application/json; charset=utf-8".toMediaType() + private val client = OkHttpClient() + init { host = System.getenv("es.hostname") username = System.getenv("es.username") @@ -28,21 +31,30 @@ object ESUploader { val url = "$host/$indexName/_doc/${benchmark.id()}" val auth = if (username != null && password != null) { - BasicAuthorization(username!!, password!!) + Credentials.basic(username!!, password!!); } else { null } val json = kotlinJsonMapper.writeValueAsString(benchmark) - val response = khttp.put( - url = url, - auth = auth, - headers = mapOf("Content-Type" to "application/json"), - data = json - ) - logMessage { "${response.statusCode} -> ${response.jsonObject}" } - if (response.statusCode != 200 && response.statusCode != 201) { - throw IllegalStateException("Error code ${response.statusCode} -> ${response.text}") + val body: RequestBody = json.toRequestBody(JSON) + val request: Request = Request.Builder() + .url(url) + .post(body) + .header("Content-Type", "application/json") + .also { builder -> + auth?.let { + builder.header("Authorization", it) + } + } + .build() + client.newCall(request).execute().use { response -> + val code = response.code + val string = response.body?.string() + logMessage { "$code -> $string" } + if (code != 200 && code != 201) { + throw IllegalStateException("Error code $code -> $string") + } } } } \ No newline at end of file