Skip to content

Commit

Permalink
Optimize some zio.http.endpoint.Endpoint code (#3213)
Browse files Browse the repository at this point in the history
Co-authored-by: Nabil Abdel-Hafeez <[email protected]>
  • Loading branch information
guizmaii and 987Nabil authored Dec 24, 2024
1 parent 267c6ce commit 079f3d8
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions zio-http/shared/src/main/scala/zio/http/endpoint/Endpoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
): HttpCodec[HttpCodecType.RequestType, AuthedInput] = {
input ++ authCodec
}.asInstanceOf[HttpCodec[HttpCodecType.RequestType, AuthedInput]]

type AuthedInput = authCombiner.Out

/**
Expand Down Expand Up @@ -295,13 +296,11 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
self.alternatives.map { case (endpoint, condition) =>
Handler.fromFunctionZIO { (request: zio.http.Request) =>
val outputMediaTypes =
NonEmptyChunk
.fromChunk(
request.headers
.getAll(Header.Accept)
.flatMap(_.mimeTypes),
)
.getOrElse(defaultMediaTypes)
request.headers
.getAll(Header.Accept)
.flatMap(_.mimeTypes)
.nonEmptyOrElse(defaultMediaTypes)(ZIO.identityFn)

(endpoint.input ++ authCodec(endpoint.authType)).decodeRequest(request, config).orDie.flatMap { value =>
original(value).foldZIO(
success = output => Exit.succeed(endpoint.output.encodeResponse(output, outputMediaTypes, config)),
Expand All @@ -312,15 +311,17 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
}

// TODO: What to do if there are no endpoints??
def handlers2(handlers: Chunk[(Handler[Env, Nothing, Request, Response], HttpCodec.Fallback.Condition)]) =
NonEmptyChunk
.fromChunk(handlers)
.getOrElse(
NonEmptyChunk(
Handler.fail(zio.http.Response(status = Status.NotFound)) -> HttpCodec.Fallback.Condition.IsHttpCodecError,
),
def handlers2(
handlers: Chunk[(Handler[Env, Nothing, Request, Response], HttpCodec.Fallback.Condition)],
): NonEmptyChunk[(Handler[Env, Response, Request, Response], HttpCodec.Fallback.Condition)] = {
def noFound: NonEmptyChunk[(Handler[Env, Response, Request, Response], HttpCodec.Fallback.Condition)] =
NonEmptyChunk(
Handler.fail(zio.http.Response(status = Status.NotFound)) -> HttpCodec.Fallback.Condition.IsHttpCodecError,
)

handlers.nonEmptyOrElse(ifEmpty = noFound)(ZIO.identityFn)
}

val handler =
Handler.fromZIO(CodecConfig.codecRef.get).flatMap { config =>
val hdlrs = handlers(config)
Expand All @@ -344,13 +345,12 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
val error = cause.defects.head.asInstanceOf[HttpCodecError]
val response = {
val outputMediaTypes =
NonEmptyChunk
.fromChunk(
request.headers
.getAll(Header.Accept)
.flatMap(_.mimeTypes) :+ MediaTypeWithQFactor(MediaType.application.`json`, Some(0.0)),
)
.getOrElse(defaultMediaTypes)
(
request.headers
.getAll(Header.Accept)
.flatMap(_.mimeTypes) :+ MediaTypeWithQFactor(MediaType.application.`json`, Some(0.0))
).nonEmptyOrElse(defaultMediaTypes)(ZIO.identityFn)

codecError.encodeResponse(error, outputMediaTypes, config)
}
ZIO.succeed(response)
Expand Down

0 comments on commit 079f3d8

Please sign in to comment.