Hi, I have a use-case where my class type for Mars...
# http4k
g
Hi, I have a use-case where my class type for Marshalling is a variable (not known at compile time). So, I am unable to use reified methods like
auto<T>
. What’s the idiomatic way?
Copy code
fun main() {
  callout(mapOf("<https://pokeapi.co/api/v2/pokemon?limit=10>" to Results::class.java))
}

private fun callout(map: Map<String, Class<out Any>>) {
  for ((url, clazz) in map) {
    val result = JavaHttpClient()(Request(Method.GET, url))
    println(Body.auto(clazz).toLens()(result)); // ^^^ Compiler error: [REIFIED_TYPE_FORBIDDEN_SUBSTITUTION] Cannot use 'CapturedType(out Any)' as reified type parameter
  }
}

inline fun <reified T : Any> Body.Companion.auto(@Suppress("UNUSED_PARAMETER") ignore: Class<T>) = auto<T>()

data class Pokemon(val name: String)
data class Results(val results: List<Pokemon>)
I tried this, is there a better & idiomatic http4k way?
Copy code
import org.http4k.client.JavaHttpClient
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.format.Moshi

fun main() {
  callout(mapOf("<https://pokeapi.co/api/v2/pokemon?limit=10>" to Results::class.java))
}

private fun callout(map: Map<String, Class<out Any>>) {
  for ((url, clazz) in map) {
    val result = JavaHttpClient()(Request(Method.GET, url))
    val pokemon = Moshi.asA(result.bodyString(), clazz.kotlin)
    println(pokemon)
  }
}

data class Pokemon(val name: String)
data class Results(val results: List<Pokemon>)