Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Response Headers #908

Merged
merged 8 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public fun <T : Any> HttpResponse.toJson(
json: Json = Json { allowStructuredMapKeys = true },
deserializationStrategy: DeserializationStrategy<T>
): Result<T?, Throwable> = runCatching {
json.decodeFromString(deserializationStrategy, body)
json.decodeFromString(deserializationStrategy, response?.json().toString())
}
1 change: 1 addition & 0 deletions fuel/src/appleMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> = emptyMap()
}
9 changes: 9 additions & 0 deletions fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
val map = mutableMapOf<String, String>()
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)!!
}
8 changes: 3 additions & 5 deletions fuel/src/jsMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
@@ -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<String, String> = emptyMap()
}
15 changes: 12 additions & 3 deletions fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String, String> {
val headers = mutableMapOf<String, String>()
[email protected]().forEach { value: String, key: String ->
headers.put(key, value)
}
return headers
}
}
1 change: 1 addition & 0 deletions fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> = emptyMap()
}
12 changes: 12 additions & 0 deletions fuel/src/jvmMain/kotlin/fuel/JVMHttpLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -67,9 +68,20 @@ public class JVMHttpLoader(callFactoryLazy: Lazy<Call.Factory>) : HttpLoader {
return HttpResponse().apply {
statusCode = response.code
body = response.body
headers = response.toHeaders()
}
}

private fun Response.toHeaders(): Map<String, String> {
val header = mutableMapOf<String, String>()
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) {
Expand Down
11 changes: 11 additions & 0 deletions fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Loading