All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.contract.extensions.kt Maven / Gradle / Ivy

There is a newer version: 5.31.0.0
Show newest version
package org.http4k.contract

import org.http4k.contract.PreFlightExtraction.Companion.All
import org.http4k.contract.security.Security
import org.http4k.core.Filter
import org.http4k.core.Method
import org.http4k.core.NoOp
import org.http4k.lens.Path
import org.http4k.lens.PathLens
import org.http4k.util.Appendable

fun contract(fn: ContractBuilder.() -> Unit) = ContractBuilder().apply(fn).run {
    ContractRoutingHttpHandler(
        renderer, security, tags.all.toSet(),
        descriptionSecurity,
        descriptionPath,
        preFlightExtraction,
        routes.all,
        preSecurityFilter = preSecurityFilter,
        postSecurityFilter = postSecurityFilter,
        includeDescriptionRoute = includeDescriptionRoute,
        webhooks = webhooks
    )
}

class ContractBuilder internal constructor() {
    var renderer: ContractRenderer = NoRenderer
    var security: Security? = null
    val tags = Appendable()
    var descriptionSecurity: Security? = null
    var descriptionPath = ""
    var preFlightExtraction: PreFlightExtraction = All
    var routes = Appendable()
    var preSecurityFilter = Filter.NoOp
    var postSecurityFilter = Filter.NoOp
    var includeDescriptionRoute = false
    internal val webhooks = mutableMapOf>()
}

fun ContractBuilder.webhook(name: String, fn: () -> WebCallback) {
    webhooks.getOrPut(name) { mutableListOf() } += fn()
}

operator fun  String.div(next: PathLens): ContractRouteSpec1 =
    ContractRouteSpec0(toBaseFn(this), RouteMeta()) / next

operator fun String.div(next: String) = "$this/$next"

operator fun  PathLens.div(next: String): ContractRouteSpec2 = this / Path.fixed(next)
operator fun  PathLens.div(next: PathLens): ContractRouteSpec2 =
    ContractRouteSpec1({ it }, RouteMeta(), this) / next

infix fun String.bind(router: ContractRoutingHttpHandler): ContractRoutingHttpHandler = router.withBasePath(this)

infix fun  PathLens.bindContract(method: Method) =
    ContractRouteSpec1({ it }, RouteMeta(), this).bindContract(method)

infix fun String.bindContract(method: Method) = ContractRouteSpec0(toBaseFn(this), RouteMeta()).bindContract(method)

infix fun String.meta(new: RouteMetaDsl.() -> Unit) = ContractRouteSpec0(toBaseFn(this), routeMetaDsl(new))
infix fun  PathLens.meta(new: RouteMetaDsl.() -> Unit) = "/" / this meta (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 } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy