-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize some zio.http.endpoint.Endpoint
code
#3213
Open
guizmaii
wants to merge
3
commits into
main
Choose a base branch
from
optimise/handlers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,8 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType]( | |
codecError: HttpCodec[HttpCodecType.ResponseType, HttpCodecError], | ||
documentation: Doc, | ||
authType: Auth, | ||
) { self => | ||
) { | ||
self => | ||
|
||
val authCombiner: Combiner[Input, authType.ClientRequirement] = | ||
implicitly[Combiner[Input, authType.ClientRequirement]] | ||
|
@@ -67,6 +68,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 | ||
|
||
/** | ||
|
@@ -257,34 +259,35 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType]( | |
def implementHandler[Env](original: Handler[Env, Err, Input, Output])(implicit trace: Trace): Route[Env, Nothing] = { | ||
import HttpCodecError.asHttpCodecError | ||
|
||
def authCodec(authType: AuthType): HttpCodec[HttpCodecType.RequestType, Unit] = authType match { | ||
case AuthType.None => HttpCodec.empty | ||
case AuthType.Basic => | ||
HeaderCodec.authorization.transformOrFail { | ||
case Header.Authorization.Basic(_, _) => Right(()) | ||
case _ => Left("Basic auth required") | ||
} { case () => | ||
Left("Unsupported") | ||
} | ||
case AuthType.Bearer => | ||
HeaderCodec.authorization.transformOrFail { | ||
case Header.Authorization.Bearer(_) => Right(()) | ||
case _ => Left("Bearer auth required") | ||
} { case () => | ||
Left("Unsupported") | ||
} | ||
case AuthType.Digest => | ||
HeaderCodec.authorization.transformOrFail { | ||
case _: Header.Authorization.Digest => Right(()) | ||
case _ => Left("Digest auth required") | ||
} { case () => | ||
Left("Unsupported") | ||
} | ||
case AuthType.Custom(codec) => | ||
codec.transformOrFailRight[Unit](_ => ())(_ => Left("Unsupported")) | ||
case AuthType.Or(auth1, auth2, _) => | ||
authCodec(auth1).orElseEither(authCodec(auth2))(Alternator.leftRightEqual[Unit]) | ||
} | ||
def authCodec(authType: AuthType): HttpCodec[HttpCodecType.RequestType, Unit] = | ||
authType match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated and unnecessary format change |
||
case AuthType.None => HttpCodec.empty | ||
case AuthType.Basic => | ||
HeaderCodec.authorization.transformOrFail { | ||
case Header.Authorization.Basic(_, _) => Right(()) | ||
case _ => Left("Basic auth required") | ||
} { case () => | ||
Left("Unsupported") | ||
} | ||
case AuthType.Bearer => | ||
HeaderCodec.authorization.transformOrFail { | ||
case Header.Authorization.Bearer(_) => Right(()) | ||
case _ => Left("Bearer auth required") | ||
} { case () => | ||
Left("Unsupported") | ||
} | ||
case AuthType.Digest => | ||
HeaderCodec.authorization.transformOrFail { | ||
case _: Header.Authorization.Digest => Right(()) | ||
case _ => Left("Digest auth required") | ||
} { case () => | ||
Left("Unsupported") | ||
} | ||
case AuthType.Custom(codec) => | ||
codec.transformOrFailRight[Unit](_ => ())(_ => Left("Unsupported")) | ||
case AuthType.Or(auth1, auth2, _) => | ||
authCodec(auth1).orElseEither(authCodec(auth2))(Alternator.leftRightEqual[Unit]) | ||
} | ||
|
||
val maybeUnauthedResponse = authType.asInstanceOf[AuthType] match { | ||
case AuthType.None => None | ||
|
@@ -295,13 +298,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).map(endpoint.output.encodeResponse(_, outputMediaTypes, config)).catchAll { error => | ||
ZIO.succeed(endpoint.error.encodeResponse(error, outputMediaTypes, config)) | ||
|
@@ -311,15 +312,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) | ||
|
@@ -343,13 +346,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) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated and unnecessary format change