org.http4k.contract.extensions.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.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Uri
import org.http4k.lens.PathLens
fun contract(vararg serverRoutes: ContractRoute) = contract(NoRenderer, "", NoSecurity, *serverRoutes)
fun contract(renderer: ContractRenderer, vararg serverRoutes: ContractRoute) = contract(renderer, "", NoSecurity, *serverRoutes)
fun contract(renderer: ContractRenderer, descriptionPath: String, vararg serverRoutes: ContractRoute) = contract(renderer, descriptionPath, NoSecurity, *serverRoutes)
fun contract(renderer: ContractRenderer = NoRenderer, descriptionPath: String = "", security: Security = NoSecurity, vararg serverRoutes: ContractRoute) =
ContractRoutingHttpHandler(renderer, security, descriptionPath, serverRoutes.map { it })
operator fun String.div(next: PathLens): ContractRouteSpec1 = ContractRouteSpec0(toBaseFn(this), RouteMeta()) / next
operator fun PathLens.div(next: PathLens): ContractRouteSpec2 = ContractRouteSpec1({ it }, RouteMeta(), this) / next
infix fun String.bind(router: ContractRoutingHttpHandler): ContractRoutingHttpHandler = router.withBasePath(this)
interface RouteBinder {
fun newRequest(baseUri: Uri = Uri.of("")): Request
infix fun to(fn: T): ContractRoute
}
infix fun PathLens.bindContract(method: Method) = ContractRouteSpec1({ it }, RouteMeta(), this).bindContract(method)
infix fun String.bindContract(method: Method): RouteBinder = ContractRouteSpec0(toBaseFn(this), RouteMeta()).bindContract(method)
infix fun ContractRouteSpec0.bindContract(method: Method) = let { spec ->
object : RouteBinder {
override fun newRequest(baseUri: Uri) = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta) { fn }
}
}
infix fun ContractRouteSpec1.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A) -> HttpHandler> {
override fun newRequest(baseUri: Uri) = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta) { fn(it[spec.a]) }
}
}
infix fun ContractRouteSpec2.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B) -> HttpHandler> {
override fun newRequest(baseUri: Uri) = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta) { fn(it[spec.a], it[spec.b]) }
}
}
infix fun ContractRouteSpec3.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B, C) -> HttpHandler> {
override fun newRequest(baseUri: Uri) = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B, C) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta) { fn(it[spec.a], it[spec.b], it[spec.c]) }
}
}
infix fun ContractRouteSpec4.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B, C, D) -> HttpHandler> {
override fun newRequest(baseUri: Uri): Request = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B, C, D) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta) { fn(it[spec.a], it[spec.b], it[spec.c], it[spec.d]) }
}
}
infix fun ContractRouteSpec5.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B, C, D, E) -> HttpHandler> {
override fun newRequest(baseUri: Uri): Request = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B, C, D, E) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta) { fn(it[spec.a], it[spec.b], it[spec.c], it[spec.d], it[spec.e]) }
}
}
infix fun ContractRouteSpec6.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B, C, D, E, F) -> HttpHandler> {
override fun newRequest(baseUri: Uri): Request = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B, C, D, E, F) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta
) { fn(it[spec.a], it[spec.b], it[spec.c], it[spec.d], it[spec.e], it[spec.f]) }
}
}
infix fun ContractRouteSpec7.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B, C, D, E, F, G) -> HttpHandler> {
override fun newRequest(baseUri: Uri): Request = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B, C, D, E, F, G) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta
) { fn(it[spec.a], it[spec.b], it[spec.c], it[spec.d], it[spec.e], it[spec.f], it[spec.g]) }
}
}
infix fun ContractRouteSpec8.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B, C, D, E, F, G, H) -> HttpHandler> {
override fun newRequest(baseUri: Uri): Request = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B, C, D, E, F, G, H) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta
) { fn(it[spec.a], it[spec.b], it[spec.c], it[spec.d], it[spec.e], it[spec.f], it[spec.g], it[spec.h]) }
}
}
infix fun ContractRouteSpec9.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B, C, D, E, F, G, H, I) -> HttpHandler> {
override fun newRequest(baseUri: Uri): Request = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B, C, D, E, F, G, H, I) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta
) { fn(it[spec.a], it[spec.b], it[spec.c], it[spec.d], it[spec.e], it[spec.f], it[spec.g], it[spec.h], it[spec.i]) }
}
}
infix fun ContractRouteSpec10.bindContract(method: Method) = let { spec ->
object : RouteBinder<(A, B, C, D, E, F, G, H, I, J) -> HttpHandler> {
override fun newRequest(baseUri: Uri): Request = Request(method, "").uri(baseUri.path(spec.describe(Root)))
override fun to(fn: (A, B, C, D, E, F, G, H, I, J) -> HttpHandler): ContractRoute = ContractRoute(method, spec, spec.routeMeta
) { fn(it[spec.a], it[spec.b], it[spec.c], it[spec.d], it[spec.e], it[spec.f], it[spec.g], it[spec.h], it[spec.i], it[spec.j]) }
}
}
infix fun String.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec0(toBaseFn(this), routeMetaDsl(new))
infix fun ContractRouteSpec0.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec0(pathFn, routeMetaDsl(new))
infix fun ContractRouteSpec1.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec1(pathFn, routeMetaDsl(new), a)
infix fun ContractRouteSpec2.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec2(pathFn, routeMetaDsl(new), a, b)
infix fun ContractRouteSpec3.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec3(pathFn, routeMetaDsl(new), a, b, c)
infix fun ContractRouteSpec4.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec4(pathFn, routeMetaDsl(new), a, b, c, d)
infix fun ContractRouteSpec5.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec5(pathFn, routeMetaDsl(new), a, b, c, d, e)
infix fun ContractRouteSpec6.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec6(pathFn, routeMetaDsl(new), a, b, c, d, e, f)
infix fun ContractRouteSpec7.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec7(pathFn, routeMetaDsl(new), a, b, c, d, e, f, g)
infix fun ContractRouteSpec8.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec8(pathFn, routeMetaDsl(new), a, b, c, d, e, f, g, h)
infix fun ContractRouteSpec9.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec9(pathFn, routeMetaDsl(new), a, b, c, d, e, f, g, h, i)
infix fun ContractRouteSpec10.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec10(pathFn, routeMetaDsl(new), a, b, c, d, e, f, g, h, i, j)
internal fun toBaseFn(path: String): (PathSegments) -> PathSegments = PathSegments(path).let { { old: PathSegments -> old / it } }