Response Converter #314
Unanswered
joseluisgs
asked this question in
Q&A
Replies: 2 comments
-
|
Hi @joseluisgs , you need to replace "com.example.model.MyOwnResponse" with kotlin.Result . Otherwise no converter for Result can be found, and Ktor tries to convert the Response to Result |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Hi @Foso I include the solution for everyone!! Another question, my final configuration is ok? Anything else to improve it? Thank you so much! class KotlinResultConverter : SuspendResponseConverter {
override fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {
return typeData.qualifiedName == "kotlin.Result"
}
override suspend fun <RequestType> wrapSuspendResponse(
typeData: TypeData,
requestFunction: suspend () -> Pair<TypeInfo, HttpResponse>,
ktorfit: Ktorfit
): Any {
return try {
val (info, response) = requestFunction()
Result.success<Any>(response.body(info))
} catch (ex: Throwable) {
Result.failure(ex)
}
}
}Ktorfit.Builder()
.httpClient {
install(ContentNegotiation) {
json(Json { isLenient = true; ignoreUnknownKeys = true })
}
install(DefaultRequest) {
header(HttpHeaders.ContentType, ContentType.Application.Json)
}
}
.baseUrl(API_URL)
.responseConverter(FlowResponseConverter())
.responseConverter(KotlinResultConverter())
.build().create<KtorFitRest>() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi @Foso
I will try to make a Response Converter using a Result class. I will try th use the Result Kotlin class https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/
But when I use:
@get("api/users/{id}")
suspend fun getById(@path("id") id: Int): Result
I will I have
Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'Result' is not found.
Mark the class as @serializable or provide the serializer explicitly.
This is my implementation
class KtorFitResponseConverter : SuspendResponseConverter {
}
I try to have my own ResponseConverter using your web
https://foso.github.io/Ktorfit/responseconverter/
But I always have problem with serialization
Please could you give me a example or advice to make it using Result Kotlin, my own result or for example https://github.com/michaelbull/kotlin-result
I will appreciate it very much
Than you very much!
Beta Was this translation helpful? Give feedback.
All reactions