From c0c6052fb256756e522c83c8673d16397bb069ed Mon Sep 17 00:00:00 2001 From: DadiBit <38182988+DadiBit@users.noreply.github.com> Date: Tue, 6 Jun 2023 18:06:04 +0200 Subject: [PATCH] refactor: merged `Http` interface into `Endpoint` --- .../com/openmeteo/api/common/http/Endpoint.kt | 16 +++++- .../com/openmeteo/api/common/http/Http.kt | 28 ---------- .../com/openmeteo/api/common/http/HttpTest.kt | 52 ------------------- 3 files changed, 14 insertions(+), 82 deletions(-) delete mode 100644 lib/src/main/kotlin/com/openmeteo/api/common/http/Http.kt delete mode 100644 lib/src/test/kotlin/com/openmeteo/api/common/http/HttpTest.kt diff --git a/lib/src/main/kotlin/com/openmeteo/api/common/http/Endpoint.kt b/lib/src/main/kotlin/com/openmeteo/api/common/http/Endpoint.kt index 5c9ef4a..a92227d 100644 --- a/lib/src/main/kotlin/com/openmeteo/api/common/http/Endpoint.kt +++ b/lib/src/main/kotlin/com/openmeteo/api/common/http/Endpoint.kt @@ -19,7 +19,7 @@ import javax.net.ssl.HttpsURLConnection open class Endpoint( @Transient val context: URL, -) : Http { +) { companion object { @OptIn(ExperimentalSerializationApi::class) @@ -48,13 +48,19 @@ open class Endpoint( inline fun protoBuf(inputStream: InputStream) = ProtoBuf.decodeFromByteArray(inputStream.readAllBytes()) + /** + * Open a new HTTPS connection + */ + private fun connect(url: URL) = + url.openConnection() as HttpsURLConnection + /** * Handle responses based on their status code: * - 200: return the inputStream * - 400: parse JSON error as [BadRequest] * - else: return a generic error with the url and the response code/message */ - override fun response(connection: HttpsURLConnection): InputStream = + private fun response(connection: HttpsURLConnection): InputStream = with(connection) { when (responseCode) { 200 -> inputStream @@ -63,6 +69,12 @@ open class Endpoint( } } + /** + * GET some data from the url + */ + fun get(url: URL) = + response(connect(url)) + /** * GET, with a [Query], the endpoint context url and parse the response data * (from the specified query format or, if undefined, from json) diff --git a/lib/src/main/kotlin/com/openmeteo/api/common/http/Http.kt b/lib/src/main/kotlin/com/openmeteo/api/common/http/Http.kt deleted file mode 100644 index 2077921..0000000 --- a/lib/src/main/kotlin/com/openmeteo/api/common/http/Http.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.openmeteo.api.common.http - -import java.net.URL -import javax.net.ssl.HttpsURLConnection - -/** - * A simple HTTP/S client - */ -fun interface Http { - - /** - * A simple response callback - */ - fun response(connection: HttpsURLConnection): T - - /** - * Open a new HTTPS connection - */ - private fun connect(url: URL) = - url.openConnection() as HttpsURLConnection - - /** - * GET some data from the url - */ - fun get(url: URL) = - response(connect(url)) - -} diff --git a/lib/src/test/kotlin/com/openmeteo/api/common/http/HttpTest.kt b/lib/src/test/kotlin/com/openmeteo/api/common/http/HttpTest.kt deleted file mode 100644 index 987057d..0000000 --- a/lib/src/test/kotlin/com/openmeteo/api/common/http/HttpTest.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.openmeteo.api.common.http - -import com.openmeteo.api.common.Response -import kotlinx.serialization.Serializable -import kotlinx.serialization.json.Json -import java.net.URL -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNotNull - -class HttpTest { - - companion object { - /** - * An HTTP client that returns the body string if the response code is 200, - * else return null - */ - val client = Http { connection -> - if (connection.responseCode == 200) - connection.inputStream - .use { it.bufferedReader().readText() } - else null - } - } - - /** - * The response body of "https://httpbin.org/get" - */ - @Serializable - class R( - val args: Map, - val headers: Map, - val url: String, - ) : Response - - /** - * Test a get request to "https://httpbin.org/get" - */ - @Test - fun get() { - val body = client.get(URL("https://httpbin.org/get")) - assertNotNull(body) - val response = Json.decodeFromString(body) - response.run { - assert(args.isEmpty()) - assertEquals("httpbin.org", headers["Host"]) - assertNotNull(headers["User-Agent"]?.startsWith("Java/")) - assertEquals(url, "https://httpbin.org/get") - } - } - -}