Skip to content

Commit

Permalink
Customized HttpResponse Per Platforms (#863)
Browse files Browse the repository at this point in the history
* HttResponse is now Expect/Actual class to make easier to adding more variables.

* Oops

* adding more files for Kotlinx Serializations

* fixed Apple stuffs

* JVM's HttpResponse for body is ResponseBody from OKHttp.

* changing JVM Tests

* remove empty space
  • Loading branch information
iNoles committed May 5, 2023
1 parent e4cb87a commit d9f8251
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import com.github.kittinunf.forge.core.JSON
import fuel.HttpResponse

public fun <T : Any> HttpResponse.toForge(deserializer: JSON.() -> DeserializedResult<T>): DeserializedResult<T> =
Forge.modelFromJson(body, deserializer)
Forge.modelFromJson(body.string(), deserializer)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public val defaultMapper: ObjectMapper = ObjectMapper().registerKotlinModule()

public inline fun <reified T : Any> HttpResponse.toJackson(mapper: ObjectMapper = defaultMapper): Result<T, Throwable> =
doTry(work = {
Result.success(mapper.readValue(body))
Result.success(mapper.readValue(body.string()))
}, errorHandler = {
Result.failure(it)
})
36 changes: 34 additions & 2 deletions fuel-kotlinx-serialization/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,39 @@ kotlin {
}
val jsMain by getting
val jsTest by getting
val iosMain by getting
val iosTest by getting

val appleMain by creating {
dependsOn(commonMain)
}

val iosMain by getting {
dependsOn(appleMain)
}
val macosArm64Main by getting {
dependsOn(appleMain)
}
val macosX64Main by getting {
dependsOn(appleMain)
}
val iosSimulatorArm64Main by getting {
dependsOn(appleMain)
}

val appleTest by creating {
dependsOn(commonTest)
}

val iosTest by getting {
dependsOn(appleTest)
}
val macosArm64Test by getting {
dependsOn(appleTest)
}
val macosX64Test by getting {
dependsOn(appleTest)
}
val iosSimulatorArm64Test by getting {
dependsOn(appleTest)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fuel.serialization

import com.github.kittinunf.result.Result
import com.github.kittinunf.result.runCatching
import fuel.HttpResponse
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.json.Json

public fun <T : Any> HttpResponse.toJson(
json: Json = Json { allowStructuredMapKeys = true },
deserializationStrategy: DeserializationStrategy<T>
): Result<T?, Throwable> = runCatching {
body?.let { json.decodeFromString(deserializationStrategy, it) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fuel.serialization

import com.github.kittinunf.result.Result
import com.github.kittinunf.result.runCatching
import fuel.HttpResponse
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.json.Json

public fun <T : Any> HttpResponse.toJson(
json: Json = Json { allowStructuredMapKeys = true },
deserializationStrategy: DeserializationStrategy<T>
): Result<T?, Throwable> = runCatching {
json.decodeFromString(deserializationStrategy, body.string())
}
16 changes: 5 additions & 11 deletions fuel-moshi-jvm/src/main/kotlin/fuel/moshi/ResponseExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ public val defaultMoshi: Moshi.Builder = Moshi.Builder()

public inline fun <reified T : Any> HttpResponse.toMoshi(): Result<T?, IOException> = toMoshi(T::class.java)

public fun <T : Any> HttpResponse.toMoshi(clazz: Class<T>): Result<T?, IOException> = try {
Result.success(defaultMoshi.build().adapter(clazz).fromJson(body))
} catch (ioe: IOException) {
Result.failure(ioe)
}
public fun <T : Any> HttpResponse.toMoshi(clazz: Class<T>): Result<T?, IOException> =
toMoshi(defaultMoshi.build().adapter(clazz))

public fun <T : Any> HttpResponse.toMoshi(type: Type): Result<T?, IOException> = try {
Result.success(defaultMoshi.build().adapter<T>(type).fromJson(body))
} catch (ioe: IOException) {
Result.failure(ioe)
}
public fun <T : Any> HttpResponse.toMoshi(type: Type): Result<T?, IOException> =
toMoshi(defaultMoshi.build().adapter(type))

public fun <T : Any> HttpResponse.toMoshi(jsonAdapter: JsonAdapter<T>): Result<T?, IOException> = try {
Result.success(jsonAdapter.fromJson(body))
Result.success(jsonAdapter.fromJson(body.source()))
} catch (ioe: IOException) {
Result.failure(ioe)
}
2 changes: 1 addition & 1 deletion fuel/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ kotlin {

val jvmMain by getting {
dependencies {
implementation(libs.okhttp)
api(libs.okhttp)
}
}
val jvmTest by getting {
Expand Down
9 changes: 9 additions & 0 deletions fuel/src/appleMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fuel

import platform.Foundation.NSData

public actual class HttpResponse {
public var statusCode: Int = -1
public var nsData: NSData? = null
public var body: String? = null
}
3 changes: 2 additions & 1 deletion fuel/src/appleMain/kotlin/fuel/HttpUrlFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ internal class HttpUrlFetcher(private val sessionConfiguration: NSURLSessionConf

return HttpResponse().apply {
statusCode = httpResponse.statusCode.toInt()
body = bodyString ?: ""
nsData = data
body = bodyString
}
}

Expand Down
5 changes: 1 addition & 4 deletions fuel/src/commonMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package fuel

public class HttpResponse {
public var statusCode: Int = -1
public var body: String = ""
}
public expect class HttpResponse
11 changes: 11 additions & 0 deletions fuel/src/jsMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fuel

import org.khronos.webgl.ArrayBuffer
import org.w3c.files.Blob

public actual class HttpResponse {
public var statusCode: Int = -1
public lateinit var array: ArrayBuffer
public var body: String = ""
public lateinit var blob: Blob
}
3 changes: 2 additions & 1 deletion fuel/src/jsMain/kotlin/fuel/HttpUrlFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fuel
import kotlinx.browser.window
import kotlinx.coroutines.await
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 @@ -19,7 +18,9 @@ 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()
}
}
}
8 changes: 8 additions & 0 deletions fuel/src/jvmMain/kotlin/fuel/HttpResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fuel

import okhttp3.ResponseBody

public actual class HttpResponse {
public var statusCode: Int = -1
public lateinit var body: ResponseBody
}
2 changes: 1 addition & 1 deletion fuel/src/jvmMain/kotlin/fuel/OKUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal suspend fun Call.await(): HttpResponse = suspendCancellableCoroutine {
override fun onResponse(call: Call, response: Response) {
val httpResponse = HttpResponse().apply {
statusCode = response.code
body = response.body.string()
body = response.body
}
it.resume(httpResponse)
}
Expand Down
8 changes: 4 additions & 4 deletions fuel/src/jvmTest/kotlin/fuel/HttpLoaderBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class HttpLoaderBuilderTest {
mockWebServer.enqueue(MockResponse().setBody("Hello World"))

val request = Request.Builder().url(mockWebServer.url("hello").toString()).build()
val response = JVMHttpLoader().get(request).body
val response = JVMHttpLoader().get(request).body.string()
assertEquals("Hello World", response)

mockWebServer.shutdown()
Expand All @@ -49,7 +49,7 @@ internal class HttpLoaderBuilderTest {
.build()

val request = Request.Builder().url(mockWebServer.url("hello").toString()).build()
val response = httpLoader.get(request).body
val response = httpLoader.get(request).body.string()
assertEquals("Hello World", response)
}

Expand All @@ -60,7 +60,7 @@ internal class HttpLoaderBuilderTest {
val okhttp = OkHttpClient.Builder().callTimeout(30L, TimeUnit.MILLISECONDS).build()
val httpLoader = FuelBuilder().config(okhttp).build()
val request = Request.Builder().url(mockWebServer.url("hello2").toString()).build()
val response = httpLoader.get(request).body
val response = httpLoader.get(request).body.string()
assertEquals("Hello World 2", response)
}

Expand All @@ -70,7 +70,7 @@ internal class HttpLoaderBuilderTest {

val request = Request.Builder().url(mockWebServer.url("socket").toString()).build()
try {
JVMHttpLoader().get(request).body
JVMHttpLoader().get(request).body.string()
} catch (ste: SocketTimeoutException) {
assertNotNull(ste, "socket timeout")
}
Expand Down
6 changes: 3 additions & 3 deletions fuel/src/jvmTest/kotlin/fuel/HttpLoaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class HttpLoaderTest {

val unsuccessfulRequest = Request.Builder().url(mockWebServer.url("get").toString()).build()

val string = httpLoader.get(unsuccessfulRequest).body
val string = httpLoader.get(unsuccessfulRequest).body.string()

val request1 = mockWebServer.takeRequest()

Expand All @@ -44,7 +44,7 @@ internal class HttpLoaderTest {

val request = Request.Builder().url(mockWebServer.url("get").toString()).build()

val string = httpLoader.get(request).body
val string = httpLoader.get(request).body.string()

val request1 = mockWebServer.takeRequest()

Expand Down Expand Up @@ -151,7 +151,7 @@ internal class HttpLoaderTest {
.url(mockWebServer.url("delete").toString())
.build()

val string = httpLoader.delete(request).body
val string = httpLoader.delete(request).body.string()

val request1 = mockWebServer.takeRequest()

Expand Down
4 changes: 2 additions & 2 deletions fuel/src/jvmTest/kotlin/fuel/RoutingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal class RoutingTest {
mockWebServer.enqueue(MockResponse().setBody("Hello World"))

val getTest = TestApi.GetTest(mockWebServer.url("").toString())
val response = JVMHttpLoader().method(getTest.request).body
val response = JVMHttpLoader().method(getTest.request).body.string()
val request1 = mockWebServer.takeRequest()

assertEquals("Hello World", response)
Expand All @@ -76,7 +76,7 @@ internal class RoutingTest {
mockWebServer.enqueue(MockResponse().setBody("Hello World With Params"))

val getTest = TestApi.GetParamsTest(mockWebServer.url("").toString())
val response = JVMHttpLoader().method(getTest.request).body
val response = JVMHttpLoader().method(getTest.request).body.string()
val request1 = mockWebServer.takeRequest()

assertEquals("Hello World With Params", response)
Expand Down

0 comments on commit d9f8251

Please sign in to comment.