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

com.justai.jaicf.channel.http.HttpBotRequest.kt Maven / Gradle / Ivy

package com.justai.jaicf.channel.http

import java.io.ByteArrayInputStream
import java.io.InputStream
import java.nio.charset.Charset

/**
 * Contains details of the HTTP request to the corresponding [HttpBotChannel]
 *
 * @property stream input stream containing a request data
 * @property headers request HTTP headers
 * @property parameters HTTP query parameters
 * @property requestMetadata optional metadata for request processing
 */
class HttpBotRequest(
    stream: InputStream,
    val headers: Map> = mapOf(),
    val parameters: Map> = mapOf(),
    val requestMetadata: String? = null
) {
    val stream = stream.buffered()

    fun receiveText(charset: Charset = Charset.forName("UTF-8")) = stream.runAndReset { bufferedReader(charset).readText() }

    fun firstHeader(name: String) = headers[name]?.first()

    fun firstParameter(name: String) = parameters[name]?.first()

    override fun toString() =
        "HttpBotRequest(stream=$stream, headers=$headers, parameters=$parameters, requestMetadata=$requestMetadata"
}

fun  InputStream.runAndReset(action: InputStream.() -> R): R {
    mark(0)
    return action().also { reset() }
}

fun String.asHttpBotRequest(requestMetadata: String? = null) = HttpBotRequest(
    ByteArrayInputStream(this.toByteArray()),
    requestMetadata = requestMetadata
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy