https://kotlinlang.org logo
Title
u

Uberto Barbini

04/09/2021, 8:21 AM
hi @Razvan I'm using Kondor with Http4k
r

Razvan

04/09/2021, 9:54 AM
Hi, I could use it in my tests by manually declaring the Body mapping but I was wandering if there was a way to add it as a http4k-format to do the do the automapping or implement
Json<JsonNode>
With manually mapping is pretty straightforward:
val bodyMapper = Body.string(ContentType.APPLICATION_JSON).map(JStatus::fromJsonThrow, JStatus::toJson).toLens()

data class Status(val status: AppStatus, val date: String)

object JStatus : JAny<Status>() {
    val status by str(Status::status)
    val date by str(Status::date)
    override fun JsonNodeObject.deserializeOrThrow() = Status(
        status = +status,
        date = +date
    )

    fun fromJsonThrow(json: String) = fromJson(json).orThrow()
}
If you have some more advanced exemples I’ll happy try to use them as inspiration
u

Uberto Barbini

04/09/2021, 11:46 AM
We did somethink like
.mapToLens(JStatus)
which work with any converter
it's 3 lines of code... let me find them...
r

Razvan

04/09/2021, 12:34 PM
Something like:
fun  <T: Any, K: JAny<T>> K.mapToLens() = Body.string(ContentType.APPLICATION_JSON).map( { this.fromJson(it).orThrow() }) {
    this.toJson(it)
}.toLens()

val bodyLens = JStatus.mapToLens()
? I can se a
T.mapToLens(JAny<T>)
but it won’t be a bidirectional lens just a one way to respond Status as Json.
u

Uberto Barbini

04/09/2021, 12:53 PM
Yes similar, apart that is a bidilens
Instead if JAny use JConverter so it would work also with collections and sealed class
r

Razvan

04/09/2021, 1:15 PM
Thanks I’ll keep playing with and see if I can come around with something working. If I get lost, I’ll reach out. Thank you.
👍 1
u

Uberto Barbini

04/09/2021, 1:15 PM
I will put something in the kondor-json examples and readme, but yes, please continue to experiment
also we use these ext funs:
// for http clients
fun <T: Any> Request.bodyAsJson(converter: JConverter<T>, value: T) =
    body(converter.toJson(value)).contentType(APPLICATION_JSON)

fun <T: Any> Response.parseJsonBody(converter: JConverter<T>): T =
    converter.fromJson(bodyString()).orThrow()

//for http servers

fun <T: Any> Request.parseJsonBody(converter: JConverter<T>): T =
    converter.fromJson(bodyString()).orThrow()

fun <T: Any> Response.bodyAsJson(converter: JConverter<T>, value: T) =
    body(converter.toJson(value)).contentType(APPLICATION_JSON)
r

Razvan

04/09/2021, 3:51 PM
Thanks, useful ones. Btw thanks for pairing with Dmitry for the videos on his youtube channel. Always nice to see how other think and are programming.
u

Uberto Barbini

04/09/2021, 4:33 PM
yeah, we should do more often!
btw me and Dmitry will present something about http4k together at JaxLondon in October
r

Razvan

04/09/2021, 6:00 PM
Nice. looking forward to… It’s awesome you take the time to spread the word about this great lib. After http4k returning to some old Spring Boot projets at works is incredible painful. Thank to all of you that help http4k to gain traction.
👍 1