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

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

There is a newer version: 5.31.0.0
Show newest version
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))

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy