!htc WIP #19956 removing case classes from HttpMessages
This commit is contained in:
parent
b3c691e4a8
commit
c69efe4dd8
5 changed files with 228 additions and 13 deletions
|
|
@ -13,7 +13,7 @@ import scala.collection.immutable
|
|||
import scala.reflect.{ classTag, ClassTag }
|
||||
import akka.parboiled2.CharUtils
|
||||
import akka.stream.Materializer
|
||||
import akka.util.ByteString
|
||||
import akka.util.{HashCode, ByteString}
|
||||
import akka.http.impl.util._
|
||||
import akka.http.javadsl.{ model ⇒ jm }
|
||||
import akka.http.scaladsl.util.FastFuture._
|
||||
|
|
@ -134,11 +134,14 @@ object HttpMessage {
|
|||
/**
|
||||
* The immutable model HTTP request model.
|
||||
*/
|
||||
final case class HttpRequest(method: HttpMethod = HttpMethods.GET,
|
||||
uri: Uri = Uri./,
|
||||
headers: immutable.Seq[HttpHeader] = Nil,
|
||||
entity: RequestEntity = HttpEntity.Empty,
|
||||
protocol: HttpProtocol = HttpProtocols.`HTTP/1.1`) extends jm.HttpRequest with HttpMessage {
|
||||
final class HttpRequest(
|
||||
val method: HttpMethod,
|
||||
val uri: Uri,
|
||||
val headers: immutable.Seq[HttpHeader],
|
||||
val entity: RequestEntity,
|
||||
val protocol: HttpProtocol)
|
||||
extends jm.HttpRequest with HttpMessage {
|
||||
|
||||
HttpRequest.verifyUri(uri)
|
||||
require(entity.isKnownEmpty || method.isEntityAccepted, s"Requests with method '${method.value}' must have an empty entity")
|
||||
require(protocol != HttpProtocols.`HTTP/1.0` || !entity.isInstanceOf[HttpEntity.Chunked],
|
||||
|
|
@ -196,6 +199,46 @@ final case class HttpRequest(method: HttpMethod = HttpMethods.GET,
|
|||
override def getUri: jm.Uri = uri.asJava
|
||||
/** Java API */
|
||||
override def withUri(uri: jm.Uri): HttpRequest = copy(uri = uri.asScala)
|
||||
|
||||
/* Manual Case Class things, to easen bin-compat */
|
||||
|
||||
def copy(method: HttpMethod = method,
|
||||
uri: Uri = uri,
|
||||
headers: immutable.Seq[HttpHeader] = headers,
|
||||
entity: RequestEntity = entity,
|
||||
protocol: HttpProtocol = protocol) = new HttpRequest(method, uri, headers, entity, protocol)
|
||||
|
||||
|
||||
|
||||
override def hashCode(): Int = {
|
||||
var result = HashCode.SEED
|
||||
result = HashCode.hash(result, _1)
|
||||
result = HashCode.hash(result, _2)
|
||||
result = HashCode.hash(result, _3)
|
||||
result = HashCode.hash(result, _4)
|
||||
result = HashCode.hash(result, _5)
|
||||
result
|
||||
}
|
||||
|
||||
override def equals(obj: scala.Any): Boolean = obj match {
|
||||
case HttpRequest(_method, _uri, _headers, _entity, _protocol) =>
|
||||
method == _method &&
|
||||
uri == _uri &&
|
||||
headers == _headers &&
|
||||
entity == _entity &&
|
||||
protocol == _protocol
|
||||
case _ => false
|
||||
}
|
||||
|
||||
override def toString = s"""HttpRequest(${_1},${_2},${_3},${_4},${_5})"""
|
||||
|
||||
// name-based unapply accessors
|
||||
def _1 = method
|
||||
def _2 = uri
|
||||
def _3 = headers
|
||||
def _4 = entity
|
||||
def _5 = protocol
|
||||
|
||||
}
|
||||
|
||||
object HttpRequest {
|
||||
|
|
@ -239,15 +282,28 @@ object HttpRequest {
|
|||
case _ ⇒ throw new IllegalArgumentException("""`uri` must have scheme "http", "https" or no scheme""")
|
||||
}
|
||||
}
|
||||
|
||||
/* Manual Case Class things, to easen bin-compat */
|
||||
|
||||
def apply(method: HttpMethod = HttpMethods.GET,
|
||||
uri: Uri = Uri./,
|
||||
headers: immutable.Seq[HttpHeader] = Nil,
|
||||
entity: RequestEntity = HttpEntity.Empty,
|
||||
protocol: HttpProtocol = HttpProtocols.`HTTP/1.1`) = new HttpRequest(method, uri, headers, entity, protocol)
|
||||
|
||||
def unapply(any: HttpRequest) = new OptHttpRequest(any)
|
||||
}
|
||||
|
||||
/**
|
||||
* The immutable HTTP response model.
|
||||
*/
|
||||
final case class HttpResponse(status: StatusCode = StatusCodes.OK,
|
||||
headers: immutable.Seq[HttpHeader] = Nil,
|
||||
entity: ResponseEntity = HttpEntity.Empty,
|
||||
protocol: HttpProtocol = HttpProtocols.`HTTP/1.1`) extends jm.HttpResponse with HttpMessage {
|
||||
final class HttpResponse(
|
||||
val status: StatusCode,
|
||||
val headers: immutable.Seq[HttpHeader],
|
||||
val entity: ResponseEntity,
|
||||
val protocol: HttpProtocol)
|
||||
extends jm.HttpResponse with HttpMessage {
|
||||
|
||||
require(entity.isKnownEmpty || status.allowsEntity, "Responses with this status code must have an empty entity")
|
||||
require(protocol == HttpProtocols.`HTTP/1.1` || !entity.isInstanceOf[HttpEntity.Chunked],
|
||||
"HTTP/1.0 responses must not have a chunked entity")
|
||||
|
|
@ -272,4 +328,58 @@ final case class HttpResponse(status: StatusCode = StatusCodes.OK,
|
|||
override def withEntity(entity: jm.RequestEntity): HttpResponse = withEntity(entity: jm.ResponseEntity)
|
||||
|
||||
def mapEntity(f: ResponseEntity ⇒ ResponseEntity): HttpResponse = withEntity(f(entity))
|
||||
}
|
||||
|
||||
/* Manual Case Class things, to easen bin-compat */
|
||||
|
||||
def copy(status: StatusCode = status,
|
||||
headers: immutable.Seq[HttpHeader] = headers,
|
||||
entity: ResponseEntity = entity,
|
||||
protocol: HttpProtocol = protocol) = new HttpResponse(status, headers, entity, protocol)
|
||||
|
||||
|
||||
override def equals(obj: scala.Any): Boolean = obj match {
|
||||
case HttpResponse(_status, _headers, _entity, _protocol) =>
|
||||
status == _status &&
|
||||
headers == _headers &&
|
||||
entity == _entity &&
|
||||
protocol == _protocol
|
||||
case _ => false
|
||||
}
|
||||
|
||||
override def hashCode: Int = {
|
||||
var result = HashCode.SEED
|
||||
result = HashCode.hash(result, _1)
|
||||
result = HashCode.hash(result, _2)
|
||||
result = HashCode.hash(result, _3)
|
||||
result = HashCode.hash(result, _4)
|
||||
result
|
||||
}
|
||||
|
||||
override def toString = s"""HttpResponse(${_1},${_2},${_3},${_4})"""
|
||||
|
||||
// name-based unapply accessors
|
||||
def _1 = this.status
|
||||
def _2 = this.headers
|
||||
def _3 = this.entity
|
||||
def _4 = this.protocol
|
||||
|
||||
}
|
||||
|
||||
object HttpResponse {
|
||||
/* Manual Case Class things, to easen bin-compat */
|
||||
|
||||
def apply(status: StatusCode = StatusCodes.OK,
|
||||
headers: immutable.Seq[HttpHeader] = Nil,
|
||||
entity: ResponseEntity = HttpEntity.Empty,
|
||||
protocol: HttpProtocol = HttpProtocols.`HTTP/1.1`) = new HttpResponse(status, headers, entity, protocol)
|
||||
|
||||
def unapply(any: HttpResponse): OptHttpResponse = new OptHttpResponse(any)
|
||||
}
|
||||
|
||||
final class OptHttpRequest(val get: HttpRequest) extends AnyVal {
|
||||
def isEmpty: Boolean = get == null
|
||||
}
|
||||
|
||||
final class OptHttpResponse(val get: HttpResponse) extends AnyVal {
|
||||
def isEmpty: Boolean = get == null
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue