Skip to content

Commit

Permalink
fix non suspended case
Browse files Browse the repository at this point in the history
  • Loading branch information
abendt committed May 6, 2024
1 parent 2bff2fe commit 0730669
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 156 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ dependencies {
testImplementation("io.ktor:ktor-serialization-kotlinx-json")

testImplementation("io.ktor:ktor-client-logging")

testImplementation("io.kotest.extensions:kotest-extensions-wiremock:3.0.1")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:1.8.0")
}

spotless {
Expand Down
22 changes: 13 additions & 9 deletions src/main/kotlin/arrow/ArrowEitherConverterFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class ArrowEitherConverterFactory : Converter.Factory {
override fun suspendResponseConverter(
typeData: TypeData,
ktorfit: Ktorfit,
): Converter.SuspendResponseConverter<HttpResponse, Either<Throwable, *>>? {
): Converter.SuspendResponseConverter<HttpResponse, Either<*, *>>? {
if (typeData.typeInfo.type == Either::class) {
return object : Converter.SuspendResponseConverter<HttpResponse, Either<Throwable, *>> {
override suspend fun convert(result: KtorfitResult): Either<Throwable, Any> =
return object : Converter.SuspendResponseConverter<HttpResponse, Either<*, *>> {
override suspend fun convert(result: KtorfitResult): Either<Any, Any> =
result.fold(::Left) {
readBody(it, typeData)
}
Expand All @@ -36,22 +36,26 @@ class ArrowEitherConverterFactory : Converter.Factory {
private suspend fun readBody(
httpResponse: HttpResponse,
typeData: TypeData,
): Either<Exception, Any> =
): Either<Any, Any> =
try {
httpResponse.body<Either<Throwable, Any>>(typeData.typeArgs[1].typeInfo).right()
httpResponse.body<Any>(typeData.typeArgs[1].typeInfo).right()
} catch (ex: Exception) {
ex.left()
}

override fun responseConverter(
typeData: TypeData,
ktorfit: Ktorfit,
): Converter.ResponseConverter<HttpResponse, Either<Throwable, *>>? {
): Converter.ResponseConverter<HttpResponse, Either<*, *>>? {
if (typeData.typeInfo.type == Either::class) {
return object : Converter.ResponseConverter<HttpResponse, Either<Throwable, *>> {
override fun convert(getResponse: suspend () -> HttpResponse): Either<Throwable, Any> =
return object : Converter.ResponseConverter<HttpResponse, Either<*, *>> {
override fun convert(getResponse: suspend () -> HttpResponse): Either<Any, Any> =
runBlocking {
readBody(getResponse(), typeData)
try {
readBody(getResponse(), typeData)
} catch (ex: Exception) {
ex.left()
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/demo/ExampleApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ interface ExampleApi {

@Serializable
data class PersonResponse(val name: String, val birth_year: String, val films: List<String>)

@Serializable
data class PersonResponse2(val missingField: String)
Loading

0 comments on commit 0730669

Please sign in to comment.