dave
01/30/2019, 5:15 PMpackage guide.modules.contracts
import org.http4k.contract.ApiInfo
import org.http4k.contract.OpenApi
import org.http4k.contract.contract
import org.http4k.contract.meta
import org.http4k.core.Body
import org.http4k.core.HttpHandler
import org.http4k.core.Method.GET
import <http://org.http4k.core.Method.POST|org.http4k.core.Method.POST>
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.with
import org.http4k.format.Jackson
import org.http4k.format.Jackson.auto
data class MyGreatThing(val value: String)
fun main() {
val bodyLens = Body.auto<MyGreatThing>().toLens()
val handler: HttpHandler =
contract(OpenApi(ApiInfo("awesome", "1.0"), Jackson),
"/echo" meta {
summary = "my echo endpoint"
receiving(bodyLens to MyGreatThing("foobar"))
returning("cool" to Response(OK).with(bodyLens of MyGreatThing("foobar")))
}
bindContract POST to { Response(OK).with(bodyLens of bodyLens(it)) })
println(handler(Request(GET, "/")))
}