Skip to content

Commit

Permalink
Cleanup QuickRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou committed Jun 23, 2024
1 parent 28ae855 commit 35aca45
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions adapters/quick/src/main/scala/caliban/QuickRequestHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.stream.{ UStream, ZPipeline, ZStream }

import java.nio.charset.StandardCharsets.UTF_8
import scala.util.Try
import scala.util.control.NonFatal

final private class QuickRequestHandler[R](
Expand All @@ -41,22 +42,23 @@ final private class QuickRequestHandler[R](
def configureWebSocket[R1](config: quick.WebSocketConfig[R1]): QuickRequestHandler[R & R1] =
new QuickRequestHandler[R & R1](interpreter, config)

def handleHttpRequest(request: Request)(implicit trace: Trace): URIO[R, Response] =
ZIO.suspendSucceed { // Suspending to ensure that all the CPU work we're about to do happens in the ZIO thread pool
transformHttpRequest(request)
.flatMap(executeRequest(request.method, _))
.foldZIO(
Exit.succeed,
resp => Exit.succeed(transformResponse(request, resp))
)
}
def handleHttpRequest(request: Request)(implicit trace: Trace): URIO[R, Response] = ZIO.suspendSucceed {
transformHttpRequest(request)
.flatMap(executeRequest(request.method, _))
.foldZIO(
Exit.succeed,
resp => Exit.succeed(transformResponse(request, resp))
)
}

def handleUploadRequest(request: Request)(implicit trace: Trace): URIO[R, Response] =
def handleUploadRequest(request: Request)(implicit trace: Trace): URIO[R, Response] = ZIO.suspendSucceed {
transformUploadRequest(request).flatMap { case (req, fileHandle) =>
executeRequest(request.method, req)
.map(transformResponse(request, _))
.provideSomeLayer[R](fileHandle)
}.merge
executeRequest(request.method, req).provideSomeLayer[R](fileHandle)
}.foldZIO(
Exit.succeed,
v => Exit.succeed(transformResponse(request, v))
)
}

def handleWebSocketRequest(request: Request)(implicit trace: Trace): URIO[R, Response] =
Response.fromSocketApp {
Expand Down Expand Up @@ -131,10 +133,10 @@ final private class QuickRequestHandler[R](
partsMap: Map[String, FormField],
key: String
)(implicit jsonValueCodec: JsonValueCodec[A]): IO[Response, A] =
ZIO
Exit
.fromOption(partsMap.get(key))
.flatMap(_.asChunk)
.flatMap(v => ZIO.attempt(readFromArray[A](v.toArray)))
.flatMap(v => Exit.fromTry(Try(readFromArray[A](v.toArray))))
.orElseFail(Response.badRequest)

def parsePath(path: String): List[PathValue] = path.split('.').toList.map(PathValue.parse)
Expand Down

0 comments on commit 35aca45

Please sign in to comment.