org.http4k.contract.routeMeta.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.ContentType
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.core.with
import org.http4k.lens.BiDiBodyLens
import org.http4k.lens.Header
data class Tag(val name: String, val description: String? = null)
data class RouteMeta private constructor(val summary: String,
val description: String?,
val request: Request? = null,
val tags: Set = emptySet(),
val produces: Set = emptySet(),
val consumes: Set = emptySet(),
val responses: Map> = emptyMap()) {
constructor(name: String = "", description: String? = null) : this(name, description, null)
fun taggedWith(tag: String) = taggedWith(Tag(tag))
fun taggedWith(vararg new: Tag) = copy(tags = tags.plus(new))
@JvmName("returningResponse")
fun returning(new: Pair) =
copy(
produces = produces.plus(Header.Common.CONTENT_TYPE(new.second)?.let { listOf(it) } ?: emptyList()),
responses = responses.plus(new.second.status to new))
@JvmName("returningStatus")
fun returning(new: Pair) = returning(new.first to Response(new.second))
fun receiving(new: Pair, T>): RouteMeta = copy(request = Request(GET, "").with(new.first of new.second))
fun producing(vararg new: ContentType) = copy(produces = produces.plus(new))
fun consuming(vararg new: ContentType) = copy(consumes = consumes.plus(new))
}