org.http4k.contract.routeSpec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http4k-contract Show documentation
Show all versions of http4k-contract Show documentation
http4k typesafe HTTP contracts and OpenApi support
package org.http4k.contract
import org.http4k.core.Filter
import org.http4k.core.HttpHandler
import org.http4k.core.Request
import org.http4k.lens.LensFailure
import org.http4k.lens.Path
import org.http4k.lens.PathLens
abstract class ContractRouteSpec internal constructor(val pathFn: (PathSegments) -> PathSegments,
val routeMeta: RouteMeta,
vararg val pathLenses: PathLens<*>) : Filter {
abstract fun with(new: RouteMeta): ContractRouteSpec
abstract infix operator fun div(next: PathLens): ContractRouteSpec
open infix operator fun div(next: String) = div(Path.fixed(next))
override fun invoke(nextHandler: HttpHandler): HttpHandler =
{ req ->
val body = routeMeta.body?.let { listOf(it::invoke) } ?: emptyList<(Request) -> Any?>()
val overallFailure = body.plus(routeMeta.requestParams).fold(null as LensFailure?) { memo, next ->
try {
next(req)
memo
} catch (e: LensFailure) {
memo?.let { LensFailure(it.failures + e.failures, e) } ?: e
}
}
overallFailure?.let { throw it } ?: nextHandler(req)
}
internal fun describe(contractRoot: PathSegments): String = "${pathFn(contractRoot)}${if (pathLenses.isNotEmpty()) "/${pathLenses.joinToString("/")}" else ""}"
}
class ContractRouteSpec0 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta) : ContractRouteSpec(pathFn, routeMeta) {
override fun with(new: RouteMeta): ContractRouteSpec0 = ContractRouteSpec0(pathFn, new)
override infix operator fun div(next: String) = ContractRouteSpec0({ it / next }, routeMeta)
override infix operator fun div(next: PathLens) = ContractRouteSpec1(pathFn, routeMeta, next)
}
class ContractRouteSpec1 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta, val a: PathLens) : ContractRouteSpec(pathFn, routeMeta, a) {
override fun with(new: RouteMeta): ContractRouteSpec1 = ContractRouteSpec1(pathFn, new, a)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec2(pathFn, routeMeta, a, next)
}
class ContractRouteSpec2 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta, val a: PathLens, val b: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b) {
override fun with(new: RouteMeta): ContractRouteSpec2 = ContractRouteSpec2(pathFn, new, a, b)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec3(pathFn, routeMeta, a, b, next)
}
class ContractRouteSpec3 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
val a: PathLens, val b: PathLens, val c: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c) {
override fun with(new: RouteMeta): ContractRouteSpec3 = ContractRouteSpec3(pathFn, new, a, b, c)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec4(pathFn, routeMeta, a, b, c, next)
}
class ContractRouteSpec4 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d) {
override fun with(new: RouteMeta): ContractRouteSpec4 = ContractRouteSpec4(pathFn, new, a, b, c, d)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec5(pathFn, routeMeta, a, b, c, d, next)
}
class ContractRouteSpec5 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e) {
override fun with(new: RouteMeta): ContractRouteSpec5 = ContractRouteSpec5(pathFn, new, a, b, c, d, e)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec6(pathFn, routeMeta, a, b, c, d, e, next)
}
class ContractRouteSpec6 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens, val f: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f) {
override fun with(new: RouteMeta): ContractRouteSpec6 = ContractRouteSpec6(pathFn, new, a, b, c, d, e, f)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec7(pathFn, routeMeta, a, b, c, d, e, f, next)
}
class ContractRouteSpec7 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens,
val f: PathLens, val g: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f, g) {
override fun with(new: RouteMeta): ContractRouteSpec7 = ContractRouteSpec7(pathFn, new, a, b, c, d, e, f, g)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec8(pathFn, routeMeta, a, b, c, d, e, f, g, next)
}
class ContractRouteSpec8 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens,
val f: PathLens, val g: PathLens, val h: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f, g, h) {
override fun with(new: RouteMeta): ContractRouteSpec8 = ContractRouteSpec8(pathFn, new, a, b, c, d, e, f, g, h)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec9(pathFn, routeMeta, a, b, c, d, e, f, g, h, next)
}
class ContractRouteSpec9 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens,
val f: PathLens, val g: PathLens, val h: PathLens, val i: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f, g, h, i) {
override fun with(new: RouteMeta): ContractRouteSpec9 = ContractRouteSpec9(pathFn, new, a, b, c, d, e, f, g, h, i)
override infix operator fun div(next: String) = div(Path.fixed(next))
override infix operator fun div(next: PathLens) = ContractRouteSpec10(pathFn, routeMeta, a, b, c, d, e, f, g, h, i, next)
}
class ContractRouteSpec10 internal constructor(pathFn: (PathSegments) -> PathSegments, routeMeta: RouteMeta,
val a: PathLens, val b: PathLens, val c: PathLens, val d: PathLens, val e: PathLens,
val f: PathLens, val g: PathLens, val h: PathLens, val i: PathLens,
val j: PathLens) : ContractRouteSpec(pathFn, routeMeta, a, b, c, d, e, f, g, h, i, j) {
override fun with(new: RouteMeta): ContractRouteSpec10 = ContractRouteSpec10(pathFn, new, a, b, c, d, e, f, g, h, i, j)
override infix operator fun div(next: String) = throw UnsupportedOperationException("no longer paths!")
override infix operator fun div(next: PathLens) = throw UnsupportedOperationException("no longer paths!")
}