From b945983835f8c76fdbf7cd4ebf218cdef01715f8 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Fri, 14 Aug 2020 16:05:32 +0200 Subject: [PATCH] improve httpPostStr and add test --- .../ley/anvil/modpacktools/util/Util.kt | 28 ++++++++----------- .../ley/anvil/modpacktools/util/UtilTest.kt | 5 ++++ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt b/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt index e40dd5e..e28b938 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt @@ -9,10 +9,10 @@ import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaType import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Response import org.apache.commons.io.FileUtils import java.io.File import java.io.FileInputStream -import java.io.FileReader import java.io.IOException import java.net.URI import java.net.URL @@ -38,10 +38,7 @@ import kotlin.reflect.jvm.isAccessible fun File.readAsJson(): JsonObject { require(this.exists()) {"File to read doesn't exist"} - val reader = FileReader(this) - val out = JsonParser.parseReader(reader) as JsonObject - reader.close() - return out + return JsonParser.parseReader(reader()) as JsonObject } /** @@ -54,17 +51,14 @@ fun File.readAsJson(): JsonObject { * @return the response as string */ @Throws(IOException::class) -fun URL.httpPostStr(payload: String, contentType: MediaType? = null, additionalHeaders: Map): String? { - val builder = Request.Builder() - .url(this) - .post(payload.toRequestBody(contentType)) - - additionalHeaders.forEach {builder.addHeader(it.key, it.value)} - val resp = HTTP_CLIENT.newCall(builder.build()).execute() - val ret = resp.body?.string() - resp.close() - return ret -} +fun URL.httpPostStr(payload: String, contentType: MediaType? = null, additionalHeaders: Map? = null): Response = + HTTP_CLIENT.newCall( + Request.Builder() + .url(this) + .post(payload.toRequestBody(contentType)) + .apply {additionalHeaders?.forEach {addHeader(it.key, it.value)}} + .build() + ).execute() /** * sends a http post request @@ -76,7 +70,7 @@ fun URL.httpPostStr(payload: String, contentType: MediaType? = null, additionalH * @return the response as string */ @Throws(IOException::class) -fun URL.httpPostStr(payload: String, contentType: String, additionalHeaders: Map): String? { +fun URL.httpPostStr(payload: String, contentType: String, additionalHeaders: Map? = null): Response { return this.httpPostStr( payload, contentType.toMediaType(), diff --git a/src/test/kotlin/ley/anvil/modpacktools/util/UtilTest.kt b/src/test/kotlin/ley/anvil/modpacktools/util/UtilTest.kt index bd7adf6..3eecce4 100644 --- a/src/test/kotlin/ley/anvil/modpacktools/util/UtilTest.kt +++ b/src/test/kotlin/ley/anvil/modpacktools/util/UtilTest.kt @@ -28,4 +28,9 @@ class UtilTest { assertEquals("someValue", testFile.readAsJson()["someKey"].asString) } + + @Test + fun httpPostString() { + assertEquals(200, URL("https://postman-echo.com/post").httpPostStr("Testing").code) + } }