Last active
December 6, 2020 11:21
-
-
Save daviddenton/49d397479f2bf7e3ba48182d0a783483 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.http4k.contract.PreFlightExtraction | |
import org.http4k.contract.contract | |
import org.http4k.contract.meta | |
import org.http4k.core.Body | |
import org.http4k.core.Filter | |
import org.http4k.core.Method.POST | |
import org.http4k.core.Request | |
import org.http4k.core.Response | |
import org.http4k.core.Status.Companion.BAD_REQUEST | |
import org.http4k.core.Status.Companion.OK | |
import org.http4k.core.then | |
import org.http4k.format.Jackson.auto | |
import org.http4k.lens.LensFailure | |
import org.http4k.lens.Path | |
data class Blob(val name: String) | |
fun main() { | |
val lens = Body.auto<Blob>().toLens() | |
val filter = Filter { next -> | |
{ | |
try { | |
next(it) | |
} catch (e: LensFailure) { | |
Response(BAD_REQUEST).body(e.cause?.localizedMessage ?: "") | |
} | |
} | |
} | |
val app = contract { | |
val value = Path.of("foo") meta { | |
receiving(lens) | |
preFlightExtraction = PreFlightExtraction.IgnoreBody | |
} bindContract POST to { | |
filter.then { Response(OK).body(lens(it).toString()) } | |
} | |
routes += value | |
} | |
println(app(Request(POST, "/foo").body("{\"name\":\"}"))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment