From 30a86047aa3202045c14ba8aceb799e23532c77d Mon Sep 17 00:00:00 2001 From: Jonathan Steele Date: Tue, 14 May 2024 22:15:29 -0400 Subject: [PATCH 1/3] added headers for HTTPRespobse --- fuel/src/appleMain/kotlin/fuel/HttpResponse.kt | 1 + fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt | 9 +++++++++ fuel/src/jsMain/kotlin/fuel/HttpResponse.kt | 8 +++----- fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt | 15 ++++++++++++--- fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt | 1 + fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt | 12 ++++++++++++ 6 files changed, 38 insertions(+), 8 deletions(-) diff --git a/fuel/src/appleMain/kotlin/fuel/HttpResponse.kt b/fuel/src/appleMain/kotlin/fuel/HttpResponse.kt index bc742add..594c657c 100644 --- a/fuel/src/appleMain/kotlin/fuel/HttpResponse.kt +++ b/fuel/src/appleMain/kotlin/fuel/HttpResponse.kt @@ -6,4 +6,5 @@ public actual class HttpResponse { public var statusCode: Int = -1 public var nsData: NSData? = null public var body: String? = null + public var headers: Map = emptyMap() } diff --git a/fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt b/fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt index 6d812e0d..d7c13c10 100644 --- a/fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt +++ b/fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt @@ -85,9 +85,18 @@ internal class HttpUrlFetcher(private val sessionConfiguration: NSURLSessionConf statusCode = httpResponse.statusCode.toInt() nsData = data body = bodyString + headers = httpResponse.readHeaders() } } + private fun NSHTTPURLResponse.readHeaders(): Map { + val map = mutableMapOf() + allHeaderFields.forEach { + map[it.key as String] = it.value as String + } + return map + } + @BetaInteropApi private fun String.encode(): NSData = NSString.create(string = this).dataUsingEncoding(NSWindowsCP1251StringEncoding)!! } diff --git a/fuel/src/jsMain/kotlin/fuel/HttpResponse.kt b/fuel/src/jsMain/kotlin/fuel/HttpResponse.kt index 755cdbf2..df8414a1 100644 --- a/fuel/src/jsMain/kotlin/fuel/HttpResponse.kt +++ b/fuel/src/jsMain/kotlin/fuel/HttpResponse.kt @@ -1,11 +1,9 @@ package fuel -import org.khronos.webgl.ArrayBuffer -import org.w3c.files.Blob +import org.w3c.fetch.Response public actual class HttpResponse { public var statusCode: Int = -1 - public lateinit var array: ArrayBuffer - public var body: String = "" - public lateinit var blob: Blob + public var response: Response? = null + public var headers: Map = emptyMap() } diff --git a/fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt b/fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt index 4485366f..f34fd9ac 100644 --- a/fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt +++ b/fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt @@ -2,7 +2,9 @@ package fuel import kotlinx.browser.window import kotlinx.coroutines.await +import org.w3c.fetch.Headers import org.w3c.fetch.RequestInit + internal class HttpUrlFetcher { suspend fun fetch(request: Request, method: String?, body: String? = null): HttpResponse { val urlString = request.parameters?.let { @@ -18,9 +20,16 @@ internal class HttpUrlFetcher { ).await() return HttpResponse().apply { this.statusCode = res.status.toInt() - this.array = res.arrayBuffer().await() - this.body = res.text().await() - this.blob = res.blob().await() + this.response = res + this.headers = res.headers.mapToFuel() + } + } + + private fun Headers.mapToFuel(): Map { + val headers = mutableMapOf() + this@mapToFuel.asDynamic().forEach { value: String, key: String -> + headers.put(key, value) } + return headers } } diff --git a/fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt b/fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt index 2f55257e..3a0e4ba7 100644 --- a/fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt +++ b/fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt @@ -5,4 +5,5 @@ import okhttp3.ResponseBody public actual class HttpResponse { public var statusCode: Int = -1 public lateinit var body: ResponseBody + public var headers: Map = emptyMap() } diff --git a/fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt b/fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt index b80ac6bd..0bf7f48a 100644 --- a/fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt +++ b/fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt @@ -3,6 +3,7 @@ package fuel import okhttp3.Call import okhttp3.Request.Builder import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Response import okhttp3.executeAsync import okhttp3.internal.http.HttpMethod @@ -67,9 +68,20 @@ public class JVMHttpLoader(callFactoryLazy: Lazy) : HttpLoader { return HttpResponse().apply { statusCode = response.code body = response.body + headers = response.toHeaders() } } + private fun Response.toHeaders(): Map { + val header = mutableMapOf() + for ((key, values) in headers) { + for (value in values) { + header[key] = value.toString() + } + } + return header + } + private fun createRequestBuilder(request: Request, method: String): Builder { val builder = Builder() with(builder) { From 0142ed7cab4a1325ee8de0baf7d8e7f424f75770 Mon Sep 17 00:00:00 2001 From: Jonathan Steele Date: Tue, 14 May 2024 22:32:23 -0400 Subject: [PATCH 2/3] change json function to use response's json functions. --- .../src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuel-kotlinx-serialization/src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt b/fuel-kotlinx-serialization/src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt index 032263ae..90bcc2e2 100644 --- a/fuel-kotlinx-serialization/src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt +++ b/fuel-kotlinx-serialization/src/jsMain/kotlin/fuel/serialization/ResponseExtensions.kt @@ -10,5 +10,5 @@ public fun HttpResponse.toJson( json: Json = Json { allowStructuredMapKeys = true }, deserializationStrategy: DeserializationStrategy ): Result = runCatching { - json.decodeFromString(deserializationStrategy, body) + json.decodeFromString(deserializationStrategy, response?.json().toString()) } From d140ec8ff34520037014706ad3b84c3a499b69a4 Mon Sep 17 00:00:00 2001 From: Jonathan Steele Date: Mon, 20 May 2024 19:42:25 -0400 Subject: [PATCH 3/3] add one header test function --- fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt b/fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt index d1677e1a..15a88624 100644 --- a/fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt +++ b/fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt @@ -38,6 +38,17 @@ internal class HttpLoaderBuilderTest { mockWebServer.shutdown() } + @Test + fun `default okhttp settings with headers`() = runBlocking { + mockWebServer.enqueue(MockResponse().setBody("Hello World")) + val response = JVMHttpLoader().get { + url = mockWebServer.url("hello").toString() + }.headers + assertEquals("1", response["Content-Length"]) + + mockWebServer.shutdown() + } + @Test fun `setting connect timeouts`() = runBlocking { mockWebServer.enqueue(MockResponse().setBody("Hello World"))