mattTea
04/14/2021, 5:05 PM"/path/{param}"
pattern, or is this only possible for contract routes in the "/path" / Path.uuid().of("param")
pattern?
Here’s an example of the contract routing style, where http4k handles type-safety for the path parameter (n.b. using Result4k for error handling in these examples)…
fun getThingRoute(retriever: Retriever) =
"/path" / Path.uuid().of("id") bindContract GET to { id ->
{
retriever(id)
.map { Response(OK).body(it) }
.mapFailure { it.toErrorResponse() }
.get()
}
}
And the non-contract routing pattern I’d like to switch to, but which I think needs our own typesafe error handling (if we want to handle non-UUID path parameters, for example)...
fun getThingRoute(retriever: Retriever) =
"/path/{id}-something-after-param" bind GET to { request ->
resultFrom { UUID.fromString(request.path("id")) }
.mapFailure { NotFound("ID must be a valid UUID") }
.flatMap { id ->
retriever(id)
.map { Response(OK).body(it) }
}
.mapFailure { it.toErrorResponse() }
.get()
}
Thanks!dave
04/15/2021, 8:43 PMdave
04/15/2021, 8:44 PMdave
04/15/2021, 8:44 PMmattTea
04/16/2021, 9:35 AM