Hello hello! I was wondering, I have an app that h...
# ktor
j
Hello hello! I was wondering, I have an app that has a KTOR client. There are many CRUD operations in the app and not really the ability to abstract it all away nicely, due to reified function calls within ktor library. I have for example an abstract base class which I want to have set/get/delete/update operations with the generic types I need. But within this base class, I cannot do:
Copy code
abstract class ApiDataSource<T>(private val httpClient: HttpClient) {

    fun update(value: T) {
        httpClient.put {
            setBody(value) // <-- T needs to be reified
        }
    }
}
As of course setBody needs to have a reified type. I was looking through the functions, and nowhere can I pass a type or kclass. Am I missing something? Also, a workaround would be to serialize the body myself (by passing the KSerializer in the constructor and Json and setBody with the serialized text), but would this be preferable? I rather serialize through using the ContentNegotiation plugin
r
Hm, we may need to provide
fun setBody(body: Any, type: TypeInfo)
. So far you can set
request.body
and
request.bodyType
manually, but you will need to opt-in to usage of properties marked with
@InternalAPI
👍 1
j
thanks for the answer! At least I am not missing anything then 🙂
my current "workaround" is to provide inline functions from the base class (instead of
get
->`doGet` and
update
->
doUpdate
so I map the implementation's method to this inline function in the base class:
override fun update(value: SomeDataClass) = doUpdate(value)
But then for every class I need to provide these overrides so that is extra effort, especially if I change the interface.